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'

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 13 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
13 comments… add one
  • blagmore Jul 26, 2012 @ 17:57

    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 Jul 26, 2012 @ 23:37

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

  • John Meichle Jul 27, 2012 @ 0:21

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

  • layzee Jul 27, 2012 @ 3:01

    @ 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 Jul 28, 2012 @ 9:39

    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 Aug 8, 2012 @ 14:56

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


  • quad Aug 10, 2012 @ 15:37


    mysql -u root -p -h’′

    or another host if you connect to foreign host.


  • Lyubomir Oct 21, 2012 @ 7:46

    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 Feb 27, 2013 @ 7:21

    if mysql is really running

    open and edit /etc/my.cnf

    add this lines:

    restart mysql

    mysql -u root -p


  • swathi Jul 17, 2013 @ 6:54

    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 Jan 6, 2014 @ 1:05

    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 Feb 3, 2014 @ 11:26

    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

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum