2002ERRORTier 1 — Safe✅ HIGH confidenceCan't connect to local MySQL server through socket
🔴 Production Risk Error
HIGH — complete service outage.
What this means
Client error 2002 is returned by the MySQL/MariaDB client library when connecting to localhost (which defaults to a Unix socket connection) and the socket file does not exist or the server is not listening on it. This almost always means the server is not running.
Why it happens
- 1MariaDB/MySQL server is not running
- 2Socket file path in client configuration does not match the path the server is using
- 3Server crashed or was stopped unexpectedly
- 4Incorrect socket path specified in my.cnf, .my.cnf, or connection string
How to reproduce
Connecting to localhost when the server is stopped.
mysql -u root -p
-- Server is not runningFix 1: Start the MariaDB/MySQL service
When the server is not running.
-- On systemd systems:
sudo systemctl start mariadb
sudo systemctl status mariadb
-- On older init systems:
sudo service mysql startWhy this works
Starting the service creates the socket file and begins accepting connections.
Fix 2: Verify and match socket file path
When the server is running but the socket path is mismatched.
-- Find where the server expects the socket:
sudo grep -r 'socket' /etc/mysql/ /etc/my.cnf* 2>/dev/null
-- Connect specifying the socket path explicitly:
mysql -u root -p --socket=/var/lib/mysql/mysql.sockWhy this works
The client and server must use the same socket file path. Check both server my.cnf and client configuration.
Sources
📚 Official docs: https://mariadb.com/kb/en/starting-and-stopping-mariadb-automatically/
🔧 Source ref: MySQL Client error 2002 / CR_CONNECTION_ERROR
📖 Further reading: MariaDB Starting and Stopping
📖 Further reading: MariaDB Troubleshooting Connection Issues
Confidence assessment
✅ HIGH confidence
Stable.
See also
📄 Reference pages