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

The client tried to connect through the Unix socket file and couldn’t. The number in parentheses is the system error: (2) “No such file or directory” — the socket doesn’t exist at that path; (13) “Permission denied”; (111) “Connection refused”. Most of the time, MySQL simply isn’t running.

1. Is the server running?

sudo systemctl status mysql      # or: mariadb / mysqld

If it’s stopped or failed, find out why before starting it:

sudo journalctl -u mysql -n 50 --no-pager
sudo tail -n 50 /var/log/mysql/error.log

Then:

sudo systemctl start mysql

2. Why did it stop? Common causes

Out of memory. The kernel’s OOM killer often picks MySQL on small servers:

sudo journalctl -k | grep -iE "out of memory|killed process"

Fix: lower innodb_buffer_pool_size, reduce PHP/app workers, add swap — see how much RAM your VPS needs and InnoDB buffer pool sizing.

Disk full. MySQL can’t write and may refuse to start:

df -h

Free space and start again — see disk full troubleshooting.

Configuration error after editing my.cnf — the error log names the bad option (unknown variable 'xyz'). Fix it or move the file aside.

Data directory permissions after copying or restoring files: sudo chown -R mysql:mysql /var/lib/mysql. On Ubuntu with a moved datadir, AppArmor can also block access — see moving the MySQL data directory.

InnoDB crash recovery — after an unclean shutdown, startup may take a while or fail with corruption messages. Read the log carefully; innodb_force_recovery (1–6) can help extract data in emergencies, but restore from backup when possible.

Port or socket in use by another instance — check sudo ss -xlp | grep mysql and ps aux | grep mysqld.

3. The server runs but the socket path differs

Find where the server’s socket actually is:

sudo ss -xlp | grep -i mysql
sudo mysqld --verbose --help 2>/dev/null | grep -E '^socket'

Compare with where the client looks. Fix the client side:

mysql -u app -p --socket=/var/lib/mysql/mysql.sock

Or permanently in ~/.my.cnf / /etc/mysql/conf.d/client.cnf:

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

PHP: pdo_mysql.default_socket / mysqli.default_socket in php.ini, or unix_socket=… in the DSN.

Or use TCP instead of the socket: -h 127.0.0.1 (not localhost — localhost means “socket” to MySQL clients). In WordPress, DB_HOST set to 127.0.0.1 forces TCP. Make sure a matching 'user'@'127.0.0.1' account exists — see error 1045.

4. /var/run/mysqld missing

/run is a tmpfs recreated at boot; the systemd unit creates /run/mysqld. If you start mysqld manually (e.g. for password recovery), create it yourself:

sudo mkdir -p /var/run/mysqld && sudo chown mysql:mysql /var/run/mysqld

5. Permission denied (13)

The client user can’t access the socket or its directory. Check:

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

The directory should be traversable (755) and the socket world-connectable (srwxrwxrwx). In containers or chroots (PHP-FPM chroot), the socket path must exist inside the chroot — TCP is simpler there.

6. Docker

Inside another container, there’s no MySQL socket — connect over TCP to the database service name (db:3306). From the host to a containerised MySQL, use -h 127.0.0.1 -P <published port>.

Quick checklist

  1. systemctl status mysql — running?
  2. Error log + journalctl -k — OOM, disk, config, permissions?
  3. Socket path on server vs client?
  4. Try -h 127.0.0.1 to bypass the socket.

Related: installing MySQL on Ubuntu and WordPress “Error establishing a database connection”.

  • MySQL
  • MariaDB
  • Troubleshooting