二:基于lvm2+复制binlog来实现备份
准备环境:
节点2 IP:172.18.42.111
1、部署节点1
(1)安装MySQL服务,滚动日志并记录
[root@localhost ~]# yum install mysql-server -y
[root@localhost ~]# service mysqld start
[root@localhost ~]# mysql -e "show master status" > binary_file ##对于如何开启二进制日志小白我就不再说了
[root@localhost ~]# cat binary_file
File Position Binlog_Do_DB Binlog_Ignore_DB
mysql-bin.000004 208 ##记录了当前使用的二进制日志以及事件所在二进制日志中的pos
[root@localhost ~]# service mysqld stop ##记录完日志之后把mysql服务关闭
(2)创建lvm快照,并挂载
[root@localhost ~]# pvcreate /dev/sda5 ##创建物理卷
Physical volume "/dev/sda5" successfully created
[root@localhost ~]# vgcreate wxpp /dev/sda5 ##创建卷组,名为“wxpp”
Volume group "wxpp" successfully created
[root@localhost ~]# lvcreate -L +5G -n wxxp1 wxpp ##创建逻辑卷,大小为5G,名为“wxpp1”
Logical volume "wxxp1" created.
[root@localhost ~]# mke2fs -t ext4 /dev/wxpp/wxxp1 ##将逻辑卷格式化为“ext4”的文件系统
[root@localhost ~]# mount /dev/wxpp/wxxp1 /data/ ##将逻辑卷挂载至“/data”目录下
[root@localhost ~]# mkdir /data/mysql ##创建mysql数据库存储数据的目录,注意:此路径一定要和配置文件里的“datadir”指向的路径相同
[root@localhost ~]# chown mysql.mysql /data/mysql ##让mysql用户具有存储数据的权限
[root@localhost ~]# ll /data/
drwxr-xr-x 2 mysql mysql 4096 Jun 12 22:20 /data/mysql
drwx------ 2 root root 16384 Jun 12 22:18 lost+found
[root@localhost ~]# service mysqld start ##启动mysql服务
[root@localhost ~]# ll /data/mysql/ ##生成了大量的日志文件
total 21272
-rw-rw---- 1 mysql mysql 10485760 Jun 12 22:28 ibdata1
-rw-rw---- 1 mysql mysql 5242880 Jun 12 22:28 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 Jun 12 22:24 ib_logfile1
drwx------ 2 mysql mysql 4096 Jun 12 22:25 Mydata
drwx------ 2 mysql mysql 4096 Jun 12 22:24 mysql
-rw-rw---- 1 mysql mysql 19758 Jun 12 22:24 mysql-bin.000001
-rw-rw---- 1 mysql mysql 765307 Jun 12 22:24 mysql-bin.000002
-rw-rw---- 1 mysql mysql 1190 Jun 12 22:28 mysql-bin.000003
-rw-rw---- 1 mysql mysql 106 Jun 12 22:28 mysql-bin.000004
-rw-rw---- 1 mysql mysql 76 Jun 12 22:28 mysql-bin.index
drwx------ 2 mysql mysql 4096 Jun 12 22:24 test
mysql> flush tables with read lock; ##给数据施加读锁,防止数据改变
[root@localhost ~]# lvcreate -L +1G -s -n binary_log /dev/wxpp/wxxp1 ##创建lvm快照,大小为1G,名为“binary_log”
[root@localhost ~]# mount /dev/wxpp/binary_log /mnt/ ##将快照挂载至“/mnt”目录下
[root@localhost ~]# ll /mnt/mysql/
total 21272
-rw-rw---- 1 mysql mysql 10485760 Jun 12 22:28 ibdata1
-rw-rw---- 1 mysql mysql 5242880 Jun 12 22:28 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 Jun 12 22:24 ib_logfile1
drwx------ 2 mysql mysql 4096 Jun 12 22:25 Mydata
drwx------ 2 mysql mysql 4096 Jun 12 22:24 mysql
-rw-rw---- 1 mysql mysql 19758 Jun 12 22:24 mysql-bin.000001
-rw-rw---- 1 mysql mysql 765307 Jun 12 22:24 mysql-bin.000002
-rw-rw---- 1 mysql mysql 1190 Jun 12 22:28 mysql-bin.000003
-rw-rw---- 1 mysql mysql 106 Jun 12 22:28 mysql-bin.000004
-rw-rw---- 1 mysql mysql 76 Jun 12 22:28 mysql-bin.index
drwx------ 2 mysql mysql 4096 Jun 12 22:24 test
mysql> unlock tables; ##释放写锁
(3)生成增量备份
mysql> select * from db1; ##未修改前查看一次数据
+----+-------+------+
| ID | Name Age
+----+-------+------+
| 1 | MaGe 100
| 2 | Lweim 200
| 3 | Wzx 300
| 4 | wxpp 400
+----+-------+------+
mysql> delete from db1 where ID=1;
mysql> select * from db1;
+----+-------+------+
| ID | Name Age
+----+-------+------+
| 2 | Lweim 200
| 3 | Wzx 300
| 4 | wxpp 400
+----+-------+------+
[root@localhost ~]# mysqlbinlog --start-position=208 /data/mysql/mysql-bin.000004 > backup.sql ##生成增量备份文件
2、部署节点2
(1)将节点1快照下“/mnt/mysql“目录下的文件以及增量备份文件发送给节点2;
[root@localhost ~]# scp -r /mnt/mysql/* backup.sql root@172.18.42.111:/data/mysql/
[root@localhost ~]# chown -R mysql.mysql /data/mysql/ ##将权限更改为“mysql”
[root@localhost ~]# ll /data/mysql/
total 21272
-rw-r----- 1 mysql mysql 10485760 May 9 22:33 ibdata1
-rw-r----- 1 mysql mysql 5242880 May 9 22:33 ib_logfile0
-rw-r----- 1 mysql mysql 5242880 May 9 22:33 ib_logfile1
drwx------ 2 mysql mysql 4096 May 9 22:33 Mydata
drwx------ 2 mysql mysql 4096 May 9 22:33 mysql
-rw-r----- 1 mysql mysql 19758 May 9 22:33 mysql-bin.000001
-rw-r----- 1 mysql mysql 765307 May 9 22:33 mysql-bin.000002
-rw-r----- 1 mysql mysql 1190 May 9 22:33 mysql-bin.000003
-rw-r----- 1 mysql mysql 208 May 9 22:33 mysql-bin.000004
-rw-r----- 1 mysql mysql 76 May 9 22:33 mysql-bin.index
drwx------ 2 mysql mysql 4096 May 9 22:33 test
(2)启动mysql服务,并查看其数据库
[root@localhost ~]# service mysqld start
mysql> select * from db1; ##并不是修改之后的数据
+----+-------+------+
| ID | Name Age
+----+-------+------+
| 1 | MaGe 100
| 2 | Lweim 200
| 3 | Wzx 300
| 4 | wxpp 400
+----+-------+------+
(3)假设节点1因为人为误操作导致数据库崩了,此时让节点二导入增量备份“backup.sql”即可
[root@localhost ~]# mysql < backup.sql
mysql> select * from db1;
+----+-------+------+
| ID | Name Age
+----+-------+------+
| 2 | Lweim 200
| 3 | Wzx 300
| 4 | wxpp 400
+----+-------+------+
(4)移除lvm快照
[root@localhost ~]# umount /mnt
[root@localhost ~]# lvremove /dev/wxpp/binary_log
Logical volume "binary_log" successfully removed
当有大量数据库需要复制时,此时使用快照是最快、最有效的方式,防止对数据库施加过长时间的读锁,以免造成不必要的损失