无法从php连接到远程Mysql服务器

时间:2014-11-24 15:05:24

标签: php mysql linux centos

  • 客户端系统:CentOS Linux 7.0.1406版(核心版)
  • 客户端IP:222.222.222.222
  • http://test.com查看ip 222.222.222.222

  • 服务器系统:Gentoo Base System版本1.12.13

  • 服务器IP:111.111.111.111

连接脚本:

$connection = new \mysqli('111.111.111.111', 'user', 'password', 'db');
if (mysqli_connect_error()) {
    echo "Error: {$connection->connect_error}"; 
}
else {
    echo "Server Info: {$connection->server_info}";
}

当我尝试在浏览器“http://test.com/test.php”中打开时,我得到:

Error: Can't connect to MySQL server on '111.111.111.111' (13)

...但是当我从服务器222.222.222.222上的命令行启动此脚本时,我有另一个结果:

/usr/bin/php /var/www/reporter/test.php
Server Info: 5.5.14-enterprise-commercial-advanced-log

从命令行直接连接也正确:

mysql -h 111.111.111.111 -u user -p -P 3306
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 25236357
Server version: 5.5.14-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.




应答


客户环境问题 - Centos中的Selinux。命令解决它:

setsebool -P httpd_can_network_connect=1

1 个答案:

答案 0 :(得分:4)

客户环境问题 - Centos中的Selinux。 用于修复它的命令:

setsebool -P httpd_can_network_connect=1