binlog日志非常关键,它记录了所有的DDL和DML的数据增删改操作,使用binlog日志可以恢复数据,做主从的复制。所以,无论是开发环境还是测试环境,一定都要开启binlog功能。
下面进入主题,在本地环境模拟生产数据库故障,利用全量备份和binlog日志恢复数据库的过程。
1、测试环境信息:数据库hse_whj,测试表whj_a,表初始数据量是10条。
MariaDB [hse_whj]> show tables;
+-------------------+
| Tables_in_hse_whj |
+-------------------+
| t_pm_factory |
| whj_a |
+-------------------+
2 rows in set (0.00 sec)
MariaDB [hse_whj]> select count(1) from whj_a;
+----------+
| count(1) |
+----------+
| 10 |
+----------+
1 row in set (0.03 sec)
MariaDB [hse_whj]>
2、对hse_whj数据库做一个全量备份,备份工具使用mysqldump。备份文件存放路径是根目录下的backup_whj。
[root@centos007 /]# mysqldump -uroot -p --single-transaction --flush-logs --master_data=2 -E -R -B hse_whj > ./backup_whj/hse_whj_full.sql
Enter password:
[root@centos007 /]# cd backup_whj
[root@centos007 backup_whj]# ls
hse_whj_full.sql
[root@centos007 backup_whj]#
mysqldump参数说明:(1)--single-transaction 获取备份的一致性,只对innodb引擎有效,但不能隔离DDL操作
(2)--flush-logs 开始备份前刷新日志
(3)--master_data 把binlog的位置和文件名追加到输出文件中。如果为1就会输出CHANGE MASTER 命令;如果为2就会输出的CHANGE MASTER命令前添加注释信息
(4)-E 指定备份事件
(5)-R 指定备份存储过程及自定义函数
(6)-B 指定备份的数据库
3、向whj_a表里新增1条数据,这时表数据量是11条,而且这条数据是在备份结束之后新增的。
MariaDB [hse_whj]> INSERT INTO whj_a
-> VALUES(11);
Query OK, 1 row affected (0.10 sec)
MariaDB [hse_whj]> select count(1) from whj_a;
+----------+
| count(1) |
+----------+
| 11 |
+----------+
1 row in set (0.01 sec)
MariaDB [hse_whj]>
4、模拟事故,删除数据hse_whj。
MariaDB [hse_whj]> drop database hse_whj;
Query OK, 2 rows affected (0.32 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| hse01 |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.03 sec)
MariaDB [(none)]>
查看备份后新增的binlog文件
[root@centos007 backup_whj]# ls
hse_whj_full.sql
[root@centos007 backup_whj]# grep CHANGE hse_whj_full.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='master-log.000038', MASTER_LOG_POS=403;
[root@centos007 backup_whj]#
5、这时候不要慌张,先查看备份后新增的和当前的binlog日志,记录事故pos点。
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000038 | 708 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> show binlog events in 'master-log.000038'\G
*************************** 1. row ***************************
Log_name: master-log.000038
Pos: 4
Event_type: Format_desc
Server_id: 7
End_log_pos: 256
Info: Server ver: 10.2.14-MariaDB-log, Binlog ver: 4
*************************** 2. row ***************************
Log_name: master-log.000038
Pos: 256
Event_type: Gtid_list
Server_id: 7
End_log_pos: 315
Info: [0-8-3,0-7-80]
*************************** 3. row ***************************
Log_name: master-log.000038
Pos: 315
Event_type: Binlog_checkpoint
Server_id: 7
End_log_pos: 359
Info: master-log.000037
*************************** 4. row ***************************
Log_name: master-log.000038
Pos: 359
Event_type: Binlog_checkpoint
Server_id: 7
End_log_pos: 403
Info: master-log.000038
*************************** 5. row ***************************
Log_name: master-log.000038
Pos: 403
Event_type: Gtid
Server_id: 7
End_log_pos: 445
Info: BEGIN GTID 0-7-81
*************************** 6. row ***************************
Log_name: master-log.000038
Pos: 445
Event_type: Query
Server_id: 7
End_log_pos: 544
Info: use `hse_whj`; INSERT INTO whj_a
VALUES(11)
*************************** 7. row ***************************
Log_name: master-log.000038
Pos: 544
Event_type: Xid
Server_id: 7
End_log_pos: 575
Info: COMMIT /* xid=1083 */
*************************** 8. row ***************************
Log_name: master-log.000038
Pos: 575
Event_type: Gtid
Server_id: 7
End_log_pos: 617
Info: GTID 0-7-82
*************************** 9. row ***************************
Log_name: master-log.000038
Pos: 617
Event_type: Query
Server_id: 7
End_log_pos: 708
Info: drop database hse_whj
9 rows in set (0.01 sec)
MariaDB [(none)]>
通过读取日志发现出现问题的pos是617到708之间。语句在info行可以看到。
6、移动备份后新产生的binlog日志,并转成可以编辑的sql文件,编辑sql文件去除事故语句drop database hse_whj
[root@centos007 /]# cp ./var/lib/mysql/master-log.000038 ./backup_whj/master-log.000038
[root@centos007 /]# cd backup_whj
[root@centos007 backup_whj]# ls
hse_whj_full.sql master-log.000038
[root@centos007 backup_whj]# mysqlbinlog -d hse_whj master-log.000038 > master-log.sql
[root@centos007 backup_whj]# ls
hse_whj_full.sql master-log.000038 master-log.sql
[root@centos007 backup_whj]# vi master-log.sql
7、先恢复全量备份,在恢复全量备份之前要停止记录binlog,可以修改配置文件,重启数据库服务。避免恢复过程记录到binlog,对日志造成干扰。
[root@centos007 backup_whj]# ls
hse_whj_full.sql master-log.000038 master-log.sql
[root@centos007 backup_whj]# mysql -uroot -p < hse_whj_full.sql
Enter password:
[root@centos007 backup_whj]#
8、binlog恢复
[root@centos007 backup_whj]# ls
hse_whj_full.sql master-log.000038 master-log.sql
[root@centos007 backup_whj]# mysql -uroot -p < master-log.sql
Enter password:
[root@centos007 backup_whj]#
9、验证
+--------------------+
| Database |
+--------------------+
| hse01 |
| hse_whj |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.02 sec)
MariaDB [(none)]> use hse_whj;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hse_whj]> select count(1) from whj_a;
+----------+
| count(1) |
+----------+
| 11 |
+----------+
1 row in set (0.01 sec)
MariaDB [hse_whj]>
通过验证发现hse_whj表数据已经是11条了,恢复了之前的数据。不要忘记再开启二进制日志。