Skip to content
Log In Sign Up info@muvcloud.com

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?

ServerCommand
MySQL on Ubuntusudo systemctl status mysql
MySQL on Rocky, Almasudo systemctl status mysqld
MariaDBsudo 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/mysql and 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-config checks 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.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations