且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

连接数据库错误类型:2002:权限被拒绝

更新时间:2022-11-25 08:46:19

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

I had the same issue after getting a new CentOS 7 box, running SELinux. I could connect to my remote MySQL DB server from the command line, but Drupal (and test PHP scripts) could not.

问题出在SELinux安全策略上.

The issue turned out to be the SELinux security policies.

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

By default, the policy httpd_can_network_connect_db is disabled (meaning that your web server cannot contact a remote DB.)

通过以下方式进行检查:

Check this via:

getsebool -a | grep httpd

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

If httpd_can_network_connect_db is Off, enable it via:

setsebool -P httpd_can_network_connect_db 1

(-P标志使更改永久生效,因此该设置在重新启动后仍然有效.)

(The -P flag makes the change permanent, so the setting survives a reboot.)