你的位置:首页 > 数据库

[数据库]ERROR 2002 (HY000): Cant connect to local MySQL server through socket /tmp/mysql.sock (2)

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

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

尝试重启mysql

$ ./mysql.server restartShutting down MySQL (Percona Server).......        [ OK ]rm: cannot remove `/var/lock/subsys/mysql': Permission deniedStarting MySQL (Percona Server)..             [ OK ][[email protected] mysql]$ bin/mysql -uroot -pEnter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

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

more my.cnf |grep socksocket        = /opt/mysql/mysql.socksocket       = /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,成功连接