Linux运行模式带来的问题

因为我想自己搭建个LAPM环境进行开发PHP,我经过1个小时搭建环境终于搭建完成但是我使用PDO连接MSYQL时就出现了“ PDOException: SQLSTATE[HY000] [2002] Permission denied in /var/www/html/index.php:13 Stack trace: #0 /var/www/html/index.php(13): PDO->__construct(‘mysql:host=127….’, ‘root’, ‘?ijMPd5&’) #1 {main} ”的错误,然后我就去查找,终于找到问题所在,因为我用的系统是CentOS,需要关闭运行模式才可以正常使用PDO。

运行模式介绍:https://blog.csdn.net/qq_41453285/article/details/88539523

解决办法:

查询运行模式状态

sestatus
# 未关闭状态
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

1.临时关闭

setenforce 0
# 关闭后的状态
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

2.永久关闭

1.编辑配置文件文件目录“ /etc/selinux/config

vi /etc/selinux/config
# 把SELINUX中的enforcing替换成disabled

# 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=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

2.命令进行修改

sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config 

修改完成后需要重启系统

reboot