在LNMP使用过程中,使用mysqli_connect()链接mysql数据库时,出现以下错误
Could not connect: Can't connect to MySQL server on '127.0.0.1' (13)
使用下面命令查看时
#getsebool -a | grep httpd
发现
httpd_can_network_connect --> off
执行下面命令之后
#setsebool httpd_can_network_connect 1
可以改为on的状态
再次连接数据库时可以正常使用了
但是等到系统重启之后,发现同样的错误又出现了
于是我查看了SELinux的状态
#/usr/sbin/sestatus -v
结果发现是
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: 28
Process contexts:
Current context: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Init context: system_u:system_r:init_t:s0
/usr/sbin/sshd system_u:system_r:sshd_t:s0-s0:c0.c1023
File contexts:
Controlling terminal: unconfined_u:object_r:user_devpts_t:s0
/etc/passwd system_u:object_r:passwd_file_t:s0
/etc/shadow system_u:object_r:shadow_t:s0
/bin/bash system_u:object_r:shell_exec_t:s0
/bin/login system_u:object_r:login_exec_t:s0
/bin/sh system_u:object_r:bin_t:s0 -> system_u:object_r:shell_exec_t:s0
/sbin/agetty system_u:object_r:getty_exec_t:s0
/sbin/init system_u:object_r:bin_t:s0 -> system_u:object_r:init_exec_t:s0
/usr/sbin/sshd system_u:object_r:sshd_exec_t:s0
发现SELinux是开启的
只要我们编辑配置文件
#/etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled
之后再重启,上面的错误不会出现,数据库可以正常连接了