• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

How to Fix MySQL Error 2002 – (CR_CONNECTION_ERROR) Can’t connect to local MySQL server through socket ‘%s’ (%d)

December 20, 2023

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.

Recent Posts

  • Hong Kong VPS Docker Setup: Run Containers with Full Root Access
  • How to Set Up a Game Server on Hong Kong VPS: Low-Latency Gaming for Asia
  • How to Deploy a Node.js Application on Hong Kong VPS: Complete Guide
  • How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  • How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026

Recent Comments

  1. hello world on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot