PHP MySQLi权限被拒绝,但可以从MySQL CLI使用 [英] PHP MySQLi permission denied but working from MySQL CLI
问题描述
我收到错误
Failed to connect to MySQL: Permission denied
从PHP与MySQL,MySQLi和PDO连接到远程MySQL主机时.
When connecting from PHP with MySQL, MySQLi and PDO to a remote MySQL host.
但是,如果我在命令行上从同一主机通过MySQL以相同的凭据连接到同一主机,则效果很好.
However, if I connect to the same host, with the same credentials through MySQL from the same host on the command line it works perfectly.
所以我要假设它是一个PHP配置问题?
So I'm making the assumption its a PHP config problem?
连接自
PHP 5.6.40 (Client API version => mysqlnd 5.0.11-dev)
MySQL 5.5.59
连接到
PHP 5.6.23 (Client API version => mysqlnd 5.0.11-dev)
MySQL 5.5.52
有人知道为什么我可以从命令行连接到MySQL,但不能从PHP连接到具有完全相同凭据的同一主机吗?
Anyone got any ideas why I can connect from the command line to MySQL but not from PHP to the same host with the exact same credentials?
推荐答案
我猜您在服务器上启用了SELinux. 默认情况下,它不允许apache进程初始化传出网络连接.
I guess that you have SELinux enabled on your server. By default it doesn't allow apache process to initialize outgoing network connections.
https://serverfault.com/a/456875/442205
要检查SELinux
To check SELinux
sestatus
查看在httpd进程上设置了哪些标志
To see what flags are set on httpd processes
getsebool -a | grep httpd
允许Apache通过SELinux连接到远程数据库
To allow Apache to connect to remote database through SELinux
setsebool httpd_can_network_connect_db 1
使用-P选项使更改永久生效.如果没有此选项,则布尔值将在重新启动时重置为0.
Use -P option makes the change permanent. Without this option, the boolean would be reset to 0 at reboot.
setsebool -P httpd_can_network_connect_db 1
这篇关于PHP MySQLi权限被拒绝,但可以从MySQL CLI使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!