无法从PHP连接到远程MySQL服务器
问题描述:
- 客户端系统:CentOS的Linux的发布7.0.1406(核心)
- 客户端IP:222.222.222.222
http://test.com看起来到IP 222.222.222.222无法从PHP连接到远程MySQL服务器
服务器系统:Gentoo基本系统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.
回答
问题与客户端的环境 - SELINUX于CentOS。命令fo修复它:
setsebool -P httpd_can_network_connect=1
答
客户端环境问题 - Selinux在Centos中。 命令FO修复:
setsebool -P httpd_can_network_connect=1
+0
非常感谢! – 2016-10-16 08:48:13
检查的东西,如防火墙,了Suhosin/SELinux的百般阻挠的事,等等等等。 – 2014-11-24 15:11:14
很确定错误13是一个权限的事情。仔细检查PHP中的密码以确保? – rjdown 2014-11-24 15:13:57
为什么你在这一行有一个反斜杠 - 'new \ mysqli('111.11'? – 2014-11-24 15:14:05