// MySQL error reference

MySQL error 2002: Can't connect through socket

The client tried the local Unix socket and found no server there. The server isn't running, uses another socket path, or you meant a TCP connection.

Error number
2002
Name
CR_CONNECTION_ERROR
Category
Connection and client
Error message
Can't connect to local MySQL server through socket '%s' (%d)

What it means

When you connect to localhost on Unix, MySQL clients use a Unix socket file instead of TCP. 2002 means the socket file doesn't exist or nothing is listening on it. The number in brackets is the operating-system error, such as (2) for no such file and (111) for connection refused.

This is a client error: the request never reached a server.

Common causes

  • The MySQL server isn't running, or it crashed at startup.
  • The server's socket is in a different place than the client expects (for example /var/run/mysqld/mysqld.sock versus /tmp/mysql.sock).
  • The server runs in Docker or on another machine, so there is no local socket; you need TCP.
  • Permissions on the socket directory stop the client from reaching it.

How to fix it

  1. Check that the server is running and read its error log if it isn't, for example with systemctl status mysql on Linux or brew services list on macOS.

  2. Connect over TCP instead of the socket by using 127.0.0.1 rather than localhost, or --protocol=TCP.

    mysql -h 127.0.0.1 -P 3306 -u app -p
  3. If the server runs locally, find its socket path and point the client at it.

    -- On the server:
    SHOW VARIABLES LIKE 'socket';
    -- Client:
    mysql --socket=/var/run/mysqld/mysqld.sock -u app -p

Related errors

More connection and client errors

The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.

// fewer errors, faster fixes

Work on MySQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.