Register Login

Can't connect to local mysql server through socket 'varrunmysqldmysqld.sock' (2)

Updated May 18, 2023

Have you ever experienced this error "can't connect to local mysql server through socket '/var/run/mysqld/mysqld.sock' (2)?" Users can fix this error if they find the right problem behind this error.

For several reasons, users can encounter such errors, especially when users d multiple installations of MySQL in the operating system. In the following section of the article, users will see how to resolve this error after defining what this error is.

What causes the error "can't connect to local mysql server through socket '/var/run/mysqld/mysqld.sock' (2)?"

Generally, when users encounter this error message, it indicates while running this application, it cannot locate the socket file required to establish a MySQL connection.

And most importantly, the operating system uses this socket file to allow interface services like MySQL or PHP to connect and communicate with each other. Often, users encounter this error message when they try to restart their MySQL application and run the below command:

/usr/local/mysql/bin/mysql start will cause this error.

There are other reasons, like when users try to login into the application with different credentials. The MySQL database will reflect the error message "Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2)."

How to fix this error "Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2)" in the MySQL server?

We can encounter the above errors, which might arise for several reasons. But according to these problems, users can also find associated solutions that can no doubt fix this MySQL error.

If users cannot fix the error with one solution, which might not work, they can proceed to the other solutions moving down the article until they find the right one to resolve this problem.

Approach 1: Resolving the error by checking the MySQL service

Step 1: In this approach, users will check if their MySQL is running. The following command will help check the status of MySQL service:

sudo systemctl status mysql

Step 2: Users can use the following command to restart the application:

sudo systemctl start mysql

Step 3: Now, to resolve this error, users can set the MySQL service to start booting automatically using the below command:

sudo systemctl enable mysql

Approach 2: Verifying the mysqld.sock Location

As mentioned earlier, the error "can't connect to local mysql server through socket '/var/run/mysqld/mysqld.sock' (2)" may occur when MySQL cannot find the mysql.sock socket file.

Step 1: For this, users must first find all the sockets in their system, and they can use this command:

sudo find / -type s

Step 2: Go to the MySQL configuration file in a text editor of the users' choice. Type this command to use nano:

sudo nano /etc/mysql/my.cnf

Step 3: Use the following command of the MySQL configuration file:

[mysqld]
socket=[path to mysqld.sock]
[client]
socket=[path to mysqld.sock]

Approach 3: Check if added multiple MySQL instances

This issue can also be responsible for causing the error "Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2)."

Step 1: Stop mysqld process

service mysqld stop

OR

/etc/init.d/mysqld stop

OR

mysqladmin -u root -p shutdown

Step 2: Listing all the instances of MySQL

Here, users use the below command to list all the instances:

ps -A|grep mysqld

Step 3: If users find multiple instances running in the background, they need to terminate them using the following command:

sudo pkill -9 mysqld

Step 4: Users then restart the MySQL service to trigger only one instance using this command:

sudo systemctl restart mysql

Approach 4: Checking the MySQL folder permission

It can be another reason why users are getting such errors is that the MySQL Service cannot access the /var/run/mysqld directory due to permission restrictions. So users should give access to it by following the given steps:

Step 1: Go to settings, change the settings, and give permission for the mysqld directory to resolve this problem using the command:

sudo chmod -R 755 /var/run/mysqld

Users set the permission to 755, which lets the root user read, write, and run the directory while using the rest other; users can only read and run.

Step 2: Then, lastly, users restart the MySQL service to apply the changes to take place using the following command:

sudo systemctl restart mysql

Conclusion:

We hope this article has provided detailed solutions on how users can check and resolve the MySQL error "can't connect to local mysql server through socket '/var/run/mysqld/mysqld.sock' (2) successfully." This article will give you a clear idea of what the error is and the causes behind such unidentified errors.


×