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

标签 php mysql linux centos

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

  • 服务器系统:Gentoo Base System release 1.12.13

  • 服务器地址: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

最佳答案

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

setsebool -P httpd_can_network_connect=1

关于php - 无法从 php 连接到远程 Mysql 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27107881/

相关文章:

linux - 转义网址的 sed 正则表达式

php - parse_signed_request - base64_url_decode - strstr 使用

php - 在 PHP 中使用整数过滤数组值

mysql - SQL 约束使 2 列彼此不相等

php - include 和 require 函数中的声明错误

linux - 在linux启动时执行带参数的脚本

php - 具有自己属性的特征

PHP 表单 - 带有验证蜜 jar

MySQL 复制 : if I don't specify any databases, 将 log_bin 记录一切?

linux - 如何在 UNIX 上删除带有特殊字符的目录名和带有环境变量名的目录