连接数据库错误类型:2002:权限被拒绝 [英] Connect DATABASE Error TYPE: 2002: Permission denied

查看:663
本文介绍了连接数据库错误类型:2002:权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下脚本(cxn-test.php)连接数据库

I am trying to connect the database with the following script(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;
}

当我在终端上尝试

php cxn-test.php//成功

php cxn-test.php //success

但是当我在localhost上尝试时,出现以下错误,

But when I am trying on localhost i am getting the following error,

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

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

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

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

This is strange issue it's not working on the localhost but working good on command line.

推荐答案

获得运行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.)

这篇关于连接数据库错误类型:2002:权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆