php 无法连接到 mysql,错误 13(但命令行可以)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4078205/
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
php can't connect to mysql with error 13 (but command line can)
提问by SimSimY
I have weird situation in newly installed server, and it seems that Google can't help me this time. I can't connect to (remote) mysql from my php-code. When I try to connect from command line on the same server the connection succseds.
我在新安装的服务器上出现了奇怪的情况,这次谷歌似乎帮不了我了。我无法从我的 php 代码连接到(远程)mysql。当我尝试从同一台服务器上的命令行连接时,连接成功。
Could not connect: Can't connect to MySQL server on 'MYSQL.SERVER' (13)
无法连接:无法连接到“MYSQL.SERVER”上的 MySQL 服务器 (13)
Here is the code and the connect attempt from the command line
这是来自命令行的代码和连接尝试
[u1@bosko httpdocs]$ cat test.php
<?
$link = mysql_connect('MYSQL.SERVER', 'testusersimon', '123456');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
[u1@bosko httpdocs]$ mysql -h MYSQL.SERVER -utestusersimon --password=123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 352108
Server version: 5.0.45-community-nt-log MySQL Community Edition (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> quit
Bye
I tried running the php script both in mod_php mode and in FastCGI, check that "/etc/php.d/mysql.ini" shows up in the phpinfo() as well as mysql,mysqli and pdo_mysql sections.
我尝试在 mod_php 模式和 FastCGI 中运行 php 脚本,检查“/etc/php.d/mysql.ini”是否出现在 phpinfo() 以及 mysql、mysqli 和 pdo_mysql 部分中。
but the result was the same, I know its something simple but I just can't . Please help :)
但结果是一样的,我知道它很简单,但我不能。请帮忙 :)
Edit: The problem was with SElinux
编辑:问题出在 SElinux
setsebool -P httpd_can_network_connect_db=1
Was the solution.
是解决方案。
回答by Pallieter Koopmans
setsebool -P httpd_can_network_connect=1
will also be a helpful CLI command to many people visiting this question, as to allow mysql_connet() connections from within HTTP (Apache) requests to a remote MySQL database server, ensure to enable Network Connections from httpd in SElinux usually located in /etc/selinux/config (disabled by default to prevent hackers from attacking other machines using your httpd).
对于访问此问题的许多人来说,这也是一个有用的 CLI 命令,为了允许从 HTTP(Apache)请求中的 mysql_connet() 连接到远程 MySQL 数据库服务器,确保在 SElinux 中启用来自 httpd 的网络连接,通常位于 /etc/ selinux/config(默认禁用以防止黑客使用您的 httpd 攻击其他机器)。
回答by Max Chernopolsky
On CentOs 6, you can use the following (without -P
)
在 CentOs 6 上,您可以使用以下(不带-P
)
setsebool httpd_can_network_connect=1
回答by Grant Rostig
On Fedora 21 with apache 2/httpd version 2.6 using php version 5.6 when connecting to a remote mysql server 5.6 or mariadb version 10. It even seems to be a problem connecting to local server when specifying the server's FQDN instead of localhost in the php code.
在带有 apache 2/httpd 2.6 版的 Fedora 21 上,当连接到远程 mysql 服务器 5.6 或 mariadb 10 版时,使用 php 5.6 版。在 php 代码中指定服务器的 FQDN 而不是 localhost 时,连接到本地服务器似乎是一个问题.
This command will fix the permissions problem for the current session:
此命令将修复当前会话的权限问题:
setsebool httpd_can_network_connect_db on
To make the fix permanent for subsequent reboots you need to do this:
要使修复永久用于后续重新启动,您需要执行以下操作:
setsebool -P httpd_can_network_connect_db on
Thanks to all on this question for rescuing me from "permission denied" hell. :)
感谢所有关于这个问题的人将我从“权限被拒绝”的地狱中解救出来。:)