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

last updated in Categories

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:


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'


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

13 comment

  1. 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.

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

  3. @ 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

    By the way Vivek great job. I love cyberciti

  4. 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!

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


  6. 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 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 didn’t help, which I find rather strange.

  7. if mysql is really running

    open and edit /etc/my.cnf

    add this lines:

    restart mysql

    mysql -u root -p


  8. 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

  9. 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?

  10. 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

    Still, have a question? Get help on our forum!