PHP无法连接到错误13的MySQL(但命令行可以)

我在新安装的服务器上有奇怪的情况,这次Google似乎帮不了我。 我无法连接到(远程)从我的PHP代码的MySQL。 当我尝试从同一台服务器上的命令行进行连接时,连接失败。

无法连接:无法连接到“MYSQL.SERVER”上的MySQL服务器(13)

这里是代码和命令行的连接尝试

[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 

我尝试在mod_php模式和FastCGI中运行php脚本,检查“/etc/php.d/mysql.ini”是否出现在phpinfo()以及mysql,mysqli和pdo_mysql部分。

但结果是一样的,我知道它简单,但我不能。 请帮忙 :)

编辑:问题是与SElinux

 setsebool -P httpd_can_network_connect_db=1 

是解决scheme。

 setsebool -P httpd_can_network_connect=1 

对于许多访问这个问题的人来说也是一个有用的CLI命令,为了允许从HTTP(Apache)请求到远程MySQL数据库服务器的mysql_connet()连接,确保启用通常位于/ etc / selinux / config(默认情况下禁用,以防止黑客利用你的httpd攻击其他机器)。

在CentOs 6上,您可以使用以下(不带-P

 setsebool httpd_can_network_connect=1 

在Fedora 21上使用apache 2 / httpd版本2.6使用php版本5.6连接到远程mysql服务器5.6或mariadb版本10.甚至在php代码中指定服务器的FQDN而不是localhost时连接到本地服务器似乎是一个问题。

该命令将修复当前会话的权限问题:

 setsebool httpd_can_network_connect_db on 

为了使修复永久性的以后重新启动,你需要这样做:

 setsebool -P httpd_can_network_connect_db on 

感谢所有关于这个问题的救援我从“权限被拒绝”地狱。 🙂