php 无法连接:无法通过套接字'/var/lib/mysql/mysql.sock'连接到本地MySQL服务器(2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10712462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:52:30  来源:igfitidea点击:

Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

phpmysqlcentos

提问by run

When i connect my database by a php script it shows

当我通过 php 脚本连接我的数据库时,它显示

Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

the username and password is right.

用户名和密码是对的。

something of the my.cnf

的东西 my.cnf

[client]
#password       = your_password
port            = 3306
socket          = /data/mysql/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /data/mysql/mysql.sock
skip-external-locking

ps: i change it to socket = /var/lib/mysql/mysql.sockthe php script still says "/var/lib/mysql/mysql.sock"?. then run

ps:我把它改成socket = /var/lib/mysql/mysql.sockphp脚本仍然说“/var/lib/mysql/mysql.sock”?。然后运行

  mysqladmin -u root -p status

it shows

表明

mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/var/lib/mysql/mysql.sock' exists!




service mysqld restart


mysqld: unrecognized service

采纳答案by paulsm4

Q: Does your my.conf still says "/var/lib/mysql/mysql.sock"?

问:你的 my.conf 是否仍然显示“/var/lib/mysql/mysql.sock”?

Q: Does the directory "/var/lib/mysql" exist?

问:目录“/var/lib/mysql”是否存在?

Q: Does your mysql user (or better, mysql group) have writer permission?

问:您的 mysql 用户(或更好的 mysql 组)是否具有 writer 权限?

If so, please try re-starting mysql and see what happens.

如果是这样,请尝试重新启动 mysql,看看会发生什么。

ADDENDUM:

附录:

One other thing to try is to createa mysql.sock and explicitly giveit permissions:

要尝试的另一件事是创建一个 mysql.sock 并明确授予它权限:

cd /var/lib
mkdir mysql
touch mysql/mysql.sock
chown -R mysql:mysql mysql