Can’t connect to local MySQL server through socket [SOLVED & FIXED]
If you work with MySQL then you’re probably familiar with the can’t connect to local mysql server through socket error message. And you’ll see error message like:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2 “No such file or directory”)
In this short article, we’ll show you how to troubleshoot and fix this issue in a jiffy.
And, don’t worry it’s a pretty straight up fix and you have nothing to worry about.
So let’s go.
What Is Can’t Connect to Local MySQL Server Through Socket Error?
So what’s causing the can’t connect to local mysql server through socket error message?
This error message is basically telling you that the application is not able to locate the socket file needed to establish a MySQL connection.
And as you well know the socket file is used by the operating system to enable interface services such as MySQL or PHP to interact and communicate among each other.
For some people this appear message may occur when trying to restart MySQL and run the:
command resulting in the error message:
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
On the other hand, for others, this error message arises when they try to log into the MySQL database using any user credentials.
Then you have those we experience this same problem with their website application especially those built with PHP.
For example, all of a sudden the website refuses to work, and when you pry into the log file you see the error message logged there similar to this one:
SQLSTATE[HY000]  Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
How to Fix Can’t Connect to Local MySQL Server Through Socket Error
So how do you fix this can’t connect to local mysql server through socket error message?
Well, here are some steps you need to take to resolving this issue:
Solution #1: Check If MySQL Service is Runnning
First of all, check to make sure the mysqld service is running or not. To check if MySQL is running, run the following command:
mysqladmin -u root -p status
And if the service is not running, then, by all means, start it up by running the following command:
service mysqld start
Once you restart the service, try again to connect to MySQL.
Solution #2: Connect with 127.0.0.1
Another possible solution to the >can’t connect to local mysql server through socket> error message is to try and connect to the MySQL using the 127.0.0.1 ip address instead of localhost.
When you use localhost to connect to MySQL, the operating system uses the socket connector.
However, if you use 127.0.0.1 ip address, the operating system will use the TCP/IP connector.
So as a possible solution when you’re having issues with the socket connector, you can try to establish the connection using TCP/IP by specifying the 127.0.0.1 ip address instead of localhost.
Solution #3: Modify the my.cnf file
The my.cnf file is a configuration file used by MySQL. And it’s processed by either the mysqld or mysqladmin service depending on the options specified.
So locate this my.cnf file which is normally in the /etc/ directory and modify it accordingly as follows:
[mysqld] socket=/var/lib/mysql/mysql.sock [client] socket=/var/lib/mysql/mysql.sock
After modifying the file restart your MySQL service and try connecting again.
Solution #4: Verify mysql.sock Location
Another possible cause of this problem is that the mysql.sock file in another directory.
So you need to locate where the mysql.sock file and create a symlink to it.
Most times the file is located in either the /data/mysql_datadir/mysql.sock or /tmp/mysql.sock
So for instance, if you locate the file in /data/mysql_datadir/mysql.sock, all you need to do is create a symlink for it using the following command:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
Once you do this, go ahead and restart the MySQL service and try initiating the connection again.
Solution #5: Change MySQL Folder Permission
There one other possible solution fixing this issue and that is to change the MySQL folder’s permission.
And this can be done easily by running the following command if you’re working on a local environment:
sudo chmod -R 755 /var/lib/mysql/
After which you can restart mysql service:
service mysqld start
Then try to establish the connection again.
Solution #6: Multiple MySQL Instances
In some cases, the installation of multiple MySQL on the server environment causes this problem.
So you have a case whereby multiple instances of MySQL are running at the same time.
As such the issue running the following commands can fix the issue:
ps -A|grep mysql
Next, kill the mysql process by running this command:
sudo pkill mysql
Then do the same thing for mysqld:
ps -A|grep mysqld
Again, kill this process as well:
sudo pkill mysqld
Finally, go ahead run the following commands to restart and connect to the MySQL server:
sudo service mysql restart mysql -u root -p
Conclusion on Can’t Connect to Local MySQL Server Through Socket
We hope one of the above possible solutions to the can’t connect to local mysql server through socket error resolves your situation.