“ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock'” Error and Solution


MySQL service provides different connection methods. We generally prefer TCP/IP way where connection is established over Network Socket. There is alternative way named Socket which is connected over local system. During connection we may get error like Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ . In this tutorial we will look to solve this problem.

Solution 1 – Start The Service

Socket is created by the MySQL service and if the service is not the started the socket will not created. So starting the service is the first step to solve this problem. We will use systemctl  command.

$ sudo systemctl start mysql.service

AND check service status

$ sudo systemctl status mysql.service
Solution 1 - Start The Service
Solution 1 – Start The Service

Solution 2 – Check The Socket Path

As we try to connect to the socket the path of the Socket should be correctly specified. By the default the socket is /var/run/mysqld/mysqld.sock and this may be changed. We can check current path configuration of the socket from config file which is /etc/mysql/debian.cnf .

$ sudo cat /etc/mysql/debian.cnf
Solution 2 - Check The Socket Path
Solution 2 – Check The Socket Path

Solution 3 – Check Data Directory Permissions

One of the another reason to get related error is MySQL server do not started. This may be cause with wrong directory permissions. We should check MySQL service data path permissions with the following command and make them writeable by the MySQL service.

$ sudo chmod -R 755 /var/lib/mysql/

LEARN MORE  Linux Netstat Command With Examples

Leave a Comment