Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

by on July 26, 2012 · 13 comments· LAST UPDATED July 26, 2012

in

I'm getting the following error when I run mysql -u root -p

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

How do I solve this problem under CentOS / RHEL / Red Hat Enterprise Linux server 5.x or 6.x?

To solve this problem take the following steps.

Is MySQL Running?

Type the following command
# service mysqld status
If mysqld is not running, start it:
# service mysqld start
Try mysql command again as follows:
$ mysql -u USER -p

Is MySQL Installed?

Make sure the package mysql-server is installed:
# rpm -qa mysql-server
Sample outputs:

mysql-server-5.1.61-4.el6.x86_64

If you do not see package name, type the following command to install mysql-server:
# yum install mysql-server
Turn on mysqld to start automatically on boot:
# chkconfig mysqld on
Run mysqld service, enter:
# service mysqld start
Set a new root password for mysql server:
# mysqladmin -u root password 'MySQL-Root-User-Password'

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 13 comments… read them below or add one }

1 blagmore July 26, 2012 at 5:57 pm

The quality of posts are really low of late.
This for example. You check it’s on then you check if it’s installed.
Yet from the title of the article it sounds like you’re going to go into depth on problems that can cause this condition even when MySQL is running and whatnot.

Reply

2 Mohamed Zaian July 26, 2012 at 11:37 pm

Please mention that disk space problems on / or /var might might cause this problem too. Thx

Reply

3 John Meichle July 27, 2012 at 12:21 am

I agree, even though I have been following this site since i found it.

Reply

4 layzee July 27, 2012 at 3:01 am

@ blagmore while i agree this was not a sophisticated post, 70% of the times that error is as a result of mysql not running/installed. There so many reason why this can happen and its impossible to list every scenario since it is going to be specific to user machine

I ll just add a few things that can be checked also.
– check the logs, /var/log/mysql
– check your permission to mysql folder.
– Check your disk space if you have run out.
– check if /var/lib/mysql/mysql.sock file exists
– check the /etc/my.conf for the bind location, you can try localhost
– check the /etc/my.conf for socket setting, or change to preferred location
socket=/var/lib/mysql/mysql.sock

By the way Vivek great job. I love cyberciti

Reply

5 onex July 28, 2012 at 9:39 am

These things are very basic *if* you know how. You and I do know, but there will be those for whom this is new territory.

@Vivek: Maybe there should be some sort of traffic light system for posts that signify the difficulty rating of a post (i.e. easy, moderate, difficult) and then allow users to filter out the posts that best suit their ability. However, rather than one person deciding the difficulty, allowing the community that visits your site to reach a mean consensus by hitting one of three options on a post. Would probably need capability to register though to work properly.

Vivek – great work, keep it up!

Reply

6 Nixson August 8, 2012 at 2:56 pm

* After reading post title, I was very excited to read solution but……..highly disappointed . I was expected in-dept solution for this error…

–NIxson

Reply

7 quad August 10, 2012 at 3:37 pm

try

mysql -u root -p -h’127.0.0.1′

or another host if you connect to foreign host.

-quad

Reply

8 Lyubomir October 21, 2012 at 7:46 am

quad: Spot on! Thank you!
I’m on a shared server and /var/lib/mysql/mysql.sock does not exist, and I have a toolset so restricted that I can’t locate the socket file (if it exists; they might not be going this way). Furthermore, what renders this article useless to me is that I don’t have the necessary permissions to start/stop services or edit conf files.
Having -h 127.0.0.1 seems to have forced mysql to use tcp/ip instead of a socket file and I was able to do through.
A fun fact: -h localhost instead of 127.0.0.1 didn’t help, which I find rather strange.

Reply

9 chipz February 27, 2013 at 7:21 am

if mysql is really running

open and edit /etc/my.cnf

add this lines:
[client]
socket=/var/lib/mysql/mysql.sock

restart mysql

mysql -u root -p

finish,,

Reply

10 swathi July 17, 2013 at 6:54 am

For this question i am giving answer

Here we have to check the IP address and Port which we connected in the config file.
suppose if developers and QA team using same port, that time also we will face this issue.

So now change the port num in config file at webservice from server side and client side.

before that check the credentials that we are able to login to the database

Reply

11 CAM January 6, 2014 at 1:05 am

What if mysql server is not running? You always get that it needs the mysql.sock to start. Mysql.sock is missing.
Can this be fixed? and how?

Reply

12 Vishwanath Kamble February 3, 2014 at 11:26 am

I had this same problem, and I solved it.
I had entered bind address in my.cnf file and when I started my server today, it took some other IP from DHCP. So just enter the new IP in bind address or just keep 127.0.0.1

Reply

13 nivedita May 10, 2014 at 11:43 am

when we type service mysqld status then unrecognize service error msg show

Reply

Leave a Comment

Tagged as: , , , , , , , , , , ,

Previous Faq:

Next Faq: