How to solve Can’t connect to local MySQL server through socket

  • by

If you getting the 2002 error trying to connect to MySQL:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

It simply means that the MySQL server is not installed or running, or mysql.sock file does not exist in /var/lib/mysql/ folder.

How to solve Can’t connect to local MySQL server through socket error

  1. Check whether MySQL server is running or not

Run the following command in terminal to do this:

service mysqld start

Then try to connect to MySQL server again.

  1. Ensure that my.cnf file is configured correctly

Open my.cnf file (usually located in /etc/) and add the lines:

[mysqld]

socket=/var/lib/mysql/mysql.sock 

[client]

socket=/var/lib/mysql/mysql.sock

Restart the server

service mysql restart

and try to connect to MySQL again

  1. Try to use TCP/IP connection instead of UNIX socket connection

Connect to 127.0.0.1 instead of localhost. Doing this you will connect to MySQL server using TCP/IP connection. So when socket connection is not working, try to use 127.0.0.1 instead.

  1. Try to change permissions to MySQL folder if you are working locally:
sudo chmod -R 755 /var/lib/mysql/

Restart the server and try to connect to MySQL again.

  1. Create symlink to mysql.sock

Sometimes mysql.sock might be located in another folder. You should find it and symlink it. If your mysql.sock file is located at /tmp/mysql.sock, you can symlink it using following command:

ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

Restart the server and try to connect to MySQL again.


Download dbHarbor for free to connect to MySQL server remotely or locally.

⚓️  dbHarbor is a native macOS tool with outstanding user interface for managing SQL databases.

Missing a feature in dbHarbor? See the dbHarbor Roadmap or give us feedback using our public Roadmap powered by FeedbackScout.

dbHarbor: SQLite

Don’t miss the latest news about our development journey and SQL tutorials. Signup for our newsletter. Promise not to SPAM😎

Leave a Reply

Your email address will not be published. Required fields are marked *