What the message looks like
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)Common causes
- The database service is not running: stopped, crashed, or killed by the OOM killer.
- The server did not start because the disk is full or the data directory is damaged or has wrong permissions.
- Client and server use different socket paths, e.g.
/run/mysqld/mysqld.sockand/var/lib/mysql/mysql.sock. - The application connects to
localhost, which means the socket, while the server only listens on TCP. - A second instance or a container uses another socket.
Check
- Is the service running?
systemctl status mariadb - Why did it stop? Read the latest log lines
- Does the socket exist?
ls -l /run/mysqld/ - Is there free space for the data directory?
df -h /var/lib/mysql
Fix
- Start the service and read why it stopped:
systemctl start mariadb. On MySQL the unit is calledmysql. - Free disk space or repair permissions on
/var/lib/mysqlif that was the reason. - If the OOM killer stopped it, reduce memory use (
innodb_buffer_pool_size) or add RAM. - Use the same socket path in the
[client]and[mysqld]sections of the configuration. - In the application, use
127.0.0.1and port 3306 instead oflocalhostto connect over TCP.
FAQ
What is the difference between localhost and 127.0.0.1 here?
With localhost the MySQL client uses the Unix socket file. With 127.0.0.1 it uses TCP. If the socket is missing but TCP works, that explains this error.
Which error do I get when the password is wrong?
That is ERROR 1045. Error 2002 comes before: the client does not even reach the server.