Console9

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

Diagnose and fix MySQL socket connection errors when the server is stopped or the socket path is wrong.

MySQL produces "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'" when the MySQL client cannot find or connect to the Unix socket file.

What Causes "ERROR 2002" in MySQL

The MySQL server (mysqld) is not running. The socket file does not exist when the server is stopped.

The socket file path in the client configuration does not match the server's actual socket path. The server may create the socket at /tmp/mysql.sock while the client looks at /var/run/mysqld/mysqld.sock.

How to Fix

  1. Check if MySQL is running:
systemctl status mysql
  1. Start MySQL if it is stopped:
sudo systemctl start mysql
  1. Find the actual socket path:
find / -name "mysqld.sock" 2>/dev/null
  1. Connect using the correct socket path:
mysql -u root -p --socket=/var/run/mysqld/mysqld.sock

MySQL: ERROR 1045 Access denied— the socket connects but authentication fails.