postgresql connection failure:SQLSTATE[08006] [7] could not connect to server: Permission denied Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?


PHP 程序無法連接到 CentOS 上的PostgreSQL,但是在 CentOS 服務器上卻能正常運行 psql,

操作如下:多次重啟 PG 數據庫后發現 CGI 腳本無法連接數據庫,但是可以使用 psql 連接上數據庫

PHP 程序報錯如下:

PostgreSQL server: could not connect to server: Permission denied Is the server running on host "localhost" (127.0.0.1) 
and accepting TCP/IP connections on port 5432?

 

網上查了很多文章,發現是SELinux(Linux上的強制訪問控制安全模塊)惹得禍,很想關閉它,

但是據說他是系統被攻破后的最后一道防線,那就暫時記錄此類問題的解決方案,以后有時間的話,好好研究一下這個SELinux!

 

此問題的原因是SELinux阻止httpd進程--httpd_can_network_connect_db 連接數據庫--不管是哪種類型的數據庫

所以需要確保設置正確的布爾值以允許Web應用程序與數據庫通信,使用setsebool命令改變該布爾變量的狀態,從而使得httpd進程能夠訪問數據庫服務器:

sudo setsebool -P httpd_can_network_connect_db 1

 

改變成功后,可以使用getsebool命令進行查看:

/usr/sbin/getsebool httpd_can_network_connect_db

 

 

查看 SELinux 設置的所有 Booleans 值

getsebool -a

 

 

上述狀態改變只是暫時性的,一旦系統重啟,該變量狀態將改變回初始狀態,因此,可以使用如下命令永久性改變狀態:

/usr/sbin/setsebool -P httpd_can_network_connect_db on

 


如需關閉SELinux則可以執行以下操作
修改/etc/selinux/config SELINUX=enforcing 為 SELINUX=disabled

 

參考鏈接:

https://stackoverflow.com/questions/27749691/php-cant-connect-to-postgresql-on-centos-7

https://unix.stackexchange.com/questions/8854/how-do-i-configure-selinux-to-allow-outbound-connections-from-a-cgi-script

關於 SELinux  的參考鏈接:

https://wiki.centos.org/TipsAndTricks/SelinuxBooleans#line-44

http://www.blogjava.net/ycyk168/archive/2012/09/09/387340.html


免責聲明!

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



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