Skip to content
ERROR 2002 (HY000): Can't connect to local MySQL server

ERROR 2002 (HY000): Can't connect to local MySQL server

DodaTech 2 min read

MySQL ERROR 2002 (HY000) means the client cannot reach the MySQL server through the expected socket or port. This typically points to the MySQL service not running, a wrong socket path, or a missing installation.

What It Means

The mysql client tried to connect to the local server via /var/run/mysqld/mysqld.sock (Unix socket) or 127.0.0.1:3306 (TCP) but no process was listening at that address.

Why It Happens

  • MySQL service is stopped or crashed.
  • The socket file was deleted or moved.
  • MySQL server is not installed.
  • The server is listening on a different port or bound to a different interface.
  • Disk space is full, preventing the socket file from being created.

How to Fix It

1. Check if MySQL is running

sudo systemctl status mysql

If it shows inactive (dead), start the service:

sudo systemctl start mysql
sudo systemctl enable mysql

2. Check the socket file

Verify the default socket exists:

ls -la /var/run/mysqld/mysqld.sock

If missing, find the correct socket:

mysqladmin --socket=/tmp/mysql.sock ping

Or configure the socket path in /etc/mysql/my.cnf:

[client]
socket = /var/run/mysqld/mysqld.sock

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

3. Force TCP connection instead of socket

mysql -h 127.0.0.1 -P 3306 -u root -p

4. Reinstall MySQL if missing

sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation

5. Check for disk space issues

df -h

If /var is full, free up space and restart MySQL.

FAQ

What is the difference between socket and TCP connection?
A Unix socket connects through the filesystem (/var/run/mysqld/mysqld.sock) and is faster for local connections. TCP (127.0.0.1:3306) uses the network stack and is required for remote connections.
Why does systemctl start mysql succeed but the error persists?
MySQL may have started but failed immediately due to a corrupted database or misconfiguration. Check sudo journalctl -u mysql for startup errors, and inspect /var/log/mysql/error.log.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro