If you have done various projects with MySQL, Error 2002 (hy000): can’t connect to local MySQL server through socket ‘/tmp/mysql.sock message is not a strange issue.
When receiving this message, you cannot install or run MyQSL server or the mysql.sock file doesn’t exist in /var/lib/mysql/. Here are some solutions for this error.
What Is Exactly The Problem?
The error notifies that the application cannot locate the socket file to establish a MySQL connection. The operating system uses this file to enable interface services such as PHP and MySQL. This way, it can interact and communicate among the two.
For some coders, the message will appear when restarting the MySQL and running the command:
Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’ (38)
On the other hand, the error can arise when you log in the database using credentials.
SQLSTATE[HY000]  Can’t connect to local MySQL server through socket
How To Fix Error 2002 (Hy000): Can’t Connect To Local MySQL Server Through Socket ‘/tmp/mysql.sock
Method 1: Check MySQL Service
Before trying out other methods, you should check and make sure that the service is running. Run the command:
mysqladmin -u root -p status
If the service is not running, let’s run the
service mysqld start
code to start it.
Method 2: Connect With 127.0.0.1
Instead of localhost, you should connect to MySQL with 127.0.0.1 IP address. If you use the localhost, the operating system will use the socket connector.
However, with 127.0.0.1 IP address, The system employs the TCP/IP connector.
Method 3: Modify The my.cnf File
This is a configuration file in MySQL, which is run by the mysqladmin or mysql service. Modify the file as follows:
[mysqld] socket=/var/lib/mysql/mysql.sock [client] socket=/var/lib/mysql/mysql.sock
After this, restart the service and try reconnecting it.
Method 4: Verify mysql.sock Location
The problem can also arise from the mysql.sock file existing in another directory. Thus, you will have to locate the file and create a symlink.
You can find it in both /data/mysql_datadir/mysql.sock and /tmp/mysql.sock.
Here is how to create a symlink:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
Method 5. Change MySQL Folder Permission
If you work on a local environment, run the following command to change the folder’s permission:
sudo chmod -R 755 /var/lib/mysql/
Now let’s restart the service and establish the connection:
service mysqld start
There are various reasons for Error 2002 (hy000): can’t connect to local MySQL server through socket ‘/tmp/mysql.sock. However, our article has offered five common methods to deal with the problem. Choose the one that suits your situation and deals with the compatible operating system.
Leave a comment