MySQL 2002: Can't connect to local server through socket
The client and server disagree on the socket path, or localhost resolved to a socket in another container. Read the errno and force TCP when needed.

Introduction & Quick Summary
The client cannot reach a server that is running fine:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)Quick answer: the client and the server disagree about where the Unix socket lives, or the server is not listening there. Find the socket the server actually created and point one side at the other — or bypass the socket and use TCP.
# what the running server uses
sudo mysql -e "SHOW VARIABLES LIKE 'socket';"
# what the client looks for
mysql --help | grep -A1 "Default options" ; mysql --print-defaults
# connect over TCP instead
mysql -h 127.0.0.1 -P 3306 -u app -p --protocol=TCPThe number in parentheses is the system error, and it narrows the problem far more than the message does:
(2)—ENOENT, no file at that path. The server is down, the path is different, or the directory was removed.(13)—EACCES, the file exists and the client's user cannot open it.(111)—ECONNREFUSED, a TCP connection reached the host and nothing listened on the port.
A socket error usually means "wrong path or not running", rarely "wrong password". Authentication errors arrive later, as ERROR 1045 (28000): Access denied for user.
Core Mechanics, Signatures & Reproducible Setup
The MySQL client picks a transport from the host argument. localhost means the Unix socket; 127.0.0.1 means TCP. That single rule explains most of these reports.
mysql -u app -p # socket, localhost implied
mysql -h localhost -u app -p # still the socket
mysql -h 127.0.0.1 -u app -p # TCP to loopback
mysql --socket=/tmp/mysql.sock -u app -p| Transport | Chosen when | Typical error when broken | Port or file |
|---|---|---|---|
| Unix socket | host omitted, localhost, --socket | 2002 with errno 2 or 13 | /var/run/mysqld/mysqld.sock |
| TCP over loopback | host 127.0.0.1 or --protocol=TCP | 2002 with errno 111 | 3306 |
| TCP over network | host name or IP | 2003 (HY000), not 2002 | 3306 |
| Named pipe | Windows --pipe | 2002 with pipe name | MySQL |
Note the split: 2002 is "cannot connect through the local socket", 2003 is "cannot connect to the server over TCP". A wrong hostname or a closed firewall produces 2003, and searching for the wrong number sends you to the wrong fixes.
Key Comparison Table
| Cause | What you see | Confirm with | Fix |
|---|---|---|---|
| Server stopped | errno 2, no process | systemctl status mysql | start the service |
| Socket path mismatch | errno 2, server running | SHOW VARIABLES LIKE 'socket' | --socket= or edit my.cnf |
| Socket directory missing | errno 2 after reboot | ls -ld /var/run/mysqld | create with the right owner |
| Permission denied on socket | errno 13 | ls -l on the socket | add user to mysql group |
| Client reads another config | path differs from --print-defaults | mysql --print-defaults | fix ~/.my.cnf or the option file |
| Server on a container | no host socket | docker ps, docker exec | publish 3306 or exec inside |
| App treats localhost as TCP | connection refused | application error text | use 127.0.0.1 in the app |
Client options that decide the transport:
| Setting | Effect | Common mistake |
|---|---|---|
host in app config | localhost means socket for many drivers | assuming it means TCP |
unix_socket or socket | explicit path | left from a different distro |
protocol | forces TCP or SOCKET | omitted while debugging |
port | ignored for socket connections | changing it to fix 2002 |
bind-address on server | limits TCP interfaces | set to 127.0.0.1 and expecting remote access |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Ask the server where its socket is
sudo ss -xlp | grep mysql
ps -o args= -C mysqld
sudo grep -r "socket" /etc/mysql/ /etc/my.cnf 2>/dev/nullss -xl lists Unix sockets with the owning process, which settles the path immediately. If the server runs under systemd with a custom socket, the unit file shows it:
systemctl cat mysql | grep -iE "ExecStart|socket"Then either pass --socket to the client or make the difference permanent in a client section of the option file:
[client]
socket = /run/mysqld/mysqld.sock2. localhost versus 127.0.0.1 inside applications
This is the same rule with a different surface. PHP's PDO with host=localhost uses the socket and ignores port; host=127.0.0.1 opens TCP. Python's mysqlclient and PyMySQL behave the same way when host is empty or localhost. A Dockerized app therefore fails with 2002 even with the right password, because the socket exists only inside the database container.
services:
db:
image: mysql:8.4
environment:
MYSQL_ROOT_PASSWORD: root
app:
environment:
DB_HOST: db # a hostname, so TCP; localhost would mean "socket inside this container"
DB_PORT: "3306"Set the connection host to the service name, and keep 127.0.0.1 for cases where both processes run on one machine but you want TCP. Compare environment files across environments with the env formatter when staging works and production does not.
3. The server is not running, or died at boot
systemctl status mysql
sudo journalctl -u mysql --since "10 min ago"
sudo tail -50 /var/log/mysql/error.log
df -h /var/lib/mysqlA server that fails at boot usually leaves a specific line in the error log: InnoDB: Unable to lock ./ibdata1, No space left on device, Can't open and lock privilege tables, or an AppArmor denial for a non-standard data directory. Fixing the socket path will not help when the process never started.
4. Directory and permission problems after a reboot
/var/run is often a tmpfs, so /var/run/mysqld disappears on reboot while the unit expects it:
sudo install -d -o mysql -g mysql -m 755 /var/run/mysqld
sudo systemctl restart mysqlDistributions ship a tmpfiles.d entry or RuntimeDirectory=mysqld in the unit to recreate it. When (13) appears instead of (2), the socket exists but the client user cannot open it; adding the user to the mysql group is the usual fix, and a permissive chmod 777 is not.
5. Containers, volumes and ephemeral filesystems
A container that publishes 3306 is reachable over TCP only; the host has no socket for it. Inside the container the socket exists at the image's default path. Three correct ways to reach it:
docker exec -it db mysql -uroot -p # socket inside the container
docker run --rm -it --network host mysql:8.4 mysql -h 127.0.0.1 -uroot -p # TCP
docker compose exec db mysql -uroot -pMount the data directory on a named volume, not on a path that disappears with the container; otherwise the server starts with an empty data directory and the previous users are gone, which reads as an authentication problem after the socket is fixed.
6. Non-default paths: Homebrew, custom builds and AppArmor
On macOS with Homebrew the socket is /tmp/mysql.sock, which many tutorials still write as /var/run/mysqld/mysqld.sock. On Linux, a custom datadir plus AppArmor produces (13) even for root, with the denial visible in dmesg:
sudo dmesg | grep -i apparmor | tail -5
sudo aa-status | grep -i mysqlEither keep the data directory inside the profile's allowed paths or add a local override. When you must inspect the resulting data, exporting through the SQL formatter or a dump is safer than copying files while the server runs.
Common Compilation/Runtime Mistakes & Version Gotchas
Changing the port to fix 2002. The port is ignored for socket connections. Change the socket path or force TCP.
Creating the socket file by hand. The client connects to a listening Unix socket, not to a regular file. Only the server creates it.
Assuming the service name. mysql, mysqld, mariadb and mysql@5.7 are different units; check which one is enabled.
Restarting before reading the error log. A restart loop hides the first fatal line. Read the log, then restart.
Using skip-networking and expecting remote clients. It disables TCP entirely, so only the local socket works by design.
Version notes. MySQL 8.0 made caching_sha2_password the default plugin, and 8.4 disables mysql_native_password unless it is enabled explicitly, so a client upgrade can surface Plugin 'mysql_native_password' is not loaded right after the socket problem is solved. Public Key Retrieval is not allowed is the other common follow-on error. MariaDB uses the same socket conventions but its own error text, and Ubuntu's MariaDB package reports /run/mysqld/mysqld.sock too. On systemd systems the socket may be created by mysql.socket unit, so systemctl status mysql.socket is worth a look when mysql.service is active but the path is absent.
Reference material: Troubleshooting connection issues and Socket, which document the socket behaviour MySQL shares.
Official references
primary documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.
PostgreSQL: canceling statement due to statement timeout
The statement was canceled by statement_timeout. Find whether the role, the pooler or the client set it, then scope a longer budget to that job.
pg_dump: server version mismatch, aborting because of server version
pg_dump refuses to read a newer server. Install a matching client, run it from a container or the server host, and match pg_restore too.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.