2002ERRORTier 1 — Safe✅ HIGH confidence

Can't connect to local MySQL server through socket

Category: ConnectionVersions: All MariaDB / MySQL versions

🔴 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

  1. 1MariaDB/MySQL server is not running
  2. 2Socket file path in client configuration does not match the path the server is using
  3. 3Server crashed or was stopped unexpectedly
  4. 4Incorrect socket path specified in my.cnf, .my.cnf, or connection string

How to reproduce

Connecting to localhost when the server is stopped.

trigger — this will ERROR
mysql -u root -p
-- Server is not running
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 the server is not running.

fix
-- 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 the server is running but the socket path is mismatched.

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

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

Unix socketsystemctlsocket file path
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →