撞牆很久了 才發現這個問題
在此之前 要先做兩件事情的確認
1.從mysql clinet連線是否有問題
2.在command line下執行測試PDO連線
如果都過了 那得先檢查SELINUX
在系統上檢查SELINUX
# getenforce
如果上面的結果是 Enforcing, 即 SELinux 正在執行; Permissive 是臨時關閉; 如果是 Disabled, 即已經關閉
簡單的方法是關閉SELINUX就可以了
暫時關閉linux
#sudo setenforce 0
如果要完整關閉
#sudo vi /etc/sysconfig/selinux
找到
SELINUX=enforcing
然後修改為
SELINUX=disabled
要重新開機 reboot / restart 後才會套用
另外的方法是 setsebool
例如這個方案
http://stackoverflow.com/questions/8139451/pdo-connection-works-from-command-line-but-not-through-apache
this is a Red Hat-derived distribution (RHEL, CentOS, Fedora, ScientificLinux) running SELinux (or any non Red Hat derivative using SELinux), the default policy setting at time of this writing is to prohibit Apache from making external connections to other servers or databases. As root, you must enable the following two SELinux booleans. Use the -P
option to persist the change across a reboot.
setsebool -P httpd_can_network_connect=1
setsebool -P httpd_can_network_connect_db=1
Note that httpd_can_network_connect
may not be necessary. Try it first turning on only httpd_can_network_connect_db
那要如何確認設定成功呢
輸入
getsebool -a | grep ‘httpd’
他會顯示
httpd_can_network_connect –> on
httpd_can_network_connect_cobbler –> off
httpd_can_network_connect_db –> on
看到on 就代表成功了