简体   繁体   English

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

[英]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.

相关问题 SQLSTATE[HY000] [2002] 权限被拒绝 - SQLSTATE[HY000] [2002] Permission denied Phpmyadmin.mysqli_real_connect(): (HY000/2002): 权限被拒绝。 您的配置中定义的 controluser 连接失败 - Phpmyadmin. mysqli_real_connect(): (HY000/2002): Permission denied. Connection for controluser as defined in your configuration failed Linux上的xampp phpmyadmin访问被拒绝错误(#2002) - xampp phpmyadmin access denied error(#2002) on linux SMTP 错误:无法连接到服务器:权限被拒绝 (13) - SMTP ERROR: Failed to connect to server: Permission denied (13) CentOS 7无法连接clamd.sock文件(权限被拒绝错误) - CentOS 7 unable to connect clamd.sock file (Permission denied error) libvirt-php收到错误:无法连接到服务器权限被拒绝 - libvirt-php receives the error: unable to connect to server Permission denied Laravel的Artisan无法连接到数据库,并引发错误“ SQLSTATE [HY000] [2002]” - Laravel's Artisan Can't Connect to Database, Throws Error “SQLSTATE[HY000] [2002]” 错误:PleskDBException:无法连接到数据库:mysql_connect():没有这样的文件或目录 /var/run/mysqld/mysqld.sock(错误代码:2002)。 - ERROR: PleskDBException: Unable to connect to database: mysql_connect(): No such file or directory /var/run/mysqld/mysqld.sock (Error code: 2002). 权限被拒绝,错误日志 - Permission denied, error log Opencart:权限被拒绝错误 - Opencart : Permission Denied Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM