Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readSQL
#mysql#connections#sockets#docker
Bold white headline reading ERROR 2002 with a MySQL client crate reaching an amber missing socket file beside a cyan server cube.

Introduction & Quick Summary

The client cannot reach a server that is running fine:

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

bash
# 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=TCP

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

bash
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
TransportChosen whenTypical error when brokenPort or file
Unix sockethost omitted, localhost, --socket2002 with errno 2 or 13/var/run/mysqld/mysqld.sock
TCP over loopbackhost 127.0.0.1 or --protocol=TCP2002 with errno 1113306
TCP over networkhost name or IP2003 (HY000), not 20023306
Named pipeWindows --pipe2002 with pipe nameMySQL

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

CauseWhat you seeConfirm withFix
Server stoppederrno 2, no processsystemctl status mysqlstart the service
Socket path mismatcherrno 2, server runningSHOW VARIABLES LIKE 'socket'--socket= or edit my.cnf
Socket directory missingerrno 2 after rebootls -ld /var/run/mysqldcreate with the right owner
Permission denied on socketerrno 13ls -l on the socketadd user to mysql group
Client reads another configpath differs from --print-defaultsmysql --print-defaultsfix ~/.my.cnf or the option file
Server on a containerno host socketdocker ps, docker execpublish 3306 or exec inside
App treats localhost as TCPconnection refusedapplication error textuse 127.0.0.1 in the app

Client options that decide the transport:

SettingEffectCommon mistake
host in app configlocalhost means socket for many driversassuming it means TCP
unix_socket or socketexplicit pathleft from a different distro
protocolforces TCP or SOCKETomitted while debugging
portignored for socket connectionschanging it to fix 2002
bind-address on serverlimits TCP interfacesset 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

bash
sudo ss -xlp | grep mysql ps -o args= -C mysqld sudo grep -r "socket" /etc/mysql/ /etc/my.cnf 2>/dev/null

ss -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:

bash
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:

ini
[client] socket = /run/mysqld/mysqld.sock

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

bash
systemctl status mysql sudo journalctl -u mysql --since "10 min ago" sudo tail -50 /var/log/mysql/error.log df -h /var/lib/mysql

A 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:

bash
sudo install -d -o mysql -g mysql -m 755 /var/run/mysqld sudo systemctl restart mysql

Distributions 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 -p

Mount 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:

bash
sudo dmesg | grep -i apparmor | tail -5 sudo aa-status | grep -i mysql

Either 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 documentation

The SQL documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.

All SQL guides

More in SQL

All SQL guides

The guides filed next to this one in SQL, in library order.