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

登录服务器,使用root用户连接mysql时出现错误提示:

$ bin/mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

尝试重启mysql

$ ./mysql.server restart
Shutting down MySQL (Percona Server).......                [  OK  ]
rm: cannot remove `/var/lock/subsys/mysql': Permission denied
Starting MySQL (Percona Server)..                          [  OK  ]
[mysql@tongxtx mysql]$ bin/mysql -uroot -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

还是报同样的错。先看一下my.cnf

more my.cnf |grep sock
socket               = /opt/mysql/mysql.sock
socket              = /opt/mysql/mysql.sock

可以看到我的mysql已经声明了mysql.sock的文件位置,而错误提示为/tmp/mysql.sock; 也就是说虽然我的mysql声明了sock文件的位置,但是客户端在连接的时候, linux系统总是去/tmp目录下去找,找到了原因,就可以对症下药了:

为mysql.sock创建链接文件:

$ ln -s /opt/mysql/mysql.sock /tmp/mysql.sock
然后再次连接mysql,成功连接

你可能感兴趣的:(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2))