连接DATABASE错误类型:2002:权限被拒绝

Sun*_*dar 6 php mysql curl selinux

我试图用以下脚本连接数据库(cxn-test.php)

<?php
$host = '155.30.136.20';//dummy ip 
$user = 'abc_user';
$pass = 'xxxxxxxxx';
$dbname = 'welcome';
$link = mysqli_connect($host, $user, $pass,$dbname);
if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}else {
    echo "success" . PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)

当我在尝试终端时

php cxn-test.php //成功

但是,当我尝试localhost时,我收到以下错误,

curl -s http://localhost/cxn-test.php

Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: Permission denied

这是一个奇怪的问题,它不能在localhost上运行,但在命令行上运行良好.

小智 21

在获得运行SELinux的新CentOS 7盒后,我遇到了同样的问题.我可以从命令行连接到我的远程MySQL数据库服务器,但Drupal(和测试PHP脚本)不能.

问题原来是SELinux安全策略.

默认情况下,禁用策略httpd_can_network_connect_db(意味着您的Web服务器无法联系远程数据库.)

检查这个:

getsebool -a | grep httpd

如果httpd_can_network_connect_db为Off,请通过以下方式启用它:

setsebool -P httpd_can_network_connect_db 1
Run Code Online (Sandbox Code Playgroud)

(-P标志使更改成为永久更改,因此设置可以在重新启动后继续存在.)

  • 不错,+ 1是因为您不告诉别人要关闭SELinux! (2认同)