PHP未连接到远程MySQL [英] PHP not connecting to Remote MySQL

查看:58
本文介绍了PHP未连接到远程MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用PHP连接到远程MySQL服务器,但出现以下错误:

I need to connect to a remote MySQL server with PHP but I get the following error:

Warning: mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on ... (13) in /var/www/html/index.php on line 16

Warning: mysqli::query(): Couldn't fetch mysqli in /var/www/html/index.php on line 17

Fatal error: Call to a member function fetch_assoc() on a non-object in /var/www/html/index.php on line 18

远程服务器firewal已打开,用户有权从任何主机进行连接,并且我能够使用mysql命令行从本地服务器进行连接,但无法与PHP连接.

The remote server firewal is open, user has permission to connect from any Host, and I'm able to connect from the local server with mysql command line, but not able to connect with PHP.

这里可能是什么问题?

已经尝试使用不同的连接代码,但是总是相同的错误.

Already tried with different connection code, but always same error.

这是最新的一个:

$mysqli = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS msg");
$row = $result->fetch_assoc();
echo $row['msg'];

推荐答案

发现这是SELinux的问题,不允许HTTPd网络连接.

Found this to be an issue with SELinux that is not allowing httpd network connections.

执行中:

setsebool -P httpd_can_network_connect=1

解决了问题.

这篇关于PHP未连接到远程MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆