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

时间:2010-11-02 13:39:26

标签: php apache

我在新安装的服务器上遇到了奇怪的情况,而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

是解决方案。

3 个答案:

答案 0 :(得分:125)

setsebool -P httpd_can_network_connect=1
对于访问此问题的许多人来说,

也是一个有用的CLI命令,允许从HTTP(Apache)请求中的mysql_connet()连接到远程MySQL数据库服务器,确保从SElinux中的httpd启用网络连接,通常位于/ etc / selinux / config(默认情况下禁用,以防止黑客使用您的httpd攻击其他计算机)。

答案 1 :(得分:29)

在CentOs 6上,您可以使用以下内容(不含-P

setsebool httpd_can_network_connect=1

答案 2 :(得分:2)

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

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

setsebool httpd_can_network_connect_db on

要使修复永久化以便后续重新启动,您需要执行以下操作:

setsebool -P httpd_can_network_connect_db on

感谢所有人在这个问题上将我从'#34;许可被拒绝&#34;地狱。 :)