1.前提
通过yum源安装的MaraDB数据库,其数据存储目录都是默认的/var/lib/mysql/
,但这个目录所在分区的空间一般都比较小,因此,建议使用空间更大的分区上的目录来作为存储目录。
本文中使用的数据库环境为:
操作系统 | 数据库 |
---|---|
CentOS 7.5 | MariaDB 10.2 |
2.关闭数据库
迁移存储目录中的数据时,建议先关闭数据库,不先关闭再迁移的话,可能会出现一些奇奇怪怪的问题,解决起来浪费时间。下面列举两种可能用到的方式:
方式一:已知mariadb已注册为服务,使用systemctl stop mariadb
命令;
方式二:已知mariadb的端口,使用netstat -nlp |grep 端口号 | xargs kill -15
命令
3.迁移数据库存储目录
目标目录:/data/
迁移命令:cp -rap /var/lib/mysql/ /data/
注意:迁移完后,再检查一遍迁移后的目录的所属用户是否是mysql用户,不是的话,则需要将用户设置成mysql,否则后面启动数据库失败的。
4.修改配置文件
mariadb的配置文件默认路径为/etc/my.cnf
。使用vi /etc/my.cnf
命令修改配置文件:
修改前:
#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include *.cnf from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
port=3306
character-set-server=utf8
event_scheduler=ON
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
修改后:
#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include *.cnf from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
port=3306
character-set-server=utf8
event_scheduler=ON
datadir=/data/mysql
socket=/data/mysql/mysql.sock
[client]
default-character-set=utf8
socket=/data/mysql/mysql.sock
[mysql]
default-character-set=utf8
注意:1. [mysqld]
下的datadir
改成迁移后的目录,没有则自行添加。
2. [mysqld]
和 [client]
下的socket
改成“目标目录+/mysql/mysql.sock
”
5.启动数据库
systemctl daemon-reload
systemctl start mariadb
6.特殊情况
若存储目录需要迁移到/home/
目录中,则需要到/etc/systemd/system/mariadb.service.d/migrated-from-my.cnf-settings.conf
中,
将ProtectHome=true改成ProtectHome=false,没有的话,直接加上就行了。
[root@localhost mysql]# cat /etc/systemd/system/mariadb.service.d/migrated-from-my.cnf-settings.conf
# converted using /usr/bin/mariadb-service-convert
#
[Service]
ProtectHome=false