mysqldude
Error codes › 2002

MySQL & MariaDB error 2002: Can't connect to local MySQL server through socket

MySQLCan't connect to local MySQL server through socket '…' (…)
MariaDBCan't connect to local server through socket '…' (…)
Example from our test runs: 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.

MySQLMariaDB
SymbolCR_CONNECTION_ERRORCR_CONNECTION_ERROR
SQLSTATEHY000HY000
TypeClient library errorClient library error
Versionsevery release since 5.0 activesince 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 / mariadb and 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 one my.cnf but 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

  1. Make sure the server is running: sudo systemctl start mysql (or mariadb), then check the error log if it stops again.
  2. Find the server’s socket path, from a working session (SELECT @@socket;) or from its config (mysqld --verbose --help | grep ^socket).
  3. Point the client at it: mysql --socket=/path/to/mysqld.sock, or set socket= in the [client] section of my.cnf.
  4. 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.

  1. Connect with mysql -u root -p and create a scratch database:

    CREATE DATABASE demo;
    USE demo;
  2. 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

  1. 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

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.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
✓✓✓✓✓✓✓✓✓
Connect through a socket file that doesn't exist (server stopped, or a different socket path) repro
MySQL 5.6✓ 20025.7✓ 20028.0✓ 20028.4✓ 20029✓ 2002
MariaDB 10.6✓ 200210.11✓ 200211.4✓ 200211.8✓ 2002
Ask the server where its socket is and use that path (or connect over TCP with -h 127.0.0.1) fix
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

Related errors

  • 2003 CR_CONN_HOST_ERRORexplained
  • 2013 CR_SERVER_LOSTexplained
  • 1045 ER_ACCESS_DENIED_ERRORexplained
How the message changed between versions
MySQL fromSymbolMessage
5.0.96CR_CONNECTION_ERRORKeine Verbindung zu lokalem MySQL Server, socket: '%-.100s' (%d)
5.5.63CR_CONNECTION_ERRORCan't connect to local MySQL server through socket '%-.100s' (%d)