dongzhi1851 2016-12-16 06:49
浏览 297
已采纳

连接DATABASE错误类型:2002:权限被拒绝

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;
}

When I am trying on the terminal

php cxn-test.php //success

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

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

  • 写回答

1条回答 默认 最新

  • douyun4524 2017-01-05 01:08
    关注

    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.

    The issue turned out to be the SELinux security policies.

    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

    If httpd_can_network_connect_db is Off, enable it via:

    setsebool -P httpd_can_network_connect_db 1
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动