Mysql备份还原

Mysql备份还原

1、备份类型

热备份、温备份、冷备份 (根据服务器状态)
	热备份:读、写不受影响;
	温备份:仅可以执行读操作;
	冷备份:离线备份;读、写操作均中止;

物理备份与逻辑备份 (从对象来分)
	物理备份:复制数据文件;
	逻辑备份:将数据导出至文本文件中;

完全备份、增量备份、差异备份 (从数据收集来分)
	完全备份:备份全部数据;
	增量备份:仅备份上次完全备份或增量备份以后变化的数据;
	差异备份:仅备份上次完全备份以来变化的数据;

2、备份案例

mysqldump+binlog实现完全+增量备份

创建实验用数据库

mysql> create database school;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

创建学生表并加入实验数据

mysql>  CREATE TABLE `Student` (
    ->        `Sno` int(10) NOT NULL COMMENT '学号',  `Sname` varchar(16) NOT NULL COMMENT '姓名',
    ->        `Ssex` char(2) NOT NULL COMMENT '性别',  `Sage` tinyint(2) NOT NULL DEFAULT '0' COMMENT '学生年龄',
    ->        `Sdept` varchar(16) DEFAULT 'NULL' COMMENT '学生所在系别',  PRIMARY KEY (`Sno`)
    ->      ) ;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> select * from Student;
Empty set (0.01 sec)

mysql> INSERT INTO `Student` VALUES (1, '陆亚', '男', 24, '计算机网络'),(2, 'tom', '男', 26, '英语'),(3, '张阳', '男', 21, '物流管理'), (4, 'alex', '女', 22, '电子商务');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from Student;
+-----+--------+------+------+-----------------+
| Sno | Sname  | Ssex | Sage | Sdept           |
+-----+--------+------+------+-----------------+
|   1 | 陆亚   | 男   |   24 | 计算机网络      |
|   2 | tom    | 男   |   26 | 英语            |
|   3 | 张阳   | 男   |   21 | 物流管理        |
|   4 | alex   | 女   |   22 | 电子商务        |
+-----+--------+------+------+-----------------+
4 rows in set (0.00 sec)

完全备份:

[root@localhost ~]# mysqldump --opt -B school > school.sql
[root@localhost ~]# ll
总用量 24044
-rw-------. 1 root root      997  4月 29 14:38 anaconda-ks.cfg
-rw-r--r--. 1 root root 24585268  2月  4  2023 haproxy
-rw-r--r--. 1 root root     2434  9月  4 13:57 school.sql
drwxr-xr-x. 2 root root       35  9月  3 15:03 shell_test
-rw-r--r--. 1 root root    22174  6月 23 18:57 wget-log

mysql> show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |      1256 | No        |
+---------------+-----------+-----------+
1 row in set (0.00 sec)

再次插入数据

mysql> INSERT INTO Student values(0005,'xumubin','男',29,'中文专业'),(0006,'wangzhao','男',21,'导弹专 业');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from Student;
+-----+----------+------+------+-----------------+
| Sno | Sname    | Ssex | Sage | Sdept           |
+-----+----------+------+------+-----------------+
|   1 | 陆亚     | 男   |   24 | 计算机网络      |
|   2 | tom      | 男   |   26 | 英语            |
|   3 | 张阳     | 男   |   21 | 物流管理        |
|   4 | alex     | 女   |   22 | 电子商务        |
|   5 | xumubin  | 男   |   29 | 中文专业        |
|   6 | wangzhao | 男   |   21 | 导弹专业        |
+-----+----------+------+------+-----------------+
6 rows in set (0.01 sec)

mysql> show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |      1611 | No        |
+---------------+-----------+-----------+
1 row in set (0.00 sec)

假如此时数据库损坏:

mysql> drop database school;
Query OK, 1 row affected (0.02 sec)

开始数据还原:
1.发现数据库异常,及时刷新日志并保存

# 此命令会立刻保存binlog.000001日志,生成binlog.000002日志来接替记录的工作
保留案发现场,打开日志来分析
mysql> flush logs;    
Query OK, 0 rows affected (0.01 sec)

mysql>  show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |      1842 | No        |
| binlog.000002 |       157 | No        |
+---------------+-----------+-----------+
2 rows in set (0.00 sec)

2.先还原完全备份

此时数据库中没有school库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> source  ~/school.sql;
Query OK, 0 rows affected (0.00 sec)

Database changed

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> use school;
Database changed
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| Student          |
+------------------+
1 row in set (0.00 sec)

mysql> select * from Student;
+-----+--------+------+------+-----------------+
| Sno | Sname  | Ssex | Sage | Sdept           |
+-----+--------+------+------+-----------------+
|   1 | 陆亚   | 男   |   24 | 计算机网络      |
|   2 | tom    | 男   |   26 | 英语            |
|   3 | 张阳   | 男   |   21 | 物流管理        |
|   4 | alex   | 女   |   22 | 电子商务        |
+-----+--------+------+------+-----------------+
4 rows in set (0.00 sec)

# 可以发现通过完全备份还原,之前创建的表结构和数据都恢复了,但新插入的数据不存在,这就需要还原增量备份了

3.查看二进制日志
#注意5.7版本,insert语句已经加密,默认看不到,查看时加上选项 --base64-output=DECODE-ROWS -vv

##将binlog日志拷贝一份
[root@localhost ~]# cd /
[root@localhost /]# mkdir mysql_log
[root@localhost /]# cp /var/lib/mysql/binlog.000001 mysql_log/

# 查看二进制日志
使用mysqlbinlog
mysqlbinlog binlog.000001
	#注意5.7版本,insert语句已经加密,默认看不到,查看时加上选项 --base64-output=DECODE-ROWS -vv

# at 1482
#240904 13:59:10 server id 1  end_log_pos 1580 CRC32 0x7e3fb4db 	Write_rows: table id 112 flags: STMT_END_F
### INSERT INTO `school`.`Student`
### SET
###   @1=5 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xumubin' /* VARSTRING(64) meta=64 nullable=0 is_null=0 */
###   @3='男' /* STRING(8) meta=65032 nullable=0 is_null=0 */
###   @4=29 /* TINYINT meta=0 nullable=0 is_null=0 */
###   @5='中文专业' /* VARSTRING(64) meta=64 nullable=1 is_null=0 */
### INSERT INTO `school`.`Student`
### SET
###   @1=6 /* INT meta=0 nullable=0 is_null=0 */
###   @2='wangzhao' /* VARSTRING(64) meta=64 nullable=0 is_null=0 */
###   @3='男' /* STRING(8) meta=65032 nullable=0 is_null=0 */
###   @4=21 /* TINYINT meta=0 nullable=0 is_null=0 */
###   @5='导弹专业' /* VARSTRING(64) meta=64 nullable=1 is_null=0 */
# at 1580
#240904 13:59:10 server id 1  end_log_pos 1611 CRC32 0xd77aeed5 	Xid = 46
COMMIT/*!*/;

## 记录下我们插入新数据的时间点和提交该事务的时间点   基于时间点恢复

基于时间点增量恢复

[root@localhost mysql_log]# mysqlbinlog binlog.000001 --start-datetime="2024-09-04 13:59:10" --stop-datetime="2024-09-04 14:00:08" -r time1.sql
[root@localhost mysql_log]# ll
总用量 8
-rw-r-----. 1 root root 1842  9月  4 14:05 binlog.000001
-rw-r-----. 1 root root 2892  9月  4 14:16 time1.sql
mysql> source /mysql_log/time1.sql
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select * from Student;
+-----+----------+------+------+-----------------+
| Sno | Sname    | Ssex | Sage | Sdept           |
+-----+----------+------+------+-----------------+
|   1 | 陆亚     | 男   |   24 | 计算机网络      |
|   2 | tom      | 男   |   26 | 英语            |
|   3 | 张阳     | 男   |   21 | 物流管理        |
|   4 | alex     | 女   |   22 | 电子商务        |
|   5 | xumubin  | 男   |   29 | 中文专业        |
|   6 | wangzhao | 男   |   21 | 导弹专业        |
+-----+----------+------+------+-----------------+
6 rows in set (0.00 sec)
### 可以看到数据恢复成功

基于binlog gtid特性进行恢复

binlog日志的GTID新特性

1、 GTID 介绍

  1. 什么是GTID

GTID(Global Transaction ID),全局事务标识符。是对于一个已提交事务的编号,并且是一个全局唯一的编号。
它是MySQL 5.6加入的一个强大特性,目的在于能够实现主从自动定位和切换,而不像以前需要指定文件和位置。

GTID的格式与存储

单个GTID
GTID与主库上提交的每个事务相关联。此标识符不仅对发起事务的库是唯一的,而且在给定复制拓扑中的所有库中都是唯一的。GTID用冒号分隔的一对坐标表示,例如:

8eed0f5b-6f9b-11e9-94a9-005056a57a4e:23
前一部分是主库的server_uuid,后面一部分是主库上按提交事务的顺序确定的序列号,提交的事务序号从1开始。
GTID = server_uuid :transaction_id

GTID集
GTID集是包括一个或多个单个GTID或GTID范围的集合。源自同一服务器的一系列GTID可以折叠为单个表达式,例如:

8eed0f5b-6f9b-11e9-94a9-005056a57a4e:1-321
上面的示例表示源自server_uuid为8eed0f5b-6f9b-11e9-94a9-005056a57a4e服务器的第1到第321个事务。源自同一服务器的多个单GTID或GTID范围可以同时包含在由冒号分隔的单个表达式中,例如:

8eed0f5b-6f9b-11e9-94a9-005056a57a4e:1-3:11:47-49

3)mysql.gtid_executed表
mysql.gtid_executed表结构如下:

mysql> desc mysql.gtid_executed;
+----------------+------------+------+-----+---------+-------+
| Field          | Type       | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| source_uuid    | char(36)   | NO   | PRI | NULL    |       |
| interval_start | bigint(20) | NO   | PRI | NULL    |       |
| interval_end   | bigint(20) | NO   |     | NULL    |       |
+----------------+------------+------+-----+---------+-------+

mysql.gtid_executed表记录的是服务器上已经执行事务的GTID。三个字段分别表示发起事务的服务器UUID、UUID集的起始和结束事务ID。对于单个GTID,后两个字段的值相同。

  1. 版本支持

5.6 版本新加的特性,5.7中做了加强
5.6 中不开启,没有这个功能.
5.7 中的GTID,即使不开也会有自动生成
SET @@SESSION.GTID_NEXT= ‘ANONYMOUS’

mysql> show variables like '%gtid%';
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery      | ON        |
| enforce_gtid_consistency         | OFF       |
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | OFF       |
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+
8 rows in set (0.01 sec)
  1. 如何开启

还未开启前不会显示当前事务的gtid
Mysql备份还原_第1张图片

[root@localhost mysql_log]# vim /etc/my.cnf.d/mysql-server.cnf 
##在mysql-server.cnf 文件里加入一下两行
gtid-mode=on
enforce-gtid-consistency=true

#重启mysql服务,进行查看
[root@localhost mysql_log]# systemctl restart mysqld

DDL和DML语句查看gtid

DDL一个语句产生一个gtid
mysql> create database db3;
Query OK, 1 row affected (0.00 sec)
## 可以看到,能够看到gtid,已经由于mysql服务重启,所以binlog日志又创新创建了一个
mysql> show master status;
+---------------+----------+--------------+------------------+----------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                      |
+---------------+----------+--------------+------------------+----------------------------------------+
| binlog.000003 |      339 |              |                  | 0b058611-6a82-11ef-b2ef-000c298fa5fd:1 |
+---------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.01 sec)


mysql> create table t2 (id int);
Query OK, 0 rows affected (0.02 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+------------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+---------------+----------+--------------+------------------+------------------------------------------+
| binlog.000003 |      532 |              |                  | 0b058611-6a82-11ef-b2ef-000c298fa5fd:1-2 |
+---------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
mysql> create table t1 (id int);
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+------------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+---------------+----------+--------------+------------------+------------------------------------------+
| binlog.000003 |      725 |              |                  | 0b058611-6a82-11ef-b2ef-000c298fa5fd:1-3 |
+---------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)


DML一个事务产生一个gtid
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 values(2);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+---------------+----------+--------------+------------------+------------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+---------------+----------+--------------+------------------+------------------------------------------+
| binlog.000003 |     1092 |              |                  | 0b058611-6a82-11ef-b2ef-000c298fa5fd:1-4 |
+---------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

基于GTID进行查看binlog

mysql> SHOW BINLOG EVENTS IN 'binlog.000003';
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                              |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| binlog.000003 |    4 | Format_desc    |         1 |         126 | Server ver: 8.0.36, Binlog ver: 4                                 |
| binlog.000003 |  126 | Previous_gtids |         1 |         157 |                                                                   |
| binlog.000003 |  157 | Gtid           |         1 |         234 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:1' |
| binlog.000003 |  234 | Query          |         1 |         339 | create database db3 /* xid=6 */                                   |
| binlog.000003 |  339 | Gtid           |         1 |         416 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:2' |
| binlog.000003 |  416 | Query          |         1 |         532 | use `school`; create table t2 (id int) /* xid=9 */                |
| binlog.000003 |  532 | Gtid           |         1 |         609 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:3' |
| binlog.000003 |  609 | Query          |         1 |         725 | use `school`; create table t1 (id int) /* xid=11 */               |
| binlog.000003 |  725 | Gtid           |         1 |         804 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:4' |
| binlog.000003 |  804 | Query          |         1 |         881 | BEGIN                                                             |
| binlog.000003 |  881 | Table_map      |         1 |         931 | table_id: 92 (school.t1)                                          |
| binlog.000003 |  931 | Write_rows     |         1 |         971 | table_id: 92 flags: STMT_END_F                                    |
| binlog.000003 |  971 | Table_map      |         1 |        1021 | table_id: 92 (school.t1)                                          |
| binlog.000003 | 1021 | Write_rows     |         1 |        1061 | table_id: 92 flags: STMT_END_F                                    |
| binlog.000003 | 1061 | Xid            |         1 |        1092 | COMMIT /* xid=14 */                                               |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
15 rows in set (0.00 sec)

具备GTID后,截取查看某些事务日志:
–include-gtids
–exclude-gtids

示例:

第一次操作:

mysql> show master status;
+---------------+----------+--------------+------------------+------------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+---------------+----------+--------------+------------------+------------------------------------------+
| binlog.000003 |     1092 |              |                  | 0b058611-6a82-11ef-b2ef-000c298fa5fd:1-4 |
+---------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

mysql> create database gtid;
Query OK, 1 row affected (0.00 sec)

mysql> use gtid
Database changed
mysql> create table t1(id int);
Query OK, 0 rows affected (0.01 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

第二次操作

mysql>  create table t2(id int);
Query OK, 0 rows affected (0.01 sec)

mysql>  begin;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t2 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql>  flush logs;
Query OK, 0 rows affected (0.02 sec)

第三次操作:

mysql> create table t3(id int);
Query OK, 0 rows affected (0.01 sec)

mysql>  begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t3 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+----------------+
| Tables_in_gtid |
+----------------+
| t1             |
| t2             |
| t3             |
+----------------+
3 rows in set (0.01 sec)
## 此时模拟误删数据库
mysql> drop database gtid;
Query OK, 3 rows affected (0.02 sec)

使用binlog日志恢复误删除的gitd数据库。

首先要确定gtid的起始和结束。

#从binlog.000003开始分别flush log了两次,因此binlog.000003记录了第一次操作,binlog.000004记录了第二次操作,binlog.000005记录了第三次操作以及误删操作
mysql> show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |      1842 | No        |
| binlog.000002 |      2456 | No        |
| binlog.000003 |      1782 | No        |
| binlog.000004 |       702 | No        |
| binlog.000005 |       839 | No        |
+---------------+-----------+-----------+
5 rows in set (0.01 sec)

确定起始位置:

mysql> show binlog events in 'binlog.000003';
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                              |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
| binlog.000003 |    4 | Format_desc    |         1 |         126 | Server ver: 8.0.36, Binlog ver: 4                                 |
| binlog.000003 |  126 | Previous_gtids |         1 |         157 |                                                                   |
| binlog.000003 |  157 | Gtid           |         1 |         234 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:1' |
| binlog.000003 |  234 | Query          |         1 |         339 | create database db3 /* xid=6 */                                   |
| binlog.000003 |  339 | Gtid           |         1 |         416 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:2' |
| binlog.000003 |  416 | Query          |         1 |         532 | use `school`; create table t2 (id int) /* xid=9 */                |
| binlog.000003 |  532 | Gtid           |         1 |         609 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:3' |
| binlog.000003 |  609 | Query          |         1 |         725 | use `school`; create table t1 (id int) /* xid=11 */               |
| binlog.000003 |  725 | Gtid           |         1 |         804 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:4' |
| binlog.000003 |  804 | Query          |         1 |         881 | BEGIN                                                             |
| binlog.000003 |  881 | Table_map      |         1 |         931 | table_id: 92 (school.t1)                                          |
| binlog.000003 |  931 | Write_rows     |         1 |         971 | table_id: 92 flags: STMT_END_F                                    |
| binlog.000003 |  971 | Table_map      |         1 |        1021 | table_id: 92 (school.t1)                                          |
| binlog.000003 | 1021 | Write_rows     |         1 |        1061 | table_id: 92 flags: STMT_END_F                                    |
| binlog.000003 | 1061 | Xid            |         1 |        1092 | COMMIT /* xid=14 */                                               |
| binlog.000003 | 1092 | Gtid           |         1 |        1169 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:5' |
| binlog.000003 | 1169 | Query          |         1 |        1277 | create database gtid /* xid=21 */                                 |
| binlog.000003 | 1277 | Gtid           |         1 |        1354 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:6' |
| binlog.000003 | 1354 | Query          |         1 |        1465 | use `gtid`; create table t1(id int) /* xid=26 */                  |
| binlog.000003 | 1465 | Gtid           |         1 |        1544 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:7' |
| binlog.000003 | 1544 | Query          |         1 |        1619 | BEGIN                                                             |
| binlog.000003 | 1619 | Table_map      |         1 |        1667 | table_id: 93 (gtid.t1)                                            |
| binlog.000003 | 1667 | Write_rows     |         1 |        1707 | table_id: 93 flags: STMT_END_F                                    |
| binlog.000003 | 1707 | Xid            |         1 |        1738 | COMMIT /* xid=28 */                                               |
| binlog.000003 | 1738 | Rotate         |         1 |        1782 | binlog.000004;pos=4                                               |
+---------------+------+----------------+-----------+-------------+-------------------------------------------------------------------+
25 rows in set (0.00 sec)



查看到起始的gitid号为binlog.000003日志中的5
文件:binlog.000003
gtid:0b058611-6a82-11ef-b2ef-000c298fa5fd:5

| binlog.000003 | 1092 | Gtid           |         1 |        1169 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:5' |
| binlog.000003 | 1169 | Query          |         1 |        1277 | create database gtid /* xid=21 */        

查看结束:

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+---------------+----------+--------------+------------------+-------------------------------------------+
| binlog.000005 |      839 |              |                  | 0b058611-6a82-11ef-b2ef-000c298fa5fd:1-12 |
+---------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.01 sec)

mysql> show binlog events in 'binlog.000005';
+---------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+
| Log_name      | Pos | Event_type     | Server_id | End_log_pos | Info                                                               |
+---------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+
| binlog.000005 |   4 | Format_desc    |         1 |         126 | Server ver: 8.0.36, Binlog ver: 4                                  |
| binlog.000005 | 126 | Previous_gtids |         1 |         197 | 0b058611-6a82-11ef-b2ef-000c298fa5fd:1-9                           |
| binlog.000005 | 197 | Gtid           |         1 |         274 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:10' |
| binlog.000005 | 274 | Query          |         1 |         385 | use `gtid`; create table t3(id int) /* xid=36 */                   |
| binlog.000005 | 385 | Gtid           |         1 |         464 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:11' |
| binlog.000005 | 464 | Query          |         1 |         539 | BEGIN                                                              |
| binlog.000005 | 539 | Table_map      |         1 |         587 | table_id: 95 (gtid.t3)                                             |
| binlog.000005 | 587 | Write_rows     |         1 |         627 | table_id: 95 flags: STMT_END_F                                     |
| binlog.000005 | 627 | Xid            |         1 |         658 | COMMIT /* xid=38 */                                                |
| binlog.000005 | 658 | Gtid           |         1 |         735 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:12' |
| binlog.000005 | 735 | Query          |         1 |         839 | drop database gtid /* xid=41 */                                    |
+---------------+-----+----------------+-----------+-------------+--------------------------------------------------------------------+
11 rows in set (0.00 sec)

确认结束giid为binlog.000005日志的11
文件:binlog.000005
gtid:0b058611-6a82-11ef-b2ef-000c298fa5fd:11

| binlog.000005 | 385 | Gtid           |         1 |         464 | SET @@SESSION.GTID_NEXT= '0b058611-6a82-11ef-b2ef-000c298fa5fd:11' |
| binlog.000005 | 464 | Query          |         1 |         539 | BEGIN                                                              |
| binlog.000005 | 539 | Table_map      |         1 |         587 | table_id: 95 (gtid.t3)                                             |
| binlog.000005 | 587 | Write_rows     |         1 |         627 | table_id: 95 flags: STMT_END_F                                     |
| binlog.000005 | 627 | Xid            |         1 |         658 | COMMIT /* xid=38 */       

开始恢复,生成sql文件
binlog使用gtid截取日志

确定起始范围:5-11

文件:binlog.000003 binlog.000004 binlog.000005

[root@localhost mysql_log]# cd /var/lib/mysql
[root@localhost mysql]# mysqlbinlog --include-gtids='0b058611-6a82-11ef-b2ef-000c298fa5fd:5-11' binlog.000003  binlog.000004 binlog.000005 -r /mysql_log/gtid1.sql
[root@localhost mysql]# cd /mysql_log/
[root@localhost mysql_log]# ll
总用量 20
-rw-r-----. 1 root root  1842  9月  4 14:05 binlog.000001
-rw-r-----. 1 root root 10768  9月  4 14:52 gtid1.sql
-rw-r-----. 1 root root  2892  9月  4 14:16 time1.sql

最后,使用binlog日志恢复

mysql> set sql_log_bin=0;  #设为0后,在Master数据库上执行的语句都不记录binlog
Query OK, 0 rows affected (0.00 sec)


mysql> source /mysql_log/gtid1.sql
Query OK, 0 rows affected, 1 warning (0.00 sec)
## 发现中间出现了这个错误
ERROR 1046 (3D000): No database selected

#并且数据库gtid也不存在
mysql> show databases like 'gtid';
Empty set (0.00 sec)

原因:GTID幂等性。
开启GTID后,MySQL恢复Binlog时,重复GTID的事务不会再执行了
就想恢复?怎么办? 在命令后面加上–skip-gtids
接着上面,截取日志时添加–skip-gtids。

[root@localhost mysql]# mysqlbinlog --skip-gtids --include-gtids='0b058611-6a82-11ef-b2ef-000c298fa5fd:5-11' binlog.000003  binlog.000004 binlog.000005 -r /mysql_log/gtid2.sql


mysql> show databases like 'gtid';
+-----------------+
| Database (gtid) |
+-----------------+
| gtid            |
+-----------------+
1 row in set (0.01 sec)

mysql> select * from gtid.t1;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
## 此时数据完全恢复

lvm快照备份

你可能感兴趣的:(mysql,数据库)