0%

LVM物理卷迁移 热迁移 在线迁移

今天到公司一看,某业务的MySQL数据库把磁盘吃满了,当初才创建了16G的虚拟磁盘(因为测试成功了,直接当生产用了),已经占用了15G,火速添加了一块从固态硬盘存储上划分的128G虚拟磁盘,不停止生产环境的情况下,做了在线迁移,把旧的16G回收了。

下面使用虚拟机演示如何LVM热迁移

操作系统: Centos8.2

当前磁盘状态

有个旧的20G硬盘放着数据库文件,现在新增了一块大的30G硬盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@lvmtest-001 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda3 17G 2.0G 16G 12% /
/dev/sda1 976M 121M 789M 14% /boot
tmpfs 376M 0 376M 0% /run/user/0
/dev/mapper/mariadb-mariadb_lv 20G 4.3G 16G 22% /var/lib/mysql
[root@lvmtest-001 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17G 0 part /
sdb 8:16 0 20G 0 disk
└─mariadb-mariadb_lv 253:0 0 20G 0 lvm /var/lib/mysql
sdc 8:32 0 30G 0 disk

现在要把数据库分区/dev/mapper/mariadb-mariadb_lv迁移到新硬盘/dev/sdc上

创建物理卷,加入卷组

新磁盘加入旧磁盘的同一个卷组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@lvmtest-001 ~]# pvcreate /dev/sdc 
Physical volume "/dev/sdc" successfully created.
[root@lvmtest-001 ~]# vgextend mariadb /dev/sdc
Volume group "mariadb" successfully extended
[root@lvmtest-001 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb
VG Name mariadb
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 5119
Free PE 0
Allocated PE 5119
PV UUID VOOIk6-5CVx-1Xoq-yeXi-SGmX-DqCK-gfHrd0

--- Physical volume ---
PV Name /dev/sdc
VG Name mariadb
PV Size 30.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 7679
Free PE 7679
Allocated PE 0
PV UUID dbxhnC-aamt-SHtN-vXpX-tMOK-Tnjx-U4MPeu

迁移数据库分区到新的大容量磁盘上

数据越大迁移越久,耐心等待,这样迁移不需要停业务。

1
2
3
[root@lvmtest-001 ~]# pvmove /dev/sdb /dev/sdc
/dev/sdb: Moved: 0.18%
/dev/sdb: Moved: 100.00%

检查/dev/mapper/mariadb-mariadb_lv分区所在的磁盘

1
2
3
[root@lvmtest-001 ~]# lvs -o +devices
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert Devices
mariadb_lv mariadb -wi-a----- <20.00g /dev/sdc(0)

可以看到分区已经移动到/dev/sdc上了,可以进行下一步操作

VG卷弹出旧物理卷,删除旧物理卷

1
2
3
4
[root@lvmtest-001 ~]# vgreduce mariadb /dev/sdb
Removed "/dev/sdb" from volume group "mariadb"
[root@lvmtest-001 ~]# pvremove /dev/sdb
Labels on physical volume "/dev/sdb" successfully wiped.

扩容迁移后的分区

新的物理卷是30G,扩容原来的逻辑卷

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@lvmtest-001 ~]# lvextend /dev/mariadb/mariadb_lv /dev/sdc 
Size of logical volume mariadb/mariadb_lv changed from <20.00 GiB (5119 extents) to <30.00 GiB (7679 extents).
Logical volume mariadb/mariadb_lv successfully resized.
[root@lvmtest-001 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/mariadb/mariadb_lv
LV Name mariadb_lv
VG Name mariadb
LV UUID VGEx5r-lAb2-L7ym-xJpe-YjMZ-Lkfw-AyY5DF
LV Write Access read/write
LV Creation host, time lvmtest-001, 2020-09-24 11:08:01 +0800
LV Status available
# open 0
LV Size <30.00 GiB
Current LE 7679
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

扩容xfs分区

1
2
3
4
5
6
7
8
9
10
11
12
[root@lvmtest-001 ~]# xfs_growfs /var/lib/mysql/
meta-data=/dev/mapper/mariadb-mariadb_lv isize=512 agcount=4, agsize=1310464 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=5241856, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5241856 to 7863296

检查服务状态

1
2
[root@lvmtest-001 lib]# systemctl is-active mariadb.service 
active

检查迁移后的磁盘状态

1
2
3
4
5
6
7
8
9
10
[root@lvmtest-001 lib]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17G 0 part /
sdb 8:16 0 20G 0 disk
sdc 8:32 0 30G 0 disk
└─mariadb-mariadb_lv 253:0 0 30G 0 lvm /var/lib/mysql

数据库分区成功迁移到新硬盘,且业务没有中断。迁移之后,逻辑卷的UUID不会改变,fstab文件也不用修改,不会影响业务。/dev/sdb已经没有使用,可以回收磁盘空间。

如果觉得文章对您有帮助,可以打赏一下我吗