What the Error Says
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
MariaDB's wording is similar. The client connects to localhost through a Unix socket, a special file the server creates when it starts. The path in quotes is where the client looked. The number at the end is the system error: (2) means the file does not exist, and (111) means the file exists but no server answers on it.
Step 1: Is the Server Running?
| Server | Command |
|---|---|
| MySQL on Ubuntu | sudo systemctl status mysql |
| MySQL on Rocky, Alma | sudo systemctl status mysqld |
| MariaDB | sudo systemctl status mariadb |
If it is inactive or failed, start it with sudo systemctl start and the same name, and check the status again. If it fails at once, the reason is in the log, not in the client. Service states are explained in how to list services with systemctl.
Step 2: Read the Error Log
- MySQL on Ubuntu:
/var/log/mysql/error.log. - MySQL on AlmaLinux and Rocky Linux:
/var/log/mysql/mysqld.log. - MariaDB on Debian and Ubuntu: the systemd journal; read it with
sudo journalctl -u mariadb. - MariaDB on AlmaLinux and Rocky Linux:
/var/log/mariadb/mariadb.log.
sudo tail -n 50 /var/log/mysql/error.log
Look at the last lines before the shutdown. More about following logs is in journalctl.
Common Start-Up Failures
- Disk full: the server cannot write its files. Check with
df -h /var/lib/mysqland free space elsewhere, as in how to check disk space in Linux. - Out of memory: the buffer pool does not fit, or the kernel killed the server; see OOM killer.
- Configuration error: an unknown option after an edit. On MySQL 8.0.16 and later,
sudo mysqld --validate-configchecks the configuration without starting the server. - Port in use: another server already listens on 3306.
Never delete files from /var/lib/mysql to fix a start-up failure. Redo logs (ib_logfile0, or the #innodb_redo folder on current MySQL), ibdata1, and table files hold data the server needs for recovery; removing them can destroy databases. If the log points to corruption, take a snapshot of the server before you try anything.
Step 3: Compare the Socket Paths
If the server runs but the client still fails, the two use different paths. See where the server's socket is:
sudo ss -lx | grep mysql
Usual locations are /run/mysqld/mysqld.sock on Debian and Ubuntu (/var/run points to /run) and /var/lib/mysql/mysql.sock on AlmaLinux and Rocky Linux. Then see which options the client reads from the configuration files:
mysql --print-defaults
If a --socket option appears, that is where the client looks. The socket= setting in the [mysqld] section sets the server's path; the one in [client] sets where clients look. If they differ, make them the same and restart the server. Applications have their own setting, for example a socket path in the PHP configuration or the database host in the application's settings.
Step 4: Work Around It
mysql -h 127.0.0.1 -u app_user -p
mysql --socket=/var/lib/mysql/mysql.sock -u app_user -p
The first uses TCP instead of the socket; it works only if the server listens on 127.0.0.1 and the account matches that host. The second names the path directly. Both help you confirm the cause; fixing the configuration is the real solution.
Permissions on the Socket Folder
The server runs as the mysql user and must be able to write the socket folder. If the log says it cannot create the socket file, check that /run/mysqld exists and belongs to mysql.