1.如果把selinux关上,更改存储路径之后,在重启肯定没有任何的问题,但如果selinux为 Enforcing 模式,再改完存储路径在重启就不适用了
2.把mysql存储路径改到 /var/xp/mysql/下,只需更改这两个路径即可
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
(10:43:09)[test3 root ~] # cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#datadir=/var/xp/mysql
#socket=/var/xp/mysql/mysql.sock
#skip-grant-tables
log-bin
server-id=1
gtid_mode=ON
enforce_gtid_consistency=1
log-error=/var/log/mysqld.log
#log_timestamps = SYSTEM
#general_log = 1
#general_log_file=/var/lib/mysql/mysql/general.log
pid-file=/var/run/mysqld/mysqld.pid
3.看一下selinux是否为 Enforcing 模式
[root@test1 ~]# cat /etc/selinux/config
# 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 values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
4.创建文件
mkdir /var/xp/
#用rsync传输把mysql传输过去,保留文件的属性
rsync -a /var/lib/mysql /var/xp/
5.如果你就只改了mysql存储路径重启之后会报错
[root@test1 ~]# systemctl restart mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
6.即使改了/var/xp/mysql的selinux属性也依然如此 ,重启之后依然会报错
chcon -R --reference=/var/lib/mysql /var/xp/mysql
[root@test1 ~]# systemctl restart mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
7.解决方法
#安装命令
yum install policycoreutils-python
#把新的 MySQL 数据目录增加到mysqld_db_t 这个 SELinux 类型中
semanage fcontext -a -t mysqld_db_t "/var/xp/mysql(/.*)?"
#使用命令 restorecon 恢复这个数据目录对应的 SELinux 上下文
restorecon -Rv /var/xp/mysql
#用 semanage fcontext 命令的 -l 选项进行检查
semanage fcontext -l |grep mysqld_db_t
8.在重启
[root@test1 ~]# systemctl restart mysqld
[root@test1 ~]#
9.mysql -uroot -p登录如果报一下错误
[root@test1 ~]# mysql -p'密码'
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 '/var/lib/mysql/mysql.sock' (2)
只需在配置文件/etc/my.cfg,添加,在重启即可
[client]
socket=/var/xp/mysql/mysql.sock