| title | MySQL Can't Connect Through Socket | |||||
|---|---|---|---|---|---|---|
| slug | mysql-cant-connect-through-socket | |||||
| technologies |
|
|||||
| severity | high | |||||
| tags |
|
|||||
| related |
|
|||||
| last_reviewed | 2026-06-27 |
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)
ERROR 2002 (HY000) is a client-side error: the client tried to reach the
server over a Unix domain socket and failed. When the client host is localhost,
MySQL connects via the socket file rather than TCP, so this error appears even
when the server's network port is fine. The trailing OS errno tells you why:
(2) is ENOENT (the socket file does not exist β server is down or writes its
socket elsewhere); (111) is ECONNREFUSED (a stale socket exists but nothing
is listening).
- mysql (client connector / Unix socket)
high β no local client (including app processes on the DB host and admin tooling) can connect. If the server itself is down, it is a full database outage.
- The MySQL server is not running (crashed, failed to start, or stopped).
- The client and server disagree on the socket path β the client looks in
/tmp/mysql.sockwhile the server created/var/run/mysqld/mysqld.sock. - The server failed to start (config error, disk full, corrupt InnoDB) and so never created the socket file.
- Permissions on the socket file or its directory block the client user.
- A stale socket file left after an unclean shutdown (errno 111).
On startup the server creates the socket file at the path in its socket
variable. A localhost client reads its own socket setting (from
/etc/mysql/my.cnf, ~/.my.cnf, or the compiled default) and connect()s to
that path. If the server is down the file is absent (errno 2). If the file path
differs between client and server, the client looks at the wrong place (errno 2).
If the file exists but the server died without cleaning up, the connect is
refused (errno 111). Distinguishing "server down" from "wrong path" is the key
diagnostic step.
# Is the server process even up?
sudo systemctl status mysql
# Where does the SERVER put its socket (and is it listening on TCP)?
mysql --help 2>/dev/null | grep -A1 "Default options" # shows config files read
ss -lnp | grep -E "mysqld|3306"
# Does the socket file actually exist, and with what permissions?
ls -l /var/run/mysqld/mysqld.sock /tmp/mysql.sock 2>&1
# Why did startup fail (disk, config, InnoDB)?
sudo journalctl -u mysql --since "15 min ago" | tail -n 40β mysql.service - MySQL Community Server
Active: failed (Result: exit-code)
...
ls: cannot access '/var/run/mysqld/mysqld.sock': No such file or directory
A failed/inactive service with no socket file means the server is down (fix
startup). An active (running) service plus a socket file at a different path
than the client expects means a path mismatch (fix the client config).
-
If the server is down, read the journal to find why (disk full, config typo, InnoDB corruption) and start it:
sudo systemctl start mysql && sudo systemctl status mysql -
If the server is up but paths differ, point the client at the real socket β either per command or in config:
mysql --socket=/var/run/mysqld/mysqld.sock -u app -p
# /etc/mysql/my.cnf β keep [client] and [mysqld] socket consistent [client] socket = /var/run/mysqld/mysqld.sock
-
Remove a stale socket only if the server is confirmed stopped, then restart.
-
Fix permissions so the client user can access the socket directory (
/var/run/mysqldshould be owned bymysql). -
To bypass the socket entirely, connect over TCP:
mysql -h 127.0.0.1 -P 3306.
mysqladmin -u root -p status
# Expect "Uptime: ... Threads: ..." rather than ERROR 2002.- Set the same
socketpath in[client]and[mysqld]and template it. - Monitor the systemd unit and alert on
failed/inactive. - Catch startup failures (disk, config) in pre-deploy checks.
- Prefer
127.0.0.1TCP connections from co-located apps to avoid path drift.
mysql Β· connectivity Β· socket Β· startup Β· production