Can't connect to local MySQL server through socket [Solved]
If you run into this error message while trying to connect to a local MySQL Server:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
It means either the MySQL server is not installed/running, or the file mysql.sock
doesn’t exist in /var/lib/mysql/
.
There are a couple of solutions for this error.
1. First, check to see whether mysqld service is running or not. If not, start it:
service mysqld start
Then try to connect again.
2. Try to connect to 127.0.0.1
instead of localhost
If you connect to localhost
, it will use the socket connector, but if you connect to 127.0.0.1
the TCP/IP connector will be used. So when the socket connector is not working, try connecting to 127.0.0.1
instead.
3. Edit file my.cnf
Find the file my.cnf
(usually in /etc/
) to edit and add these following line:
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
Restart mysql and connect again.
4. Symlink
In some cases, you mysql.sock
might be located in another folder. You have to find and symlink it. You might find it in /tmp/mysql.sock
or /data/mysql_datadir/mysql.sock
For example, your file is located as in /tmp/mysql.sock
Then symlink it:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
Restart mysql and connect again.
Need a good GUI Tool for MySQL? TablePlus is a modern, native tool with an elegant UI that allows you to simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS.