CentOS下httpd下php 連接mysql 本機可以,127.0.0.1不能訪問


你看到的這個文章來自於http://www.cnblogs.com/ayanmw

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本機下使用php mysql.php 可以查看運行結果,但是 在我的windows瀏覽器下報錯:

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

發現 httpd_can_network_connect --> off
解決方案:
#setsebool httpd_can_network_connect 1

 

原來是 SELINUX,所以我一般直接關閉SELINUX和 iptables ip6tables

復制代碼
# 關閉SELINUX
chkconfig --level 12345 iptables off

chkconfig --level 12345 ip6tables off

service iptables stop

service ip6tables stop




查看SELinux狀態:

1、/usr/sbin/sestatus -v      ##如果SELinux status參數為enabled即為開啟狀態

SELinux status:                 enabled

2、getenforce                 ##也可以用這個命令檢查

關閉SELinux:

1、臨時關閉(不用重啟機器):

setenforce 0                  ##設置SELinux 成為permissive模式

                              ##setenforce 1 設置SELinux 成為enforcing模式

2、修改配置文件需要重啟機器:

修改/etc/selinux/config 文件

將SELINUX=enforcing改為SELINUX=disabled
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM