[英]Connect DATABASE Error TYPE: 2002: Permission denied
I am trying to connect the database with the following script(cxn-test.php)我正在尝试使用以下脚本(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;
}
When I am trying on the terminal当我在终端上尝试时
php cxn-test.php //success php cxn-test.php //成功
But when I am trying on localhost i am getting the following error,但是当我在本地主机上尝试时,我收到以下错误,
curl -s http://localhost/cxn-test.php curl -s http://localhost/cxn-test.php
Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: Permission denied
This is strange issue it's not working on the localhost but working good on command line.这是一个奇怪的问题,它不能在 localhost 上运行,但在命令行上运行良好。
I had the same issue after getting a new CentOS 7 box, running SELinux. 在获得运行SELinux的新CentOS 7盒后,我遇到了同样的问题。 I could connect to my remote MySQL DB server from the command line, but Drupal (and test PHP scripts) could not. 我可以从命令行连接到我的远程MySQL数据库服务器,但Drupal(和测试PHP脚本)不能。
The issue turned out to be the SELinux security policies. 问题原来是SELinux安全策略。
By default, the policy httpd_can_network_connect_db is disabled (meaning that your web server cannot contact a remote DB.) 默认情况下,禁用策略httpd_can_network_connect_db (意味着您的Web服务器无法联系远程数据库。)
Check this via: 检查这个:
getsebool -a | grep httpd
If httpd_can_network_connect_db is Off, enable it via: 如果httpd_can_network_connect_db为Off,请通过以下方式启用它:
setsebool -P httpd_can_network_connect_db 1
(The -P flag makes the change permanent, so the setting survives a reboot.) (-P标志使更改成为永久更改,因此设置可以在重新启动后继续存在。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.