SQLSTATE[HY000][2003]无法在'XXX.XXX.XXX. xxx '上连接到MySQL服务器。XXX(60)

时间:2022-09-19 22:35:20

I'm developing a Web Application under PHP with a remote database, in which I have the following test script:

我正在用PHP开发一个带有远程数据库的Web应用程序,其中有以下测试脚本:

try {
    $dbh = new PDO('mysql:host=XXX.XXX.XXX.XXX;dbname=db_app;', $user, $password);
    $date = ($dbh->query('SELECT NOW()');
    print_r($date);   
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
exit();

and when I run it I get the following output:

当我运行它时,我得到如下输出:

Error!: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '10.16.50.87' (60)

错误!: SQLSTATE[HY000][2003]在'10.16.50.87'(60)无法连接MySQL服务器

I can still connect to the target database using MySQL workbench and through Command Line Interface.

我仍然可以使用MySQL workbench和命令行接口连接到目标数据库。

From this question I get that the remote server configuration "could have been" the problem. However, we have a "brother" PHP application connecting to the same server without problems, the only difference beetwen the two is the username and password, and even when running tests on my application using the "brother" PHP application credentials, the same error is shown. So it seems that only my PHP seems to have the problem.

从这个问题中,我得到远程服务器配置“可能是”问题所在。但是,我们有一个“兄弟”PHP应用程序连接到同一个服务器,没有问题,唯一的区别是用户名和密码,甚至当使用“兄弟”PHP应用程序凭据在我的应用程序上运行测试时,也会显示相同的错误。看来只有我的PHP有问题。

I'm running tests on PHP 5.5.3 and the target server is running MySQL 5.5 Both My Application and the Brother Application are running on the Staging Server A, and both should be connecting to MySQL in Staging Server B. The Brother application is also written in PHP and is able to connect, but mine isn't. I'm guessing it could be a MySQL-relative PHP configuration.

我运行测试PHP 5.5.3和目标服务器运行MySQL 5.5我的应用程序和弟弟交付准备服务器上运行应用程序,并且都应该连接到MySQL在交付准备服务器b .哥哥应用程序也用PHP编写的,可以连接,但是我不是。我猜它可能是一个与mysql相关的PHP配置。

  • My application uses PDO, while the brother application uses mysqli.

    我的应用程序使用PDO,而兄弟应用程序使用mysqli。

  • The brother application runs on the same server as my application, and connects to the same target server.

    兄弟应用程序运行在与我的应用程序相同的服务器上,并连接到相同的目标服务器。

Any help would be appreciated.

如有任何帮助,我们将不胜感激。

1 个解决方案

#1


4  

For me it was SELinux causing the issues:

对我来说,是SELinux引起了问题:

[root@server ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted
[root@server ~]# setenforce 0
[root@server ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   permissive
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted

Run below as root:

作为根用户运行以下:

setsebool -P httpd_can_network_connect=1

setsebool - p httpd_can_network_connect = 1

#1


4  

For me it was SELinux causing the issues:

对我来说,是SELinux引起了问题:

[root@server ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted
[root@server ~]# setenforce 0
[root@server ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   permissive
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted

Run below as root:

作为根用户运行以下:

setsebool -P httpd_can_network_connect=1

setsebool - p httpd_can_network_connect = 1