在新安裝mysql后進行啟動發現報錯
mysql啟動服務命令
systemctl start mysqld@3306
Starting mysqld (via systemctl): Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld3306.service" and "journalctl -xe" for details.
按照錯誤提示信息進行排查
systemctl status mysqld3306.service
journalctl -xe
嘗試連接mysql報錯查看sock文件夾發現sock文件丟失
[root@cdh03 mysqldata3309]# mysql -uroot -S /data/mysqldata3309/sock/mysql.sock -p"Ygadb(vw<6ae"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/data/mysqldata3309/sock/mysql.sock' (2)
使用命令journalctl -xe查看報錯具體原因,發現紅色找字體的日志:SELinux is preventing mysqld from getattr access on the file /var/lib/mysql/ibdata1. For complete SELinux messages run: sealert -l
發現是selinux限制導致mysql服務啟動失敗
解決辦法
1. 查看SELinux狀態
1.1 getenforce
getenforce 命令是單詞get(獲取)和enforce(執行)連寫,可查看selinux狀態,與setenforce命令相反。
setenforce 命令則是單詞set(設置)和enforce(執行)連寫,用於設置selinux防火牆狀態,如: setenforce 0用於關閉selinux防火牆,但重啟后失效
[root@cdh03 ~]# getenforce
Enforcing
1.2 /usr/sbin/sestatus
Current mode表示當前selinux防火牆的安全策略
[root@cdh03 ~]# /usr/sbin/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: 28
SELinux status:selinux防火牆的狀態,enabled表示啟用selinux防火牆
Current mode: selinux防火牆當前的安全策略,enforcing 表示強
2. 關閉SELinux
2.1 臨時關閉
setenforce 0 :用於關閉selinux防火牆,但重啟后失效。
[root@cdh03 ~]# setenforce 0
[root@cdh03 ~]# /usr/sbin/sestatus
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: 28
2.1 永久關閉
修改selinux的配置文件,重啟后生效。
打開 selinux 配置文件
[root@cdh03 ~]# vim /etc/selinux/config
修改 selinux 配置文件
將SELINUX=enforcing改為SELINUX=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=enforcing
SELINUXTYPE= can take one of three two values:
targeted - Targeted processes are protected,
minimum - Modification of targeted policy. Only selected processes are protected.
mls - Multi Level Security protection.
SELINUXTYPE=targeted
此時獲取當前selinux防火牆的安全策略仍為Enforcing,配置文件並未生效。
[root@cdh03 ~]# getenforce
Enforcing
重啟
[root@cdh03 ~]# reboot
驗證
[root@cdh03 ~]# /usr/sbin/sestatus
SELinux status: disabled
[root@cdh03 ~]# getenforce
Disabled