前提:
数据文件要在逻辑卷上
此逻辑卷所在的卷组必须有足够空间使用快照卷
数据文件和事物日志日志要在同一个逻辑卷上
一,数据迁移到逻辑卷上
在磁盘nvme0n2 上做数据迁移
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 4.4G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─centos-root 253:0 0 10G 0 lvm /
├─centos-swap 253:1 0 4G 0 lvm [SWAP]
└─centos-home 253:2 0 5G 0 lvm /home
nvme0n2 259:3 0 10G 0 disk
1.创建一个逻辑卷
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 4.4G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─centos-root 253:0 0 10G 0 lvm /
├─centos-swap 253:1 0 4G 0 lvm [SWAP]
└─centos-home 253:2 0 5G 0 lvm /home
nvme0n2 259:3 0 10G 0 disk
创建物理卷(PV)
[root@localhost ~]# pvcreate /dev/nvme0n2
Physical volume "/dev/nvme0n2" successfully created.
创建卷组(VG)
[root@localhost ~]# vgcreate mysqlvg /dev/nvme0n2
Volume group "mysqlvg" successfully created
创建逻辑卷(LV)
[root@localhost ~]# lvcreate -n lv_mysql -L 5G mysqlvg
Logical volume "lv_mysql" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- <5.00g
root centos -wi-ao---- 10.00g
swap centos -wi-ao---- 4.00g
lv_mysql mysqlvg -wi-a----- 5.00g
格式化
[root@localhost ~]# mkfs.ext4 /dev/mysqlvg/lv_mysql
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
2.将当前的mysql数据迁移到逻辑卷上(当前mysql数据集在/usr/local/mysql/)
(1)先停止mysql应用
[root@localhost ~]# systemctl stop mysql
(2)备份所有文件到指定的地方
tar [参数] 文件名 需要打包的文件...
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# ll
total 276
drwxr-xr-x. 2 mysql mysql 4096 Mar 19 07:11 bin
drwxr-x---. 8 mysql mysql 4096 Mar 22 08:48 data
drwxr-xr-x. 2 mysql mysql 55 Mar 19 07:11 docs
drwxr-xr-x. 3 mysql mysql 4096 Mar 19 07:10 include
drwxr-xr-x. 5 mysql mysql 230 Mar 19 07:11 lib
-rw-r--r--. 1 mysql mysql 259199 Sep 7 2021 LICENSE
drwxr-xr-x. 4 mysql mysql 30 Mar 19 07:11 man
-rw-r--r--. 1 mysql mysql 566 Sep 7 2021 README
drwxr-xr-x. 28 mysql mysql 4096 Mar 19 07:11 share
drwxr-xr-x. 2 mysql mysql 90 Mar 19 07:11 support-files
#注意要切目录cd /usr/local/mysql
[root@localhost mysql]# tar czf /backup/db/mysql.tar.gz *
(3)挂载逻辑卷到当前mysql的数据目录里(一挂载/usr/local/mysql/就空了,所以要备份)
mount命令[-参数] [设备名称] [挂载点]
说明:
[挂载点]必须是一个已经存在的目录,这个目录可以不为空,但挂载后这个目录下以前的内容将不可用,umount以后会恢复正常。
[设备名称] 可以是一个分区,一个usb设备,光驱,软盘,网络共享等。
[root@localhost mysql]# cd
[root@localhost ~]# mount /dev/mysqlvg/lv_mysql /usr/local/mysql
# /usr/local/mysql已经空了
[root@localhost ~]# ls /usr/local/mysql
lost+found
(4)将刚刚备份(tar)的数据解压到数据目录里(/usr/local/mysql)
[root@localhost ~]# tar xf /backup/db/mysql.tar.gz -C /usr/local/mysql
[root@localhost ~]# ls /usr/local/mysql/
bin docs lib lost+found README support-files
data include LICENSE man share
(5)启动数据库
[root@localhost ~]# service mysqld start
此处启动失败原因/usr/local/mysql数据目录的权限变成了root,
更改权限重新启动
[root@localhost ~]# chown mysql. -R /data/DB/ && service mysqld start
二.快照备份数据库
1.给数据库加读锁(只能读)
#加读锁之后就只能读了
mysql [(none)]>flush table with read lock;
Query OK, 0 rows affected (0.01 sec)
mysql [(none)]>create database it;
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
2.给mysql的数据库所在的逻辑卷创建快照
[root@localhost ~]# lvcreate -n mysql_s -L 4G -s /dev/mysqlvg/lv_mysql
Logical volume "mysql_s" created.
3.解锁数据库
mysql [(none)]>unlock tables;
Query OK, 0 rows affected (0.00 sec)
4.将快照挂载到临时目录里
[root@localhost ~]# mkdir /mnt/mysql
[root@localhost ~]# mount -o nouuid /dev/mysqlvg/lv_mysql /mnt/mysql/
5.备份数据
[root@localhost ~]# mkdir /dbback
[root@localhost ~]# yum install -y rsync
[root@localhost ~]# rsync -av /mnt/mysql/ /dbback/
sending incremental file list
./
LICENSE
README
bin/
bin/innochecksum
bin/lz4_decompress
...
[root@localhost backup]# cd /dbback/
[root@localhost dbback]# ll
total 276
drwxr-xr-x. 2 mysql mysql 4096 Mar 19 07:11 bin
drwxr-x---. 8 mysql mysql 4096 Mar 22 09:02 data
drwxr-xr-x. 2 mysql mysql 55 Mar 19 07:11 docs
drwxr-xr-x. 3 mysql mysql 4096 Mar 19 07:10 include
drwxr-xr-x. 5 mysql mysql 230 Mar 19 07:11 lib
-rw-r--r--. 1 mysql mysql 259199 Sep 7 2021 LICENSE
drwx------. 2 root root 6 Mar 22 08:47 lost+found
drwxr-xr-x. 4 mysql mysql 30 Mar 19 07:11 man
-rw-r--r--. 1 mysql mysql 566 Sep 7 2021 README
drwxr-xr-x. 28 mysql mysql 4096 Mar 19 07:11 share
drwxr-xr-x. 2 mysql mysql 90 Mar 19 07:11 support-files
检查备份的数据是否有效
#修改配置文件
vim /etc/my.cnf
datedir=/dbback/data
basesdir=/dbback
#接着重启mysql服务,进入数据库检查原有数据
systemctl restart mysql
6.卸载快照并删除
[root@localhost dbback]# umount /mnt/mysql/
[root@localhost dbback]# lvremove /dev/mysqlvg/mysql_s
Do you really want to remove active logical volume mysqlvg/mysql_s? [y/n]: y
Logical volume "mysql_s" successfully removed