CentOS 7.6下MySQL 5.7.25数据文件路径迁移指南
采用rpm方式完成MySQL安装后,其数据文件的默认存放位置位于/var/lib/mysql,但该根目录分区通常不适合承载大量数据存储任务。
原始数据目录为:/var/lib/mysql
期望迁移的目标目录为:/home/mysql_data/mysql
操作系统环境为:centos7.6
使用的MySQL版本为:5.7.25
具体操作流程如下:
第一步,建立新的数据存放目录。
mkdir -p /home/mysql_data
第二步,暂停MySQL数据库服务。
systemctl stop mysqld
第三步,将原有数据完整迁移至新目录。
cp -arp /var/lib/mysql /home/mysql_data
第四步,编辑配置文件/etc/my.cnf进行路径替换。
重点更新datadir和socket两项配置,使其指向新的目标路径。
datadir=/home/mysql_data/mysql
socket=/home/mysql_data/mysql/mysql.sock
注意事项:
systemctl start mysqld
常见问题一:
可能出现以下报错信息:
2026-12-22T08:32:42.430564Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2026-12-22T08:32:42.430599Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2026-12-22T08:32:42.430616Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2026-12-22T08:32:42.430898Z 0 [ERROR] InnoDB: The innodb_temporary data file 'ibtmp1' must be writable 2026-12-22T08:32:42.430923Z 0 [ERROR] InnoDB: The innodb_temporary data file 'ibtmp1' must be writable 2026-12-22T08:32:42.430936Z 0 [ERROR] InnoDB: Could not create the shared innodb_temporary. 2026-12-22T08:32:42.430952Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error 2026-12-22T08:32:43.038973Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation. 2026-12-22T08:32:43.039021Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 2026-12-22T08:32:43.039037Z 0 [ERROR] Plugin 'InnoDB' init function returned error. 2026-12-22T08:32:43.039046Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed. 2026-12-22T08:32:43.039056Z 0 [ERROR] Failed to initialize builtin plugins. 2026-12-22T08:32:43.039063Z 0 [ERROR] Aborting
针对上述问题的解决方法:
需要执行相关命令以调整系统安全策略。
setenforce 1
随后再次尝试启动服务。
常见问题二:
连接报错内容如下:
该问题的解决思路:
需在配置文件my.cnf中新增或修正以下设置项:
[client]
socket = /home/mysql_data/mysql/mysql.sock
总结
以上内容介绍了在Linux环境下更改MySQL数据库数据文件存储路径的具体操作,希望能提供有效帮助。如有任何疑问欢迎留言,作者将及时回复。感谢对本站的支持! 若本文对您有所帮助,欢迎转载,请注明出处,谢谢!