Solution to error 2002 (HY000) in MySQL login startup

Error information

Login database error:`

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/ mysql.sock ‘ (2 “No such file or directory”)`


Service restart error: job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service " and "journalctl -xe" for details.


View service status information: ` systemctl status mariadb.service

● mariadb.service – MariaDB 10.1 database server
Loaded: loaded (/usr/lib/systemd/system/ mariadb.service ; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2019-04-15 17:16:38 UTC; 4s ago
Process: 2864 ExecStartPre=/usr/libexec/mysql-prepare-db-dir %n (code=exited, status=1/FAILURE)
Process: 2841 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)

Apr 15 17:16:37 xiandian systemd[1]: Starting MariaDB 10.1 database server…
Apr 15 17:16:38 xiandian mysql-prepare-db-dir[2864]: Database MariaDB is not initialized, but the directory /var/lib/mysql is not empty…e done.
Apr 15 17:16:38 xiandian mysql-prepare-db-dir[2864]: Make sure the /var/lib/mysql is empty before running mysql-prepare-db-dir.
Apr 15 17:16:38 xiandian systemd[1]: mariadb.service : control process exited, code=exited status=1
Apr 15 17:16:38 xiandian systemd[1]: Failed to start MariaDB 10.1 database server.
Apr 15 17:16:38 xiandian systemd[1]: Unit mariadb.service entered failed state.
Apr 15 17:16:38 xiandian systemd[1]: mariadb.service failed.
Hint: Some lines were ellipsized, use -l to show in full.`

The error message of restart service log: ` APR 15 17:16:21 Xiandian MySQL prepare DB dir: database MariaDB is not initialized, but the directory/var/lib/MySQL is not empty, so initialization cannot be done

Apr 15 17:16:21 mysql-prepare-db-dir: Make sure the /var/lib/mysql is empty before running mysql-prepare-db-dir.
Apr 15 17:16:21 systemd: mariadb.service : control process exited, code=exited status=1
Apr 15 17:16:21 systemd: Failed to start MariaDB 10.1 database server.
Apr 15 17:16:21 systemd: Unit mariadb.service entered failed state.
Apr 15 17:16:21 systemd: mariadb.service failed.`


You can see that the error message indicates that the database is not initialized, but/var/lib/MySQL is not empty, so it cannot be initialized.


Take a look at the directory permissions

 ll /var/lib/
 d---------. 11 mysql      mysql      4096 May  4  2018 mysql

The MySQL permission is 0, which causes the startup file sock file cannot be written in
Restart, give the directory permission 755
Chmod 755/var/lib/MySQL/
and then restart the MySQL service
systemctl restart mariadb.service
PS - e | grep - I MySQL
3191?00:00:02 mysqld
starts normally

Reproduced in: https://blog.51cto.com/9103824/2378808

Read More: