MySQL故障演习
接上次的 MySQL定时备份
该次实验主要是练习在MySQL数据库发生误删等意外情况下,利用全量备份文件和增量备份文件恢复数据。
1. 实验环境
-- 创建数据库
create database db1 character set utf8 collate utf8_general_ci;
create table db1.author (
id int(11) primary key auto_increment,
name varchar(20) comment '姓名',
phone varchar(20) comment '电话'
) comment '作者表';
create table db1.book (
id int(11) primary key auto_increment,
name varchar(100) comment '名称',
price decimal(10,2) comment '售价'
) comment '图书表';
insert into db1.author(name, phone) values
('孔子', '111'),
('亚瑟.叔本华', '222');
insert into db1.book(name, price) values ('论语', 19.9);
此时的author表和book表记录是这样的:
mysql> select * from db1.author;
+----+-------------+-------+
| id | name | phone |
+----+-------------+-------+
| 1 | 孔子 | 111 |
| 2 | 亚瑟.叔本华 | 222 |
+----+-------------+-------+
3 rows in set (0.00 sec)
mysql> select * from db1.book;
+----+------+-------+
| id | name | price |
+----+------+-------+
| 1 | 论语 | 19.90 |
+----+------+-------+
1 row in set (0.00 sec)
2. 每周日全量备份
2.1 凌晨3点全量备份
全量备份文件:db1_20181216.sql.tgz
2.2 数据库操作
insert into db1.author(name, phone) values ('刘慈欣', '333');
此时的author表记录是这样的:
mysql> select * from db1.author;
+----+-------------+-------+
| id | name | phone |
+----+-------------+-------+
| 1 | 孔子 | 111 |
| 2 | 亚瑟.叔本华 | 222 |
| 3 | 刘慈欣 | 333 |
+----+-------------+-------+
3 rows in set (0.00 sec)
3. 周一操作
3.1 周一凌晨3点增量备份
增量备份文件:mysql-bin.000026
3.2 数据库操作
-- 插入数据
insert into db1.book(name, price) values ('三体', 65);
-- 更新数据
update db1.author set phone='001' where name='孔子';
此时的author表和book表记录是这样的:
mysql> select * from db1.author;
+----+-------------+-------+
| id | name | phone |
+----+-------------+-------+
| 1 | 孔子 | 001 |
| 2 | 亚瑟.叔本华 | 222 |
| 3 | 刘慈欣 | 333 |
+----+-------------+-------+
3 rows in set (0.00 sec)
mysql> select * from db1.book;
+----+------+-------+
| id | name | price |
+----+------+-------+
| 1 | 论语 | 19.90 |
| 2 | 三体 | 65.00 |
+----+------+-------+
2 rows in set (0.00 sec)
3.3 误操作
一不小心执行了 delete from db1.author
,author表的数据全都清空了,这可怎么办啊!如果有撤回操作就好了。
而备份恢复就像是撤回操作
此时的author表记录是这样的:
mysql> select * from db1.author;
Empty set (0.00 sec)
mysql> select * from db1.book;
+----+------+-------+
| id | name | price |
+----+------+-------+
| 1 | 论语 | 19.90 |
| 2 | 三体 | 65.00 |
+----+------+-------+
2 rows in set (0.00 sec)
4. 备份恢复
在全量备份后,我们进行了n条数据库操作,这些操作都保存在今天(周一)凌晨的增量备份和当前 binlog 里。如下:
操作1
操作2
操作3(误操作)
恢复的时候,我们先将数据库恢复到原始数据状态,然后再执行一遍增量备份和binlog里的操作(特别注意:错误操作除外)就行了。
备份恢复的时候干脆先停止当前系统的使用(应该有更好的解决方法,既不影响系统的使用,也能完成备份恢复,在用户无知觉的情况下恢复数据)
(1) 增量备份
执行增量备份的脚本
/root/bash/Mysql-DailyBak.sh
增量备份文件:mysql-bin.000027
(2) 恢复备份前全量备份数据库
该步骤完全是为了以防万一,和要进行的备份恢复没啥关系。
注意执行以下命令备份:
/usr/local/mysql/bin/mysqldump -uroot -ptencns152 --quick --events --databases db1 --single-transaction > db1_20181217.sql
(3) 恢复全量备份
mysql> source /home/mysql/backup/db1_20181216.sql
mysql> select * from db1.author;
+----+-------------+-------+
| id | name | phone |
+----+-------------+-------+
| 1 | 孔子 | 111 |
| 2 | 亚瑟.叔本华 | 222 |
+----+-------------+-------+
2 rows in set (0.00 sec)
mysql> select * from db1.book;
+----+------+-------+
| id | name | price |
+----+------+-------+
| 1 | 论语 | 19.90 |
+----+------+-------+
1 row in set (0.00 sec)
(4). 恢复增量备份
增量备份共有2个:mysql-bin.000026 和 mysql-bin.000027
(4.1) 恢复 mysql-bin.000026 的操作
/usr/local/mysql/bin/mysqlbinlog --no-defaults /home/mysql/backup/daily/mysql-bin.000026 | mysql -uroot -p db1
此时的数据表是这样的:
mysql> select * from db1.author;
+----+-------------+-------+
| id | name | phone |
+----+-------------+-------+
| 1 | 孔子 | 111 |
| 2 | 亚瑟.叔本华 | 222 |
| 3 | 刘慈欣 | 333 |
+----+-------------+-------+
3 rows in set (0.00 sec)
mysql> select * from db1.book;
+----+------+-------+
| id | name | price |
+----+------+-------+
| 1 | 论语 | 19.90 |
+----+------+-------+
1 row in set (0.00 sec)
(4.2) 恢复 mysql-bin.000027 的操作(除误操作外)
# 查看 mysql-bin.000027
/usr/local/mysql/bin/mysqlbinlog --base64-output=decode-rows -v /home/mysql/backup/daily/mysql-bin.000027 > mysql-bin.000027.txt
通过观察 mysql-bin.000027.txt
....
/*!*/;
# at 842
#181218 11:17:24 server id 152 end_log_pos 896 CRC32 0xeff9a750 Table_map: `db1`.`author` mapped to number 3960
# at 896
#181218 11:17:24 server id 152 end_log_pos 992 CRC32 0x87dc5c6f Delete_rows: table id 3960 flags: STMT_END_F
### DELETE FROM `db1`.`author`
### WHERE
### @1=1
### @2='孔子'
### @3='001'
### DELETE FROM `db1`.`author`
### WHERE
### @1=2
### @2='亚瑟.叔本华'
### @3='222'
### DELETE FROM `db1`.`author`
### WHERE
### @1=3
### @2='刘慈欣'
### @3='333'
# at 992
#181218 11:17:24 server id 152 end_log_pos 1023 CRC32 0x8c16a4e2 Xid = 73792
COMMIT/*!*/;
# at 1023
....
可以看出,在位置 896 到 992 之间执行了误操作,删除了author的所有数据。
所以我们在恢复时,跳过这个位置范围即可。
/usr/local/mysql/bin/mysqlbinlog --stop-position=896 --database=db1 /home/mysql/backup/daily/mysql-bin.000027 | mysql -uroot -p db1
/usr/local/mysql/bin/mysqlbinlog --start-position=992 --database=db1 /home/mysql/backup/daily/mysql-bin.000027 | mysql -uroot -p db1
备份恢复成功 _
查询结果如下:
mysql> select * from db1.author;
+----+-------------+-------+
| id | name | phone |
+----+-------------+-------+
| 1 | 孔子 | 001 |
| 2 | 亚瑟.叔本华 | 222 |
| 3 | 刘慈欣 | 333 |
+----+-------------+-------+
3 rows in set (0.00 sec)
mysql> select * from db1.book;
+----+------+-------+
| id | name | price |
+----+------+-------+
| 1 | 论语 | 19.90 |
| 2 | 三体 | 65.00 |
+----+------+-------+
2 rows in set (0.00 sec)