ERROR 2002: Can't connect to local MySQL server through socket
MySQL ERROR 2002 means the server is down or the client looks for the socket in the wrong place. Read the errno, find @@socket, and fix the client path.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)The client tried to connect through a Unix socket file and found nothing at that path. Either the server is not running, or it created its socket somewhere else. If the server is up, read the real path from @@socket and point the client at it.
With no -h, or with -h localhost, the MySQL client uses the socket file instead of TCP (Troubleshooting Problems Connecting to MySQL). That is why "localhost" ends up as a socket error.
[1] Read the number in parentheses at the end. It is the operating system errno. Two values showed up in testing:
| Errno | Meaning | Go to |
|---|---|---|
(2) | No file at that path | [2]–[5] |
(13) | No permission on the socket or its directory | [6] |
[2] Check that the server is running. The official docs start with ps xa | grep mysqld. If there is no process, start the server; if it will not start, read the server error log. With no server, mysqladmin spells it out:
$ mysqladmin -uroot -p'<password>' pingmysqladmin: [Warning] Using a password on the command line interface can be insecure.mysqladmin: connect to server at 'localhost' failederror: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!Switching to TCP with no server only changes the error to 2003; (111) is "connection refused".
$ mysql -uroot -p'<password>' -h127.0.0.1 -e 'SELECT 1'mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)[3] If the server is running and you still get 2002, the socket path does not match. Below, the server was started with --socket=/tmp/mysql.sock. The directory the client expects holds only mysqlx.sock (the X Protocol socket), not mysqld.sock.
$ mysql -uroot -p'<password>' -e 'SELECT 1'mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)$ ls -l /var/run/mysqld/total 8-rw-r----- 1 mysql mysql 2 Oct 27 07:59 mysqld.pidsrwxrwxrwx 1 mysql mysql 0 Oct 27 07:59 mysqlx.sock-rw------- 1 mysql mysql 2 Oct 27 07:59 mysqlx.sock.lock[4] Find the socket the server actually created. If TCP is open, connect with -h127.0.0.1 and ask for @@socket. If TCP is closed too, look for listening socket names in /proc/net/unix (the docs use netstat -ln | grep mysql).
$ mysql -uroot -p'<password>' -h127.0.0.1 -e 'SELECT @@socket, @@port'mysql: [Warning] Using a password on the command line interface can be insecure.@@socket @@port/tmp/mysql.sock 3306$ grep mysql /proc/net/unix0000000000000000: 00000002 00000000 00010000 0001 01 1458954 /tmp/mysql.sock0000000000000000: 00000002 00000000 00010000 0001 01 1457910 /var/run/mysqld/mysqlx.sockDefault paths differ by package. The official Docker image and the Ubuntu package use /var/run/mysqld/mysqld.sock; Oracle's RPM packages use /var/lib/mysql/mysql.sock (RPM installation layout). Mixing a client and server from different packages, or editing only one config file, causes the mismatch.
[5] Tell the client where the socket is: --socket for a one-off, the [client] section of an option file to make it stick.
$ mysql -uroot -p'<password>' --socket=/tmp/mysql.sock -e 'SELECT CURRENT_USER()'mysql: [Warning] Using a password on the command line interface can be insecure.CURRENT_USER()root@localhost$ cat ~/.my.cnf[client]socket=/tmp/mysql.sock$ mysql -uroot -p'<password>' -e 'SELECT @@socket'mysql: [Warning] Using a password on the command line interface can be insecure.@@socket/tmp/mysql.sockYou can also move the server's socket instead. The docs say to put the same socket= under both [mysqld] and [client] (How to Protect or Change the MySQL Unix Socket File); change only one and this error comes back. A server-side change needs a restart, so plan for connected apps dropping.
[6] Errno (13) is a permission problem. Below, the socket directory was set to 700 and the client ran as nobody:
$ ls -ld /var/run/mysqlddrwx------ 1 mysql mysql 110 Oct 27 07:59 /var/run/mysqld$ iduid=65534(nobody) gid=65534(nobody) groups=65534(nobody)$ mysql -uroot -p'<password>' -e 'SELECT 1'mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (13)Restoring the directory mode (1777, this image's default) let the same user in. The docs also say to check that clients can access the socket file and its directory. Defaults differ by package, so compare with a stock server of the same version before you widen permissions.
[7] Connecting from another container or from the host, use TCP, not the socket. A sidecar that shares the network namespace still has its own filesystem, so localhost looks for a socket that is not there.
$ mysql -uroot -p'<password>' -e 'SELECT 1'mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)$ mysql -uroot -p'<password>' -h127.0.0.1 -e 'SELECT CURRENT_USER()'mysql: [Warning] Using a password on the command line interface can be insecure.CURRENT_USER()root@%$ mysql -uroot -p'<password>' -h localhost --protocol=TCP -e 'SELECT 1 AS ok'mysql: [Warning] Using a password on the command line interface can be insecure.ok1Note that CURRENT_USER() changed from root@localhost to root@%: over TCP, MySQL matches a different account row. If that row is missing or has another password, you get error 1045 next — see MySQL ERROR 1045 Access denied (using password: YES). If only root fails with 1698 on Ubuntu, see MySQL ERROR 1698 Access denied for root.
Analyze your MySQL slow log log in the browser ↗ — nothing is uploaded.