MySQL

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

ERROR 2002 (HY000): Can't connect to local MySQL server through socket — the causes (server down, wrong socket path) and the exact fixes.

What it means

ERROR 2002 (HY000): Can't connect to local MySQL server through socket means the MySQL client tried to reach the server through a Unix socket file at a specific path and found nothing there — either the file doesn’t exist, or nothing is listening behind it. Like Postgres’s socket errors, this happens before any authentication: the client and server haven’t even completed a handshake.

Common causes

  • mysqld isn’t running. By far the most common cause — check with ps xa | grep mysqld or your service manager before anything else.
  • The socket file is at a different path than the client’s default. MySQL’s default socket path varies by how it was installed (/tmp/mysql.sock for many source/manual builds, /var/run/mysqld/mysqld.sock on Debian/Ubuntu packages, /var/lib/mysql/mysql.sock on some RPM-based distributions). A client compiled or configured with a different default looks in the wrong place even though the server is up.
  • The socket file was removed by something else, such as a cron job that clears out /tmp on a schedule and deletes mysql.sock along with it.
  • mysqld was started with an explicit non-default --socket=/path/to/file and clients weren’t told to use the same path.
  • Permission issues on the socket file or its parent directory, so the connecting user can’t open it even though it exists.

How to fix it

  1. Confirm the server process is actually running: ps xa | grep mysqld, or ask your service manager (systemctl status mysql, mysqld or mariadb, depending on the package).
  2. If it’s not running, start it and check its error log for why it might be failing to start in the first place.
  3. Find where the server puts the socket: check my.cnf for a socket= line under [mysqld], or ask the running server over TCP, which doesn’t need the socket: mysql -h 127.0.0.1 -u myuser -p -e "SELECT @@socket".
  4. Point the client at that exact path: mysql --socket=/var/run/mysqld/mysqld.sock -u myuser -p, or set the matching option in the client’s own config file.
  5. If a cron job or cleanup script is deleting the socket, exclude mysql.sock from it, or move the socket to a directory that isn’t subject to cleanup.
  6. If you don’t need a local socket at all — for example, connecting to a remote server or a database reached through SSH — connect over TCP instead: mysql --protocol=TCP -h 127.0.0.1 -P 3306 -u myuser -p.

In Tablyne

Tablyne’s connection form connects over TCP (directly, or through an SSH tunnel) rather than a local socket path, so a server reachable only remotely doesn’t require finding or matching any socket file at all.

Skip the socket-path guessing. Download Tablyne or open the MySQL & MariaDB GUI.

Sources

Frequently asked questions

Why is the socket path different on my machine than in the tutorial I'm following? +

The default socket path is a compile-time and packaging decision — commonly /tmp/mysql.sock or /var/run/mysqld/mysqld.sock depending on the distribution. A client with no explicit --socket option uses its own compiled-in default, which may not match where mysqld actually created the file.

Is this the same problem as MySQL refusing my password? +

No. Error 2002 happens before any credentials are checked — the client never even reached a running server. A wrong password produces ERROR 1045 instead, after a successful connection.

Does restarting MySQL fix a missing socket file every time? +

If mysqld starts successfully, it recreates the socket file at whatever path its configuration specifies. If it's missing after a restart, mysqld likely failed to start — check its error log rather than retrying the connection.

Related errors