MySQL ERROR 2002 (HY000): Can't connect to local MySQL server through socket
The client couldn’t reach the MySQL server — it isn’t running, or the client is looking for the socket/port in the wrong place.
Meaning
With localhost, MySQL clients use a Unix socket file (e.g. /var/run/mysqld/mysqld.sock). If the server is down or the socket lives elsewhere, you get 2002. PDO shows it as SQLSTATE[HY000] [2002] No such file or directory or Connection refused.
Common causes
- MySQL service stopped or crashed
- Socket path differs between client and server
localhostused in Docker where the DB is another container- Server listening only on a different port/interface
- Disk full preventing startup
⚡ Quick fix
sudo systemctl status mysql(or mariadb) and start it- Use
127.0.0.1to force TCP, or set the correct socket path - In Docker, use the DB service name as host
- Check the MySQL error log for startup failures
Detailed fix by platform
Linux
- Check service and socket:bash
sudo systemctl status mysql sudo journalctl -u mysql -n 50 mysql -h 127.0.0.1 -u root -p # TCP instead of socket
PHP
- Set
pdo_mysql.default_socket/mysqli.default_socketin php.ini, or usehost=127.0.0.1in the DSN.
Docker
DB_HOST=db(service name), not localhost.
How to diagnose
- Service — Is mysqld running?
- Transport — Socket or TCP? Which path/port?
- Logs — Why did it stop?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026