ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

错误信息:

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

出现问题原因:

有可能是 my.cnf 配置文件中设置了 [mysqld] 的参数 socket ,而没有设置[client]的参数socket

mysql.sock 文件有什么用:

mysql 支持 socket 和 TCP/IP 连接。那么 mysql.sock 这个文件有什么用呢?连接localhost通常通过一个Unix域套接字文件进行,一般是/tmp/mysql.sock。如果套接字文件被删除了,本地客户就不能连接。/tmp 文件夹属于临时文件,随时可能被删除。

1.TCP 连接(如果报错 /tmp/mysql.sock,你可以尝试这种方式连接)

mysql -uroot -h 127.0.0.1 -p

2.socket 连接

mysql -uroot -p

解决方式:

添加 [client] 配置项,如下所示

配置前:(配置 [client] 前,会报错'/tmp/mysql.sock' (2))

[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

配置后:(配置 [client] 后,重启 mysql服务)

[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/var/lib/mysql/mysql.sock(跟这个socket路径一样)
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
port=3306
socket=/var/lib/mysql/mysql.sock

转自:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'_Mysql_脚本之家 (jb51.net)

 

posted @ 2022-01-19 10:00  薄荷味的糖  阅读(473)  评论(0编辑  收藏  举报