本文共 1978 字,大约阅读时间需要 6 分钟。
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文件中修改
<?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
这样就解决了
本文转自 神迹难觅 51CTO博客,原文链接:http://blog.51cto.com/ji123/1951888,如需转载请自行联系原作者