2002
MySQLERRORCommonConnectionHIGH confidence

Can't connect to local MySQL server through socket

Production Risk

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
trigger — this will error
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.

Start the MariaDB/MySQL service
-- 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.

Verify and match socket file path
-- 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.

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors