Can't connect to local MySQL server through socket
Production Risk
HIGH — complete service outage.
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.
- 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
Connecting to localhost when the server is stopped.
mysql -u root -p -- Server is not running
expected output
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Fix 1
Start the MariaDB/MySQL service
WHEN When the server is not running.
-- On systemd systems: sudo systemctl start mariadb sudo systemctl status mariadb -- On older init systems: sudo service mysql start
Why this works
Starting the service creates the socket file and begins accepting connections.
Fix 2
Verify and match socket file path
WHEN 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.sock
Why this works
The client and server must use the same socket file path. Check both server my.cnf and client configuration.
MySQL Client error 2002 / CR_CONNECTION_ERROR
MariaDB Starting and Stopping ↗MariaDB Troubleshooting Connection Issues ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev