FAQ系列 | 啟用SELinux後,PHP串連MySQL異常

來源:互聯網
上載者:User

標籤: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異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.