丰收在望 发表于 2023-4-23 12:45:01

MySQL 移动数据目录后启动失败


[*]GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。
[*]GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。
[*]作者: 王权富贵
[*]文章来源:GreatSQL社区投稿
背景概述

由于安装数据库时将MySQL的数据目录放在了根目录下,现在存储空间不足,想通过mv将数据目录移动到其他目录下,但将数据目录移动到其他数据目录后,启动数据库失败。
问题复现

本次测试基于 MySQL 8.0.31
1.关闭数据库
mysql> shutdown;
Query OK, 0 rows affected (0.02 sec)2.查看当前数据目录所在位置

shell> pwd
/mysql803.通过mv移动整个MySQL数据目录到其他目录

shell>mv /mysql80 /data
shell>cd /data/mysql80/svr
shell>ln -s mysql-8.0.31-linux-glibc2.12-x86_64 mysql4.修改属主属组

shell> chown -R mysql.mysql /data5.修改配置文件中数据目录的地址

shell> sed -i 's#/mysql80#/data/mysql80#g' my5001.cnf6.启动数据库

shell> /data/mysql80/svr/mysql/bin/mysqld_safe \
--defaults-file=/data/mysql80/conf/my5001.cnf --user=mysql &此时启动数据库失败,错误日志报错如下:
mysqld: File '/mysql80/dbdata/data5001/log/binlog.000012' not found (OS errno 2 - No such file or directory)
2023-02-27T10:38:09.240576+08:00 0 Could not open log file.
2023-02-27T10:38:09.240657+08:00 0 Can't init tc log
2023-02-27T10:38:09.240718+08:00 0 Aborting
2023-02-27T10:38:10.548605+08:00 0 /data/mysql80/svr/mysql/bin/mysqld: Shutdown complete (mysqld 8.0.31)MySQL Community Server - GPL.这里报错显示找不到binlog文件,并且报错显示的binlog的目录还是之前的,但是配置文件中的目录已经修改
shell> grep 'log-bin' my5001.cnf
log-bin=/data/mysql80/dbdata/data5001/log/binlog
log-bin-trust-function-creators7.问题解决

最后通过查找发现 binlog.index 文件中存放着每个binlog文件的绝对路径地址,这里的路径还是之前路径,内容如下:
shell> cat binlog.index
/mysql80/dbdata/data5001/log/binlog.000001
/mysql80/dbdata/data5001/log/binlog.000002
/mysql80/dbdata/data5001/log/binlog.000003
/mysql80/dbdata/data5001/log/binlog.000004
/mysql80/dbdata/data5001/log/binlog.000005
/mysql80/dbdata/data5001/log/binlog.000006
/mysql80/dbdata/data5001/log/binlog.000007
/mysql80/dbdata/data5001/log/binlog.000008
/mysql80/dbdata/data5001/log/binlog.000009
/mysql80/dbdata/data5001/log/binlog.000010
/mysql80/dbdata/data5001/log/binlog.000011
/mysql80/dbdata/data5001/log/binlog.000012修改binlog.index文件中binlog的绝对路径:
shell> sed -i 's#/mysql80#/data/mysql80#g' binlog.index
shell> cat binlog.index
/data/mysql80/dbdata/data5001/log/binlog.000001
/data/mysql80/dbdata/data5001/log/binlog.000002
/data/mysql80/dbdata/data5001/log/binlog.000003
/data/mysql80/dbdata/data5001/log/binlog.000004
/data/mysql80/dbdata/data5001/log/binlog.000005
/data/mysql80/dbdata/data5001/log/binlog.000006
/data/mysql80/dbdata/data5001/log/binlog.000007
/data/mysql80/dbdata/data5001/log/binlog.000008
/data/mysql80/dbdata/data5001/log/binlog.000009
/data/mysql80/dbdata/data5001/log/binlog.000010
/data/mysql80/dbdata/data5001/log/binlog.000011
/data/mysql80/dbdata/data5001/log/binlog.0000128.启动数据库

shell> /data/mysql80/svr/mysql/bin/mysqld_safe
--defaults-file=/data/mysql80/conf/my5001.cnf --user=mysql &数据库启动成功。
9.作为从节点

需要注意的是,如果该实例还作为其他实例的从节点,还需要设置 relaylog.index 文件中relay log的绝对路径,否则会报如下错误: 错误日志报错:
2023-02-27T15:56:55.224372+08:00 0 log /mysql80/dbdata/data5002/log/relaylog.000002 listed in the index, but failed to stat.
2023-02-27T15:56:55.224422+08:00 0 Error counting relay log space.
2023-02-27T15:56:55.226571+08:00 0 Slave: Failed to initialize the master info structure for channel ''; its record may still be present in 'mysql.slave_master_info' table, consider deleting it.
2023-02-27T15:56:55.226622+08:00 0 Failed to create or recover replication info repositories.执行 start replica 时也会报错:
# 客户端报错
mysql> start replica;
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository

# error log报错
2023-02-27T15:57:53.858798+08:00 8 Slave SQL for channel '': Slave failed to initialize relay log info structure from the repository, Error_code: MY-01312410.问题解决

修改 relaylog.index 文件中relay log的绝对路径
sed -i 's#/mysql80#/data/mysql80#g' relaylog.index重新启动数据库,并启动主从复制
# 重启实例
mysql> restart;

# 启动主从复制
mysql> start replica;此时主从复制恢复正常。
总结


[*]搭建数据库前应提前规划并确定存储位置及磁盘大小,以及之后数据量增长情况,避免磁盘空间不足时导致数据库不能正常使用。
[*]当我们手动进行数据目录的移动时,不仅需要修改配置文件中数据目录的路径,还需要修改 binlog.index relaylog.index 文件中binlog及relaylog的绝地路径,否则启动时可能出报错,并且不能正常建立主从关系。

Enjoy GreatSQL
来源:https://www.cnblogs.com/greatsql/p/17345624.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: MySQL 移动数据目录后启动失败