MySQL & MariaDB error 2002: Can't connect to local MySQL server through socket
Can't connect to local MySQL server through socket '/tmp/no-such-mysql.sock' (2) (5.6.51) · Can't connect to local server through socket '/tmp/no-such-mysql.sock' (2) (10.6.28) The client tried to connect through a Unix socket file and nothing answered. Either the server isn't running, or the client and server disagree on where the socket file is.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | CR_CONNECTION_ERROR | CR_CONNECTION_ERROR |
| SQLSTATE | HY000 | HY000 |
| Type | Client library error | Client library error |
| Versions | every release since 5.0 active | since 10.2.44 active |
What it means
On Linux and macOS, mysql -h localhost (and most drivers given localhost) connect through a Unix socket file such as /var/run/mysqld/mysqld.sock rather than TCP. Error 2002 is raised by the client library before any server is involved: the socket file isn’t there, or no process is listening on it. The number in brackets at the end is the operating-system error: (2) no such file, (13) permission denied, (111) connection refused.
Why it happens
- The server is not running, or it crashed (check
systemctl status mysql/mariadband the error log). - The client looks for the socket in a different place from where the server creates it: different distributions, Homebrew vs packages, or a custom
socket=in onemy.cnfbut not the other. - The client runs in a container or chroot that can’t see the host’s socket file.
- Permissions on the socket directory (
(13)).
How to fix it
- Make sure the server is running:
sudo systemctl start mysql(ormariadb), then check the error log if it stops again. - Find the server’s socket path, from a working session (
SELECT @@socket;) or from its config (mysqld --verbose --help | grep ^socket). - Point the client at it:
mysql --socket=/path/to/mysqld.sock, or setsocket=in the[client]section ofmy.cnf. - Or skip the socket and use TCP:
mysql -h 127.0.0.1 -P 3306. An error then becomes 2003 if the server isn’t listening on that port.
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database:CREATE DATABASE demo; USE demo;In a shell on the database host:
mysql -uroot -p --protocol=socket --socket=/tmp/no-such-mysql.sock -e 'SELECT 1'
You get: ERROR 2002 (HY000): Can't connect to local server through socket '/tmp/no-such-mysql.sock' (2)(11.8.9)
Verified fixes
Ask the server where its socket is and use that path (or connect over TCP with -h 127.0.0.1)
From any working connection (or the server's my.cnf), find the socket path and use it:
SELECT @@socket;Then connect through that socket, or over TCP instead:
mysql -u root -p --socket=/var/run/mysqld/mysqld.sock mysql -u root -p -h 127.0.0.1 -P 3306
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| Connect through a socket file that doesn't exist (server stopped, or a different socket path) repro | ✓ 2002 | ✓ 2002 | ✓ 2002 | ✓ 2002 | ✓ 2002 | ✓ 2002 | ✓ 2002 | ✓ 2002 | ✓ 2002 |
| Ask the server where its socket is and use that path (or connect over TCP with -h 127.0.0.1) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Related errors
How the message changed between versions
| MySQL from | Symbol | Message |
|---|---|---|
| 5.0.96 | CR_CONNECTION_ERROR | Keine Verbindung zu lokalem MySQL Server, socket: '%-.100s' (%d) |
| 5.5.63 | CR_CONNECTION_ERROR | Can't connect to local MySQL server through socket '%-.100s' (%d) |