Tag Archives: Can’t connect to local MySQL server ERROR

ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’

The binary installation mysql reports the following error

[root@tzPC sdb1]# mysql -v
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

It is found that /tmp/mysql.sock does not exist. The reason for this is generally due to inconsistent configuration files. If mysqld is started by mistake, mysqld_safe will clear mysql.sock once.

Solution

First check whether the current process has a surviving mysql, kill it, and then restart the database

$ ps -ef | grep mysql
$ kill -9 xxxx
$ /etc/init.d/mysqld start

If it still cannot be resolved, check whether the configuration file is correct

$ cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/mnt/sdb1/mysql
datadir=/mnt/sdb1/mysql_data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock

Use the command to find the mysql.sock file

$ find / -name mysql.sock

If not found, re-execute mysql_install_db to rebuild the authorization table

$ mysql/bin/mysql_install_db

Then execute the following command, the mysql.sock file will be there

$ mysql/bin/mysqld_safe &

$ find / -name mysql.sock
/tmp/mysql.sock