当在使用Apache通过PHP访问MySQL时无法建立连接的情况下

總結

Note: This translation option is only one of many possible ways to paraphrase the given phrase in Chinese.

SElinux 是原因

现象- 指发生的事物或事件。

# mysql -h192.168.xxx.xxx -uUSER DB -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7792537
Server version: 5.1.44-community-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>

# php test.php
<?php
$dbh = new mysqli('192.168.xxx.xxx','USER','PASS','DB');
$dbh->close();

这个也可以。

http://localhost/test.php

无法连接。

[18-Aug-2014 16:30:37] PHP Warning:  mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on '192.168.xxx.xxx' (13) in /path/test.php on line 2

处方

# getenforce
Enforcing
# setenforce 0
# getenforce
Permissive

我担心连接远程服务器时会触发安全警报。

请按照规定操作。

SQLSTATE[HY000] [2003] 无法连接到MySQL服务器‘xxx.xxx.xxx.xxx’(13)。