MySQL · December 20, 2023

How to Fix MySQL Error 2002 - (CR_CONNECTION_ERROR) Can't connect to local MySQL server through socket '%s' (%d)

How to Fix MySQL Error 2002 - (CR_CONNECTION_ERROR) Can't connect to local MySQL server through socket '%s' (%d)

MySQL is a popular open-source relational database management system used by many websites and applications. However, sometimes users may encounter an error message that says "Error 2002 - Can't connect to local MySQL server through socket." This error can be frustrating, but fortunately, there are several steps you can take to resolve it.

1. Check if MySQL Service is Running

The first thing you should do is check if the MySQL service is running on your server. You can do this by running the following command in your terminal:

sudo service mysql status

If the service is not running, you can start it by running:

sudo service mysql start

If the service is already running, you may need to restart it:

sudo service mysql restart

2. Verify MySQL Socket Path

MySQL uses a socket file to communicate with client applications. The error message indicates that it cannot connect to the local MySQL server through the socket file. You can check the socket path in the MySQL configuration file, usually located at /etc/mysql/my.cnf or /etc/my.cnf.

Open the configuration file using a text editor and look for the socket directive. It should look something like this:

socket = /var/run/mysqld/mysqld.sock

Make sure the socket path is correct. If it is different, update it accordingly. Save the changes and restart the MySQL service.

3. Check MySQL Port

By default, MySQL listens on port 3306. If you have changed the default port, you need to make sure that the client applications are connecting to the correct port. You can check the port configuration in the MySQL configuration file.

Open the configuration file and look for the port directive. It should look something like this:

port = 3306

If the port is different, update it accordingly. Save the changes and restart the MySQL service.

4. Check MySQL Host

If you are trying to connect to a remote MySQL server, make sure you have the correct hostname or IP address. You can check the host configuration in the MySQL configuration file.

Open the configuration file and look for the bind-address directive. It should look something like this:

bind-address = 127.0.0.1

If you are connecting to a remote server, make sure the IP address or hostname is correct. Save the changes and restart the MySQL service.

5. Check Firewall Settings

If you are still unable to connect to the MySQL server, it is possible that your firewall is blocking the connection. Make sure that the MySQL port is open in your firewall settings.

You can check the firewall status by running the following command:

sudo ufw status

If the firewall is active, you can open the MySQL port by running:

sudo ufw allow 3306

Restart the MySQL service and try connecting again.

By following these steps, you should be able to fix the MySQL Error 2002 and connect to your local or remote MySQL server successfully.

Summary

In summary, MySQL Error 2002 - (CR_CONNECTION_ERROR) Can't connect to local MySQL server through socket '%s' (%d) can be resolved by checking if the MySQL service is running, verifying the MySQL socket path, checking the MySQL port and host configurations, and ensuring that the firewall settings allow the MySQL connection.

If you are looking for reliable VPS hosting solutions, consider Server.HK. With top-notch performance and excellent customer support, Server.HK offers a wide range of VPS hosting plans to meet your needs.