Skip to content
ohsudo, Home
Databases

MySQL "Can't connect to local MySQL server through socket": find out why (ERROR 2002)

Fix MySQL ERROR 2002 "Can't connect to local MySQL server through socket": is the server running, where is the socket, and why localhost differs from 127.0.0.1.

Beginner4 min readApplies to MySQL 8.0 · MySQL 8.4 LTS · MariaDB 10.6+ · Ubuntu · Debian · RHEL / Rocky / AlmaLinux

by OhSudo ··

Checked on real software: MySQL 8.4 · November 2026How we check

Not sure this is your cause?Find it step by step: Can't connect to the database →

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

When you connect to localhost, the MySQL client doesn’t use the network. It talks to the server through a socket file on disk. This error means it couldn’t: most often because the server isn’t running, sometimes because the client and server look for the socket in different places.

The number at the end tells you which:

Number Meaning Usual cause
(2) No such file The server is stopped, or the socket is elsewhere
(111) Connection refused A socket file left behind by a server that crashed
(13) Permission denied Your user can’t reach the socket, or SELinux / AppArmor blocks it
  1. mysql -h localhostuses the socket file
  2. /var/run/mysqld/mysqld.sockmissing (2), stale (111), no access (13)(breaks here)
  3. mysqldstopped, or its socket is elsewhere
For localhost, the MySQL client doesn't use the network: it opens a socket file. 127.0.0.1 uses TCP instead. The number at the end of the error says why the socket failed.

TL;DR

Terminal window
sudo systemctl status mysql # Ubuntu / Debian
sudo systemctl status mysqld # RHEL / Rocky / AlmaLinux
sudo systemctl status mariadb # MariaDB
sudo journalctl -u mysql -n 30 --no-pager # why it stopped
sudo ss -xlnp | grep -i mysql # where the socket really is
mysql -h 127.0.0.1 -P 3306 -u root -p # try over TCP instead

1. Is the server running?

Terminal window
sudo systemctl status mysql

The service name depends on the system: mysql on Debian and Ubuntu, mysqld on RHEL-family systems, mariadb for MariaDB. If it’s inactive, start it:

Terminal window
sudo systemctl start mysql

If that fails, the log says why. The general error log is usually in /var/log/mysql/error.log on Ubuntu, or /var/log/mysqld.log on RHEL:

Terminal window
sudo journalctl -u mysql -n 30 --no-pager
sudo tail -n 30 /var/log/mysql/error.log

The usual reasons:

  • The disk is full. MySQL can’t write, so it stops or won’t start: see No space left on device.
  • Not enough memory. The kernel killed it, and the log just ends: see Linux process “Killed”.
  • A bad setting after you edited my.cnf: the log names the line (unknown variable).
  • Port 3306 is taken, by a second MySQL or a container: see Address already in use.
  • Wrong owner on the data folder after a restore: sudo chown -R mysql:mysql /var/lib/mysql.

For the general method, see Job for … failed because the control process exited.

2. The server runs, but the socket is elsewhere

Ask the system where the server’s socket is:

Terminal window
sudo ss -xlnp | grep -i mysql
u_str LISTEN 0 151 /var/lib/mysql/mysql.sock 28411 * 0 users:(("mysqld",pid=1021,fd=24))

The default depends on the distribution: /var/run/mysqld/mysqld.sock on Debian and Ubuntu, /var/lib/mysql/mysql.sock on RHEL. If the client looks in a different place from the server, tell it where, once:

Terminal window
mysql --socket=/var/lib/mysql/mysql.sock -u root -p

or permanently, in the same place for both, in /etc/my.cnf or /etc/mysql/my.cnf:

[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock

Restart the server after changing it. mysql --help | grep -A1 'Default options' lists the config files it reads, and in which order.

3. localhost is not 127.0.0.1

This surprises almost everyone. On Unix, MySQL clients treat the name localhost specially and use the socket. To use TCP, give the address:

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

So if the socket path is the problem, this works right away. It’s also useful as a test: if TCP works and the socket doesn’t, the server is fine and you’re looking at a path or permission problem. If neither works, the server isn’t running, or doesn’t listen where you think. See sudo ss -tlnp | grep 3306, and the Port Lookup entry for 3306.

The same applies to applications: in PHP, Python or Node.js, host: localhost may mean the socket, and host: 127.0.0.1 means TCP.

4. Inside Docker

With MySQL in a container, its socket is inside the container. A client on the host, or in another container, has no such file. Connect over TCP instead:

Terminal window
docker compose exec db mysql -u root -p # from inside the container: the socket works
mysql -h 127.0.0.1 -P 3306 -u root -p # from the host, with the port published

Between containers, use the service name as the host (db), not localhost: see connecting from a container to localhost.

5. (111) or (13)

(111) means the file exists, but no server listens on it. It’s often left behind by a crash. Start the server, which normally replaces it. If it complains, and you’ve made sure no mysqld is running (pgrep -a mysqld), remove the stale socket and its lock file, and start again.

(13) means permissions: check every folder on the way with namei -l /var/run/mysqld/mysqld.sock. On RHEL, a socket in a non-standard folder needs the right SELinux label; on Ubuntu, AppArmor limits where mysqld may write (sudo dmesg | grep -i apparmor).

Shell, we have a problem!

Similar, but different: ERROR 2003 (HY000): Can't connect to MySQL server on '10.0.0.5:3306' (111) is the TCP version: nothing listens on that address, or a firewall is in the way. ERROR 1045 (28000): Access denied means you reached the server and it refused your login: see MySQL ERROR 1045. And ERROR 1040: Too many connections means it’s running and full: see ERROR 1040. The same failure over TCP is ERROR 2003.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles