Ffile2fix
Sign in Get started

How to fix MySQL "ERROR 2002: Can't connect through socket"

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

The MySQL client tried to connect through a local socket file and could not. Error (2) means the socket file does not exist, which almost always means the MySQL server is not running. It can also mean the client and server expect the socket in different places.

Also appears as: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) · SQLSTATE[HY000] [2002] No such file or directory · mysqli_real_connect(): (HY000/2002): Connection refused · ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)

Common causes

  • The MySQL or MariaDB service is stopped or crashed
  • MySQL failed to start because the disk is full or a config error
  • The client looks for the socket in a different path than the server creates it
  • PHP uses 'localhost' (socket) while MySQL runs in Docker or on another host
  • The MySQL process was killed for using too much memory
  • Permissions stop the client from reading the socket's directory

How to fix it

  1. Check that MySQL is running. Run sudo systemctl status mysql (or mariadb / mysqld). If it is stopped, start it with sudo systemctl start mysql.
  2. Read why it stopped. Check sudo journalctl -u mysql -n 50 and /var/log/mysql/error.log. Look for 'No space left on device', InnoDB errors or 'Out of memory'.
  3. Check disk space. Run df -h. If the disk holding /var/lib/mysql is full, free space (old logs, backups) before starting MySQL again.
  4. Find the real socket path. Run mysqladmin variables | grep socket or check socket= in /etc/mysql/my.cnf. Make the client use the same path, for example pdo_mysql.default_socket in php.ini.
  5. Use TCP when MySQL is remote. If MySQL runs in Docker or another server, connect to 127.0.0.1 or its hostname instead of localhost. localhost forces a socket connection in the MySQL client and PHP.
  6. Check for OOM kills. Run dmesg | grep -i -E 'killed process.*mysqld'. If MySQL was killed, lower innodb_buffer_pool_size or add memory or swap.

Diagnose a missing MySQL socket

sudo systemctl status mysql --no-pager
sudo tail -n 50 /var/log/mysql/error.log
df -h /var/lib/mysql
ls -l /var/run/mysqld/
mysql -h 127.0.0.1 -P 3306 -u appuser -p

How to stop it happening again

  • Monitor disk space on the database server
  • Enable MySQL to start at boot
  • Size innodb_buffer_pool_size to leave RAM for other services
  • Use 127.0.0.1 or a hostname when MySQL is not on the same machine

Frequently asked questions

What do the numbers (2) and (111) mean?

(2) means the socket file does not exist, so the server is usually down. (111) means the connection was refused, so nothing is accepting connections on it.

Why does 127.0.0.1 work but localhost fails?

localhost makes MySQL clients use the socket file, while 127.0.0.1 uses TCP. If only TCP works, the client's socket path is wrong.

Is my data lost?

Usually not. The server simply is not running. Check the error log before starting it again, especially after a crash or full disk.