yum install php php-mysql mysql
yum install mysql-server //這個必須安裝,才會有/etc/init.d的mysqld
mysql 使用的端口為3306
注意1.我們驗證php程式執行環境
方法,在DoucumentRoot路徑下建個test.php檔案
<?php
phpinfo();
?>
用浏覽器浏覽後有php的頁面顯示。
2.測試php程式與mysql通信
方法,在 test.php檔案中修改
$link = mysql_connect('127.0.0.1','root','');
if(!$link){
die("datebase connect error:".mysql_error());//連接配接不上輸出錯誤注意點
}
else
echo "Success";
?>
若出現報錯:SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XXX' (13)
這是SELinux不允許通路外網,
簡單點就可以使用setenfroce=0;就可以了。
或者: 使用指令getsebool - get SELinux boolean value(s)
指令 getsebool -a | grep httpd
allow_httpd_anon_write --> off
allow_httpd_mod_auth_ntlm_winbind --> off
allow_httpd_mod_auth_pam --> off
allow_httpd_sys_script_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> on
httpd_dbus_sssd --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_manage_ipa --> off
httpd_read_user_content --> off
httpd_run_preupgrade --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_tmp_exec --> off
httpd_tty_comm --> on
httpd_unified --> on
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_verify_dns --> off
注意這裡httpd_can_network_connect --> off
這裡設定使用指令setsebool httpd_can_network_connect 1
PS: 1. 若出現錯誤 Lost connection to MySQL server at 'reading initial communication packet', system error: 111
有可能是mysql服務沒打開,可用指令service mysqld start
2.若出現錯誤ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.18.9' (113)
很可能是防火牆的問題
iptables -F
iptables -L
<b>這樣就解決了</b>
<b></b>
<b>本文轉自 神迹難覓 51CTO部落格,原文連結:http://blog.51cto.com/ji123/1951888,如需轉載請自行聯系原作者</b>