When you are working with MySQL server, you are getting the ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock
‘ (2) on your terminal window, it means that mysqld socket is missing or its path is set incorrectly in your mysql server.
Here are some 2 solutions to resolve ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) ubuntu linux:
Solution 1: Set the Correct Path for mysqld.sock File
Here are the steps to fix this error by setting the correct path of mysqld.sock file:
Step 1: Check the socket file location
Open your command line or terminal window, and type the command mysql_config --socket
, press enter, this will tell you the path to the MySQL socket:
mysql_config --socket
The default location for the socket file is /var/run/mysqld/mysqld.sock
.
If the socket file is located elsewhere or missing, you need to specify the correct location while connecting to the database or create new mysqld socket.
Step 2: Edit Configuration File
Set the correct path of mysqld.sock
file, just simply type sudo nano /etc/mysql/my.cnf
command and press enter, it will open my.conf
file on your command line:
sudo nano /etc/mysql/my.cnf
Now find socket = /tmp/mysql.sock
line and change it to socket = /var/run/mysqld/mysqld.sock
in my.cnf file; Like the following:
///find this line
socket = /tmp/mysql.sock
////Change it to
socket = /var/run/mysqld/mysqld.sock
Save and close my.cnf
file from command line.
And type the sudo systemctl restart mysql
command at the command line to restart your MySQL server to apply the changes you made:
sudo systemctl restart mysql
Solution 2: Set the Correct Path for mysqld.sock File
Here are the steps to fix this error by creating mysqld directory and mysqld.sock file in mysql server:
Step 1: Create mysqld Directory
If your MySQL server does not have a mysqld directory, you can type the sudo mkdir /var/run/mysqld
command on a command line window and press Enter, which will create the mysqld directory inside /var/run/
:
sudo mkdir /var/run/mysqld
Step 2: Create mysqld.socket File
Now you can create the mysqld.sock
file inside the mysqld
directory, Simply type the command touch /var/run/mysqld/mysqld.sock
on the command line window, press enter:
touch /var/run/mysqld/mysqld.sock
Step 3: Set Correct Permissions
Now the permission of the mysqld
directory and mysqld.sock
file that you have created has to be set to 755, Simply type the sudo chmod 755 /var/run/mysqld/
command on the command line to set the directory permissions, and type the sudo chmod 755 /var/run/mysqld/mysqld.sock
command on the command line to set file permissions:
sudo chmod 755 /var/run/mysqld/ sudo chown mysql:mysql /var/run/mysqld/ sudo chmod 755 /var/run/mysqld/mysqld.sock sudo chown mysql:mysql /var/run/mysqld/mysqld.sock
To apply the changes you made, simply type the sudo systemctl restart mysql
command at the command line to restart your MySQL server:
sudo systemctl restart mysql
Here is the video solution for this error:
Conclusion
In conclusion, the “Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)” error message is a common issue that can be resolved by following solutions mentioned above.