CentOS下php使用127.0.0.1不能連接mysql的解決方法

字號:


    php代碼很簡單:
    代碼如下:
    $server="127.0.0.1";
    println("Begin");
    $link = mysql_connect($server,"mysql","mysql");
    if (!$link) {
    die('Could not connect: ' . mysql_error().mysql_errno());
    }
    linux本機(jī)下使用php mysql.php 可以查看運(yùn)行結(jié)果,但是 在我的windows瀏覽器下報(bào)錯:
    代碼如下:
    Could not connect: Can't connect to MySQL server on '127.0.0.1' (13) 2003
    原因:
    代碼如下:
    #getsebool -a | grep httpd
    [neo@neo phpMyTest]$ getsebool -a | grep httpd
    發(fā)現(xiàn) httpd_can_network_connect --> off
    解決方案:
    代碼如下:
    #setsebool httpd_can_network_connect 1
    原來是 SELINUX,所以我一般直接關(guān)閉SELINUX和 iptables ip6tables
    代碼如下:
    # 關(guān)閉SELINUX
    chkconfig --level 12345 iptables off
    chkconfig --level 12345 ip6tables off
    service iptables stop
    service ip6tables stop
    查看SELinux狀態(tài):
    1、/usr/sbin/sestatus -v ##如果SELinux status參數(shù)為enabled即為開啟狀態(tài)
    SELinux status: enabled
    2、getenforce ##也可以用這個命令檢查
    關(guān)閉SELinux:
    1、臨時(shí)關(guān)閉(不用重啟機(jī)器):
    setenforce 0 ##設(shè)置SELinux 成為permissive模式
    ##setenforce 1 設(shè)置SELinux 成為enforcing模式
    2、修改配置文件需要重啟機(jī)器:
    修改/etc/selinux/config 文件
    將SELINUX=enforcing改為SELINUX=disabled