MySQL安装好后,默认数据是存在/var/lib/mysql下的,我们可以在/etc/my.cnf下看到:
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
如果我们有其他数据盘,我们希望mysql的datadir修改到数据盘下(例如/storage)
于是就修改 datadir=/var/lib/mysql 改成 datadir=/storage/var/lib/mysql
然后就发现MySQL启动不起来了,查看日志是/storage/var/lib/mysql这个文件夹不存在
于是就手工新建/storage/var/lib/mysql这个文件夹,再次启动,发现还是不行,提示权限不对
于是就修改权限:
# 修改owner是mysql chown mysql.mysql /storage/var/lib/mysql # 修改权限 chmod 750 /storage/var/lib/mysql
再次启动MySQL,systemctl restart mysql
[Warning] Can't create test file /storage/var/lib/mysql/localhost.lower-test
还是不行,可是权限也给了,自己用mysql的账号去实际新建一个文件,也是不行
最后一番查找和折腾,终于找到了问题的根源,那就是selinux
默认CentOS可能是开着selinux的,导致MySQL在初始化数据库的时候,没有成功
所以解决办法有2个
- 一个是临时关闭selinux
执行setenforce 0
#临时关闭selinux setenforce 0 #关闭selinux后再启动 systemctl restart mysqld
- 还有一个办法是设置中彻底关闭selinux
找到配置文件:/etc/selinux/config
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=disabled # 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真是个大坑,我之前的文章里也多次遇到一些奇奇怪怪的问题,解决方法是关闭selinux,所以建议这货刚装完CentOS第一件事情就是关闭它!
文章评论