centos9更换mysql数据目录
作者:admin 来源:原创 2024/1/19 12:41:38

    在已设置mysql远程登录密码前提下更换数据目录,更换后,无法通过命令更新密码(未解决)

一、停止服务

    停止所有使用到数据库的相关服务

二、停止mysql数据库服务

systemctl stop mysqld

三、备份配置文件

cp /etc/my.cnf /etc/my.cnf.bak

四、使用rsync指令把原mysql数据复制到新路径,此可以保留文件的属性

rsync -a /var/lib/mysql /home

五、修改配置文件
nano /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.3/en/server-configuration-defaults.html

[mysqld]
lower_case_table_names=1
max_connections = 2000
max_connect_errors = 1000
#
# 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.3/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/home/mysql
socket=/home/mysql/mysql.sock

log-error=/home/mysql/mysqld.log
pid-file=/home/mysql/mysqld.pid

    此时直接启动数据库会提示权限不足

    解决方法:

#把新的 MySQL 数据目录增加到mysqld_db_t 这个 SELinux 类型中
semanage fcontext -a -t mysqld_db_t "/home/mysql(/.*)?"
 
#使用命令 restorecon 恢复这个数据目录对应的 SELinux 上下文
restorecon -Rv /home/mysql
 
#用 semanage fcontext 命令的 -l 选项进行检查
semanage fcontext -l |grep mysqld_db_t

六、启动数据库服务

systemctl start mysqld

七、登录mysql

mysql -u root -p
     提示:Could not create unix socket lock file /home/mysql.sock.lock.

    

    尝试解决:在配置文件/etc/my.cfg,添加

    nano /etc/my.cfg

[client]
socket=/home/mysql/mysql.sock
    

    重启服务后失败!不过由于在转移数据库目录前已经设置了账号密码,故使用Navicat Premium工具连接成功。

    其他备忘:

    执行setenforce 0只是暂时关闭selinux,系统重启后,权限会恢复。

    selinux配置文件目录:/etc/selinux/config

    

称      呼:
联系方式:
您的评论:
技术支持:l.w.dong@qq.com www.luweidong.cn
广州市   wx:lwdred
Copyright © 2014 三味书屋 All Rights Reserved
技术支持:l.w.dong@qq.com  sitemap xml  sitemap html

粤公网安备44010602011869号

粤ICP备13031080号-1