登錄服務器,使用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,成功連接