≡ Menu

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

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'

{ 13 comments… add one }

  • blagmore July 26, 2012, 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.

  • Mohamed Zaian July 26, 2012, 11:37 pm

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

  • John Meichle July 27, 2012, 12:21 am

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

  • layzee July 27, 2012, 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

    By the way Vivek great job. I love cyberciti

  • onex July 28, 2012, 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!

  • Nixson August 8, 2012, 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…


  • quad August 10, 2012, 3:37 pm


    mysql -u root -p -h’′

    or another host if you connect to foreign host.


  • Lyubomir October 21, 2012, 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 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.

  • chipz February 27, 2013, 7:21 am

    if mysql is really running

    open and edit /etc/my.cnf

    add this lines:

    restart mysql

    mysql -u root -p


  • swathi July 17, 2013, 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

  • CAM January 6, 2014, 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?

  • Vishwanath Kamble February 3, 2014, 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

  • nivedita May 10, 2014, 11:43 am

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

Leave a Comment

   Tagged with: , , , , , , , , , , ,