標籤:mysql faq php selinux
同事報告一起奇怪的現象,一個最簡單的測試PHP代碼,在測試環境很正常,但是在正式環境下,無論用何種方式(tcp/ip、unix socket)都無法串連mysql。
我協助查看了下,確實如此,無論是指定IP、連接埠的tcp/ip方式串連,或者是用unix socket方式串連,報錯資訊都類似:
Could not connect: Can‘t connect to MySQL server on ‘MYSQL.SERVER‘ (13)
無論如何修改MySQL的授權,或者調整php.ini中關於MySQL的設定,或者修改MySQL的監聽網口,都無法解決。
而如果用命令列人工串連MySQL,則一切正常。
問題看起來像是php(with apache)不被允許串連MySQL,但是防火牆也沒有限制。想來想去,唯有SELinux的因素會導致這個問題。
getsebool -a|grep -i httpd
可以看到這裡設定了httpd進程的許可模式,再仔細看一下,有一個選項:
getsebool -a|grep -i httpd_can_network_connecthttpd_can_network_connect --> off
現在明白了,原來是SELinux限制了httpd對外訪問的許可權。將其開啟即可:
setsebool -P httpd_can_network_connect=1
如果對SELinux不熟悉,也可以直接修改系統設定檔 /etc/sysconfig/selinux,全域關閉:
# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.#SELINUX=enforcing#把設定改為disabledSELINUX=disabled# SELINUXTYPE= can take one of these two values:# targeted - Targeted processes are protected,# mls - Multi Level Security protection.SELINUXTYPE=targeted
然後重啟作業系統,即可生效。
或者,直接執行命令,可線上生效:
[[email protected]~]# setenforce 0setenforce: SELinux is disabled
--------------------------------------分割線--------------------------------------
知數堂 (http://zhishuedu.com)培訓是由資深MySQL專家葉金榮、吳炳錫聯合推出的專業優質培訓品牌,主要有MySQL DBA實戰最佳化和Python營運開發課程,是業內最有良心、最有品質的培訓課程。
本文出自 “老葉茶館” 部落格,請務必保留此出處http://imysql.blog.51cto.com/1540006/1879758
FAQ系列 | 啟用SELinux後,PHP串連MySQL異常