Introduction
This an article where there is a relation with the previous article in this link. That article has a title of ‘How to Solve Error Message ERROR 2013 (HY000): Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0′. The main cause of the error in that article is because the connection process to the MySQL Database Server failed. The reason why it failed because of the connection is not possible. It is because the socket file in ‘/var/run/mysqld/mysqld.socket’ is not exist. Although the process of MySQL Database Server is running, the connection still failed. Below is the actual connection process to MySQL Database Server :
root@hostname ~# mysql -uroot ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) root@hostname ~#
The above command pattern is ‘mysql -uroot’ where the execution of the connection is using ‘root’ account without specifying password further. It is because there is no ‘-p’ additional parameter for passing password to connect to MySQL Database Server. Eventhough the password is not a part of the command, the connection is still failed. The error message informs that the connection through the socket file located in ‘/var/run/mysqld/mysqld.sock’ failed.
Solving the problem
So, after finding the main cause, the following are steps to solve the problem :
1. Check the file socket for representing the connection MySQL Database Server. It exist in ‘/var/run/mysqld’. Below is the actual content of the file :
root@hostname ~# cd /var/run/mysqld/ root@hostname mysqld(keystone)# ls mysqld.pid mysql.sock mysql.sock.lock root@hostname ~#
According to the output above, there are no file with the name of ‘mysqld.sock’ at all in the ‘/var/run/mysqld’. So, since it really need the socket file with the name of ‘mysqld.socket’, just define it in the MySQL Database Server’s configuration file. Just read the article in this link to check for the running configuration file used by MySQL Database Server. After finding the right configuration file of the MySQL Database Server, don’t forget to add the following line :
socket = /var/run/mysqld/mysqld.sock
Just add it on the mysqld block as follows :
[mysqld] socket = /var/run/mysqld/mysqld.sock
Don’t forget to restart the service of MySQL Database Server. After successfully restarting the service, try to reconnect again as in the above or previous command execution.