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'
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 9 comments… read them below or add one }
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.
Please mention that disk space problems on / or /var might might cause this problem too. Thx
I agree, even though I have been following this site since i found it.
@ 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
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!
* After reading post title, I was very excited to read solution but……..highly disappointed . I was expected in-dept solution for this error…
–NIxson
try
mysql -u root -p -h’127.0.0.1′
or another host if you connect to foreign host.
-quad
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.
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,,