第17章 其他数据库日志【4.日志与备份篇】【MySQL高级】3

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
日志服务 SLS,月写入数据量 50GB 1个月
简介: 第17章 其他数据库日志【4.日志与备份篇】【MySQL高级】3
  • Statement

每一条会修改数据的sql都会记录在binlog中。

优点:不需要记录每一行的变化,减少了binlog日志量,节约了IO,提高性能。

  • Row
    5.1.5版本的MySQL才开始支持row level 的复制,它不记录sql语句上下文相关信息,仅保存哪条记录被修改。优点:row level 的日志内容会非常清楚的记录下每一行数据修改的细节。而且不会出现某些特定情况下的存储过程,或function,以及trigger的调用和触发无法被正确复制的问题。
    Mixed

从5.1.8版本开始,MySQL提供了Mixed格式,实际上就是Statement与Row的结合。

详细情况,下章讲解。

演示

mysql> SHOW BINARY LOGS;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |       498 | No        |
| binlog.000002 |      1400 | No        |
| binlog.000003 |      7004 | No        |
| binlog.000004 | 647264652 | No        |
| binlog.000005 |   3583532 | No        |
| binlog.000006 | 517896094 | No        |
| binlog.000007 |    139656 | No        |
| binlog.000008 |  34024458 | No        |
| binlog.000009 |      7276 | No        |
| binlog.000010 |       156 | No        |
+---------------+-----------+-----------+
10 rows in set (0.02 sec)
mysql> use atguigudb33;
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
mysql> select * from student;
+----+---------+--------+
| id | name    | class  |
+----+---------+--------+
|  1 | 张三3   | 一班   |
|  3 | 李四1   | 一班   |
|  6 | jerry   | 一班   |
|  8 | 王五    | 二班   |
| 11 | Tim     | 一班   |
| 15 | 赵六    | 二班   |
| 17 | tom     | 三班   |
| 20 | 钱七    | 三班   |
+----+---------+--------+
8 rows in set (0.00 sec)
mysql> insert into student(id,name,class) values(18,'Jerry','四班');
Query OK, 1 row affected (0.00 sec)
mysql> update student set name ='Tom1' where id=17;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> 
mysql> SHOW BINARY LOGS;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |       498 | No        |
| binlog.000002 |      1400 | No        |
| binlog.000003 |      7004 | No        |
| binlog.000004 | 647264652 | No        |
| binlog.000005 |   3583532 | No        |
| binlog.000006 | 517896094 | No        |
| binlog.000007 |    139656 | No        |
| binlog.000008 |  34024458 | No        |
| binlog.000009 |      7276 | No        |
| binlog.000010 |       809 | No        | #156->809 
+---------------+-----------+-----------+
10 rows in set (0.00 sec)

演示-查看binlog文件

[root@centos7-mysql-1 mysql]# mysqlbinlog "/var/lib/mysql/binlog.000010"
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#220813 15:49:16 server id 1  end_log_pos 125 CRC32 0xb8b54d0b  Start: binlog v 4, server v 8.0.25 created 220813 15:49:16 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
fFf3Yg8BAAAAeQAAAH0AAAABAAQAOC4wLjI1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAB8V/diEwANAAgAAAAABAAEAAAAYQAEGggAAAAICAgCAAAACgoKKioAEjQA
CigBC021uA==
'/*!*/;
# at 125
#220813 15:49:16 server id 1  end_log_pos 156 CRC32 0xec56ebcc  Previous-GTIDs
# [empty]
# at 156
#220813 16:51:31 server id 1  end_log_pos 235 CRC32 0xd9671797  Anonymous_GTID  last_committed=0  sequence_number=1 rbr_only=yes  original_committed_timestamp=1660380691404597 immediate_commit_timestamp=1660380691404597 transaction_length=314
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
# original_commit_timestamp=1660380691404597 (2022-08-13 16:51:31.404597 CST)
# immediate_commit_timestamp=1660380691404597 (2022-08-13 16:51:31.404597 CST)
/*!80001 SET @@session.original_commit_timestamp=1660380691404597*//*!*/;
/*!80014 SET @@session.original_server_version=80025*//*!*/;
/*!80014 SET @@session.immediate_server_version=80025*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 235
#220813 16:51:31 server id 1  end_log_pos 317 CRC32 0xade91e8f  Query thread_id=8 exec_time=0 error_code=0
SET TIMESTAMP=1660380691/*!*/;
SET @@session.pseudo_thread_id=8/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1168113696/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=255,@@session.collation_connection=255,@@session.collation_server=255/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
/*!80011 SET @@session.default_collation_for_utf8mb4=255*//*!*/;
BEGIN
/*!*/;
# at 317
#220813 16:51:31 server id 1  end_log_pos 386 CRC32 0x755bfbc8  Table_map: `atguigudb33`.`student` mapped to number 105
# at 386
#220813 16:51:31 server id 1  end_log_pos 439 CRC32 0x16df2352  Write_rows: table id 105 flags: STMT_END_F
BINLOG '
E2b3YhMBAAAARQAAAIIBAAAAAGkAAAAAAAEAC2F0Z3VpZ3VkYjMzAAdzdHVkZW50AAMDDw8EPAAe
AAYBAQACASHI+1t1
E2b3Yh4BAAAANQAAALcBAAAAAGkAAAAAAAEAAgAD/wASAAAABUplcnJ5BuWbm+ePrVIj3xY=
'/*!*/;
# at 439
#220813 16:51:31 server id 1  end_log_pos 470 CRC32 0x8082e98a  Xid = 36
COMMIT/*!*/;
# at 470
#220813 16:52:03 server id 1  end_log_pos 549 CRC32 0xd24aceb2  Anonymous_GTID  last_committed=1  sequence_number=2 rbr_only=yes  original_committed_timestamp=1660380723139128 immediate_commit_timestamp=1660380723139128 transaction_length=339
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
# original_commit_timestamp=1660380723139128 (2022-08-13 16:52:03.139128 CST)
# immediate_commit_timestamp=1660380723139128 (2022-08-13 16:52:03.139128 CST)
/*!80001 SET @@session.original_commit_timestamp=1660380723139128*//*!*/;
/*!80014 SET @@session.original_server_version=80025*//*!*/;
/*!80014 SET @@session.immediate_server_version=80025*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 549
#220813 16:52:03 server id 1  end_log_pos 640 CRC32 0x36b0b76c  Query thread_id=8 exec_time=0 error_code=0
SET TIMESTAMP=1660380723/*!*/;
BEGIN
/*!*/;
# at 640
#220813 16:52:03 server id 1  end_log_pos 709 CRC32 0x58a56350  Table_map: `atguigudb33`.`student` mapped to number 105
# at 709
#220813 16:52:03 server id 1  end_log_pos 778 CRC32 0x1d659187  Update_rows: table id 105 flags: STMT_END_F
BINLOG '
M2b3YhMBAAAARQAAAMUCAAAAAGkAAAAAAAEAC2F0Z3VpZ3VkYjMzAAdzdHVkZW50AAMDDw8EPAAe
AAYBAQACASFQY6VY
M2b3Yh8BAAAARQAAAAoDAAAAAGkAAAAAAAEAAgAD//8AEQAAAAN0b20G5LiJ54+tABEAAAAEVG9t
MQbkuInnj62HkWUd
'/*!*/;
# at 778
#220813 16:52:03 server id 1  end_log_pos 809 CRC32 0xaf15e2a4  Xid = 37
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@centos7-mysql-1 mysql]# 

演示-v查看

[root@centos7-mysql-1 mysql]# mysqlbinlog -v "/var/lib/mysql/binlog.000010"
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#220813 15:49:16 server id 1  end_log_pos 125 CRC32 0xb8b54d0b  Start: binlog v 4, server v 8.0.25 created 220813 15:49:16 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
fFf3Yg8BAAAAeQAAAH0AAAABAAQAOC4wLjI1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAB8V/diEwANAAgAAAAABAAEAAAAYQAEGggAAAAICAgCAAAACgoKKioAEjQA
CigBC021uA==
'/*!*/;
# at 125
#220813 15:49:16 server id 1  end_log_pos 156 CRC32 0xec56ebcc  Previous-GTIDs
# [empty]
# at 156
#220813 16:51:31 server id 1  end_log_pos 235 CRC32 0xd9671797  Anonymous_GTID  last_committed=0  sequence_number=1 rbr_only=yes  original_committed_timestamp=1660380691404597 immediate_commit_timestamp=1660380691404597 transaction_length=314
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
# original_commit_timestamp=1660380691404597 (2022-08-13 16:51:31.404597 CST)
# immediate_commit_timestamp=1660380691404597 (2022-08-13 16:51:31.404597 CST)
/*!80001 SET @@session.original_commit_timestamp=1660380691404597*//*!*/;
/*!80014 SET @@session.original_server_version=80025*//*!*/;
/*!80014 SET @@session.immediate_server_version=80025*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 235
#220813 16:51:31 server id 1  end_log_pos 317 CRC32 0xade91e8f  Query thread_id=8 exec_time=0 error_code=0
SET TIMESTAMP=1660380691/*!*/;
SET @@session.pseudo_thread_id=8/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1168113696/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=255,@@session.collation_connection=255,@@session.collation_server=255/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
/*!80011 SET @@session.default_collation_for_utf8mb4=255*//*!*/;
BEGIN
/*!*/;
# at 317
#220813 16:51:31 server id 1  end_log_pos 386 CRC32 0x755bfbc8  Table_map: `atguigudb33`.`student` mapped to number 105
# at 386
#220813 16:51:31 server id 1  end_log_pos 439 CRC32 0x16df2352  Write_rows: table id 105 flags: STMT_END_F
BINLOG '
E2b3YhMBAAAARQAAAIIBAAAAAGkAAAAAAAEAC2F0Z3VpZ3VkYjMzAAdzdHVkZW50AAMDDw8EPAAe
AAYBAQACASHI+1t1
E2b3Yh4BAAAANQAAALcBAAAAAGkAAAAAAAEAAgAD/wASAAAABUplcnJ5BuWbm+ePrVIj3xY=
'/*!*/;
### INSERT INTO `atguigudb33`.`student`
### SET
###   @1=18
###   @2='Jerry'
###   @3='四班'
# at 439
#220813 16:51:31 server id 1  end_log_pos 470 CRC32 0x8082e98a  Xid = 36
COMMIT/*!*/;
# at 470
#220813 16:52:03 server id 1  end_log_pos 549 CRC32 0xd24aceb2  Anonymous_GTID  last_committed=1  sequence_number=2 rbr_only=yes  original_committed_timestamp=1660380723139128 immediate_commit_timestamp=1660380723139128 transaction_length=339
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
# original_commit_timestamp=1660380723139128 (2022-08-13 16:52:03.139128 CST)
# immediate_commit_timestamp=1660380723139128 (2022-08-13 16:52:03.139128 CST)
/*!80001 SET @@session.original_commit_timestamp=1660380723139128*//*!*/;
/*!80014 SET @@session.original_server_version=80025*//*!*/;
/*!80014 SET @@session.immediate_server_version=80025*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 549
#220813 16:52:03 server id 1  end_log_pos 640 CRC32 0x36b0b76c  Query thread_id=8 exec_time=0 error_code=0
SET TIMESTAMP=1660380723/*!*/;
BEGIN
/*!*/;
# at 640
#220813 16:52:03 server id 1  end_log_pos 709 CRC32 0x58a56350  Table_map: `atguigudb33`.`student` mapped to number 105
# at 709
#220813 16:52:03 server id 1  end_log_pos 778 CRC32 0x1d659187  Update_rows: table id 105 flags: STMT_END_F
BINLOG '
M2b3YhMBAAAARQAAAMUCAAAAAGkAAAAAAAEAC2F0Z3VpZ3VkYjMzAAdzdHVkZW50AAMDDw8EPAAe
AAYBAQACASFQY6VY
M2b3Yh8BAAAARQAAAAoDAAAAAGkAAAAAAAEAAgAD//8AEQAAAAN0b20G5LiJ54+tABEAAAAEVG9t
MQbkuInnj62HkWUd
'/*!*/;
### UPDATE `atguigudb33`.`student`
### WHERE
###   @1=17
###   @2='tom'
###   @3='三班'
### SET
###   @1=17
###   @2='Tom1'
###   @3='三班'
# at 778
#220813 16:52:03 server id 1  end_log_pos 809 CRC32 0xaf15e2a4  Xid = 37
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@centos7-mysql-1 mysql]# 

演示–base64-output=DECODE-ROWS

[root@centos7-mysql-1 mysql]# mysqlbinlog -v --base64-output=DECODE-ROWS "/var/lib/mysql/binlog.000010"
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#220813 15:49:16 server id 1  end_log_pos 125 CRC32 0xb8b54d0b  Start: binlog v 4, server v 8.0.25 created 220813 15:49:16 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
# at 125
#220813 15:49:16 server id 1  end_log_pos 156 CRC32 0xec56ebcc  Previous-GTIDs
# [empty]
# at 156
#220813 16:51:31 server id 1  end_log_pos 235 CRC32 0xd9671797  Anonymous_GTID  last_committed=0  sequence_number=1 rbr_only=yes  original_committed_timestamp=1660380691404597 immediate_commit_timestamp=1660380691404597 transaction_length=314
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
# original_commit_timestamp=1660380691404597 (2022-08-13 16:51:31.404597 CST)
# immediate_commit_timestamp=1660380691404597 (2022-08-13 16:51:31.404597 CST)
/*!80001 SET @@session.original_commit_timestamp=1660380691404597*//*!*/;
/*!80014 SET @@session.original_server_version=80025*//*!*/;
/*!80014 SET @@session.immediate_server_version=80025*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 235
#220813 16:51:31 server id 1  end_log_pos 317 CRC32 0xade91e8f  Query thread_id=8 exec_time=0 error_code=0
SET TIMESTAMP=1660380691/*!*/;
SET @@session.pseudo_thread_id=8/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1168113696/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=255,@@session.collation_connection=255,@@session.collation_server=255/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
/*!80011 SET @@session.default_collation_for_utf8mb4=255*//*!*/;
BEGIN
/*!*/;
# at 317
#220813 16:51:31 server id 1  end_log_pos 386 CRC32 0x755bfbc8  Table_map: `atguigudb33`.`student` mapped to number 105
# at 386
#220813 16:51:31 server id 1  end_log_pos 439 CRC32 0x16df2352  Write_rows: table id 105 flags: STMT_END_F
### INSERT INTO `atguigudb33`.`student`
### SET
###   @1=18
###   @2='Jerry'
###   @3='四班'
# at 439
#220813 16:51:31 server id 1  end_log_pos 470 CRC32 0x8082e98a  Xid = 36
COMMIT/*!*/;
# at 470
#220813 16:52:03 server id 1  end_log_pos 549 CRC32 0xd24aceb2  Anonymous_GTID  last_committed=1  sequence_number=2 rbr_only=yes  original_committed_timestamp=1660380723139128 immediate_commit_timestamp=1660380723139128 transaction_length=339
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
# original_commit_timestamp=1660380723139128 (2022-08-13 16:52:03.139128 CST)
# immediate_commit_timestamp=1660380723139128 (2022-08-13 16:52:03.139128 CST)
/*!80001 SET @@session.original_commit_timestamp=1660380723139128*//*!*/;
/*!80014 SET @@session.original_server_version=80025*//*!*/;
/*!80014 SET @@session.immediate_server_version=80025*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 549
#220813 16:52:03 server id 1  end_log_pos 640 CRC32 0x36b0b76c  Query thread_id=8 exec_time=0 error_code=0
SET TIMESTAMP=1660380723/*!*/;
BEGIN
/*!*/;
# at 640
#220813 16:52:03 server id 1  end_log_pos 709 CRC32 0x58a56350  Table_map: `atguigudb33`.`student` mapped to number 105
# at 709
#220813 16:52:03 server id 1  end_log_pos 778 CRC32 0x1d659187  Update_rows: table id 105 flags: STMT_END_F
### UPDATE `atguigudb33`.`student`
### WHERE
###   @1=17
###   @2='tom'
###   @3='三班'
### SET
###   @1=17
###   @2='Tom1'
###   @3='三班'
# at 778
#220813 16:52:03 server id 1  end_log_pos 809 CRC32 0xaf15e2a4  Xid = 37
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@centos7-mysql-1 mysql]# 

演示-show binlog events in …;

mysql>  show binlog events in 'binlog.000010';
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
| Log_name      | Pos | Event_type     | Server_id | End_log_pos | Info                                 |
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
| binlog.000010 |   4 | Format_desc    |         1 |         125 | Server ver: 8.0.25, Binlog ver: 4    |
| binlog.000010 | 125 | Previous_gtids |         1 |         156 |                                      |
| binlog.000010 | 156 | Anonymous_Gtid |         1 |         235 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 | 235 | Query          |         1 |         317 | BEGIN                                |
| binlog.000010 | 317 | Table_map      |         1 |         386 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 | 386 | Write_rows     |         1 |         439 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 | 439 | Xid            |         1 |         470 | COMMIT /* xid=36 */                  |
| binlog.000010 | 470 | Anonymous_Gtid |         1 |         549 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 | 549 | Query          |         1 |         640 | BEGIN                                |
| binlog.000010 | 640 | Table_map      |         1 |         709 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 | 709 | Update_rows    |         1 |         778 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 | 778 | Xid            |         1 |         809 | COMMIT /* xid=37 */                  |
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
12 rows in set (0.00 sec)

演示-from

mysql>  show binlog events in 'binlog.000010' from 470;
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
| Log_name      | Pos | Event_type     | Server_id | End_log_pos | Info                                 |
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
| binlog.000010 | 470 | Anonymous_Gtid |         1 |         549 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 | 549 | Query          |         1 |         640 | BEGIN                                |
| binlog.000010 | 640 | Table_map      |         1 |         709 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 | 709 | Update_rows    |         1 |         778 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 | 778 | Xid            |         1 |         809 | COMMIT /* xid=37 */                  |
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
5 rows in set (0.00 sec)
mysql> 

演示-limit

mysql>  show binlog events in 'binlog.000010' from 470 limit 0,2;
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
| Log_name      | Pos | Event_type     | Server_id | End_log_pos | Info                                 |
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
| binlog.000010 | 470 | Anonymous_Gtid |         1 |         549 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 | 549 | Query          |         1 |         640 | BEGIN                                |
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
2 rows in set (0.00 sec)
mysql> 

5.4 使用日志恢复数据

如果MySQL服务器启用了二进制日志,在数据库出现意外丢失数据时,可以使用MySQLbinlog工具从指定的时间点开始(例如,最后一次备份)直到现在或另一个指定的时间点的日志中恢复数据。

mysqlbinlog恢复数据的语法如下:

mysqlbinlog [option] filename|mysql –uuser -ppass;

这个命令可以这样理解:使用mysqlbinlog命令来读取filename中的内容,然后使用mysql命令将这些内容恢复到数据库中。


filename :是日志文件名。

option :可选项,比较重要的两对option参数是–start-date、–stop-date 和–start-position、-- stop-position。

–start-date 和 --stop-date :可以指定恢复数据库的起始时间点和结束时间点。

–start-position和–stop-position :可以指定恢复数据的开始位置和结束位置。

注意:使用mysqlbinlog命令进行恢复操作时,必须是编号小的先恢复,例如atguigu-bin.000001必须在atguigu-bin.000002之前恢复。

案例

现在对student表有以下操作

mysql> use atgiugudb33;
ERROR 1049 (42000): Unknown database 'atgiugudb33'
mysql> use atguigudb33;
Database changed
mysql> select * from student;
+----+---------+--------+
| id | name    | class  |
+----+---------+--------+
|  1 | 张三3   | 一班   |
|  3 | 李四1   | 一班   |
|  6 | jerry   | 一班   |
|  8 | 王五    | 二班   |
| 11 | Tim     | 一班   |
| 15 | 赵六    | 二班   |
| 17 | Tom1    | 三班   |
| 18 | Jerry   | 四班   |
| 20 | 钱七    | 三班   |
+----+---------+--------+
9 rows in set (0.00 sec)
#插入数据
mysql> insert into student(id,name,class) values(21,'aaa','No.1');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student(id,name,class) values(22,'aaa','No.1');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student(id,name,class) values(23,'aaa','No.1');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+---------+--------+
| id | name    | class  |
+----+---------+--------+
|  1 | 张三3   | 一班   |
|  3 | 李四1   | 一班   |
|  6 | jerry   | 一班   |
|  8 | 王五    | 二班   |
| 11 | Tim     | 一班   |
| 15 | 赵六    | 二班   |
| 17 | Tom1    | 三班   |
| 18 | Jerry   | 四班   |
| 20 | 钱七    | 三班   |
| 21 | aaa     | No.1   |
| 22 | aaa     | No.1   |
| 23 | aaa     | No.1   |
+----+---------+--------+
12 rows in set (0.00 sec)
#删除数据
mysql> delete from student where id =21;
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+---------+--------+
| id | name    | class  |
+----+---------+--------+
|  1 | 张三3   | 一班   |
|  3 | 李四1   | 一班   |
|  6 | jerry   | 一班   |
|  8 | 王五    | 二班   |
| 11 | Tim     | 一班   |
| 15 | 赵六    | 二班   |
| 17 | Tom1    | 三班   |
| 18 | Jerry   | 四班   |
| 20 | 钱七    | 三班   |
| 22 | aaa     | No.1   |
| 23 | aaa     | No.1   |
+----+---------+--------+
11 rows in set (0.00 sec)
#更新数据
mysql> update student set name='bbb' where id=22;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from student;
+----+---------+--------+
| id | name    | class  |
+----+---------+--------+
|  1 | 张三3   | 一班   |
|  3 | 李四1   | 一班   |
|  6 | jerry   | 一班   |
|  8 | 王五    | 二班   |
| 11 | Tim     | 一班   |
| 15 | 赵六    | 二班   |
| 17 | Tom1    | 三班   |
| 18 | Jerry   | 四班   |
| 20 | 钱七    | 三班   |
| 22 | bbb     | No.1   |
| 23 | aaa     | No.1   |
+----+---------+--------+
11 rows in set (0.00 sec)
mysql> 

误操作

mysql> delete from student where id > 21;
Query OK, 2 rows affected (0.01 sec)
mysql> select * from student;
+----+---------+--------+
| id | name    | class  |
+----+---------+--------+
|  1 | 张三3   | 一班   |
|  3 | 李四1   | 一班   |
|  6 | jerry   | 一班   |
|  8 | 王五    | 二班   |
| 11 | Tim     | 一班   |
| 15 | 赵六    | 二班   |
| 17 | Tom1    | 三班   |
| 18 | Jerry   | 四班   |
| 20 | 钱七    | 三班   |
+----+---------+--------+
9 rows in set (0.00 sec)
mysql> 

尝试恢复

mysql> show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |       498 | No        |
| binlog.000002 |      1400 | No        |
| binlog.000003 |      7004 | No        |
| binlog.000004 | 647264652 | No        |
| binlog.000005 |   3583532 | No        |
| binlog.000006 | 517896094 | No        |
| binlog.000007 |    139656 | No        |
| binlog.000008 |  34024458 | No        |
| binlog.000009 |      7276 | No        |
| binlog.000010 |      2707 | No        |
+---------------+-----------+-----------+
10 rows in set (0.00 sec)
mysql> 

新增binlog

mysql> flush logs;
Query OK, 0 rows affected (0.03 sec)
mysql> show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |       498 | No        |
| binlog.000002 |      1400 | No        |
| binlog.000003 |      7004 | No        |
| binlog.000004 | 647264652 | No        |
| binlog.000005 |   3583532 | No        |
| binlog.000006 | 517896094 | No        |
| binlog.000007 |    139656 | No        |
| binlog.000008 |  34024458 | No        |
| binlog.000009 |      7276 | No        |
| binlog.000010 |      2751 | No        |
| binlog.000011 |       156 | No        |
+---------------+-----------+-----------+
11 rows in set (0.01 sec)

时间恢复mysqlbinlog 和位置恢复show binlog events in

mysql> show binlog events in 'binlog.000010' ;
+---------------+------+----------------+-----------+-------------+--------------------------------------+
| Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                 |
+---------------+------+----------------+-----------+-------------+--------------------------------------+
| binlog.000010 |    4 | Format_desc    |         1 |         125 | Server ver: 8.0.25, Binlog ver: 4    |
| binlog.000010 |  125 | Previous_gtids |         1 |         156 |                                      |
| binlog.000010 |  156 | Anonymous_Gtid |         1 |         235 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 |  235 | Query          |         1 |         317 | BEGIN                                |
| binlog.000010 |  317 | Table_map      |         1 |         386 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 |  386 | Write_rows     |         1 |         439 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 |  439 | Xid            |         1 |         470 | COMMIT /* xid=36 */                  |
| binlog.000010 |  470 | Anonymous_Gtid |         1 |         549 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 |  549 | Query          |         1 |         640 | BEGIN                                |
| binlog.000010 |  640 | Table_map      |         1 |         709 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 |  709 | Update_rows    |         1 |         778 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 |  778 | Xid            |         1 |         809 | COMMIT /* xid=37 */                  |
| binlog.000010 |  809 | Anonymous_Gtid |         1 |         888 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 |  888 | Query          |         1 |         970 | BEGIN                                |
| binlog.000010 |  970 | Table_map      |         1 |        1039 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 | 1039 | Write_rows     |         1 |        1088 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 | 1088 | Xid            |         1 |        1119 | COMMIT /* xid=49 */                  |
| binlog.000010 | 1119 | Anonymous_Gtid |         1 |        1198 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 | 1198 | Query          |         1 |        1280 | BEGIN                                |
| binlog.000010 | 1280 | Table_map      |         1 |        1349 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 | 1349 | Write_rows     |         1 |        1398 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 | 1398 | Xid            |         1 |        1429 | COMMIT /* xid=50 */                  |
| binlog.000010 | 1429 | Anonymous_Gtid |         1 |        1508 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 | 1508 | Query          |         1 |        1590 | BEGIN                                |
| binlog.000010 | 1590 | Table_map      |         1 |        1659 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 | 1659 | Write_rows     |         1 |        1708 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 | 1708 | Xid            |         1 |        1739 | COMMIT /* xid=51 */                  |
| binlog.000010 | 1739 | Anonymous_Gtid |         1 |        1818 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 | 1818 | Query          |         1 |        1900 | BEGIN                                |
| binlog.000010 | 1900 | Table_map      |         1 |        1969 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 | 1969 | Delete_rows    |         1 |        2018 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 | 2018 | Xid            |         1 |        2049 | COMMIT /* xid=53 */                  |
| binlog.000010 | 2049 | Anonymous_Gtid |         1 |        2128 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 | 2128 | Query          |         1 |        2219 | BEGIN                                |
| binlog.000010 | 2219 | Table_map      |         1 |        2288 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 | 2288 | Update_rows    |         1 |        2352 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 | 2352 | Xid            |         1 |        2383 | COMMIT /* xid=55 */                  |
| binlog.000010 | 2383 | Anonymous_Gtid |         1 |        2462 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000010 | 2462 | Query          |         1 |        2544 | BEGIN                                |
| binlog.000010 | 2544 | Table_map      |         1 |        2613 | table_id: 105 (atguigudb33.student)  |
| binlog.000010 | 2613 | Delete_rows    |         1 |        2676 | table_id: 105 flags: STMT_END_F      |
| binlog.000010 | 2676 | Xid            |         1 |        2707 | COMMIT /* xid=57 */                  |
| binlog.000010 | 2707 | Rotate         |         1 |        2751 | binlog.000011;pos=4                  |
+---------------+------+----------------+-----------+-------------+--------------------------------------+
43 rows in set (0.00 sec)

思路:先恢复3个insert49 50 51,再恢复delete 53,最后update55

查看日志发现,在备份数据后首先执行的是插入数据操作,在Info信息中xid的值分别是49、50、51

步骤1:恢复插入的数据



插入的操作在三个事务中,而且三个事务是连在一起的,所以我们可以直接恢复三个事务,开始pos是888,结束pos是1739

[root@centos7-mysql-1 mysql]# /usr/bin/mysqlbinlog --start-position=初位置 --stop-position=初位置 --database=数据库名 /var/lib/mysql/日志文件 | /usr/bin/mysql -uroot -p密码 -v 数据库名 
[root@centos7-mysql-1 mysql]# /usr/bin/mysqlbinlog --start-position=888 --stop-position=1739 --database=atguigudb33 /var/lib/mysql/binlog.000010 | /usr/bin/mysql -uroot -p123456 -v atguigudb33
mysql: [Warning] Using a password on the command line interface can be insecure.
--------------
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/
--------------
--------------
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/
--------------
--------------
ROLLBACK
--------------
--------------
BINLOG '
fFf3Yg8BAAAAeQAAAH0AAAAAAAQAOC4wLjI1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAB8V/diEwANAAgAAAAABAAEAAAAYQAEGggAAAAICAgCAAAACgoKKioAEjQA
CigBC021uA==
'
--------------
--------------
SET TIMESTAMP=1660382438
--------------
--------------
SET @@session.pseudo_thread_id=8
--------------
--------------
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1
--------------
--------------
SET @@session.sql_mode=1168113696
--------------
--------------
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1
--------------
--------------
/*!*/
--------------
--------------
SET @@session.character_set_client=255,@@session.collation_connection=255,@@session.collation_server=255
--------------
--------------
SET @@session.lc_time_names=0
--------------
--------------
SET @@session.collation_database=DEFAULT
--------------
--------------
/*!80011 SET @@session.default_collation_for_utf8mb4=255*/
--------------
--------------
BEGIN
--------------
--------------
BINLOG '
5mz3YhMBAAAARQAAAA8EAAAAAGkAAAAAAAEAC2F0Z3VpZ3VkYjMzAAdzdHVkZW50AAMDDw8EPAAe
AAYBAQACASF++w+W
5mz3Yh4BAAAAMQAAAEAEAAAAAGkAAAAAAAEAAgAD/wAVAAAAA2FhYQROby4xpdPUJw==
'
--------------
--------------
COMMIT
--------------
--------------
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*/
--------------
--------------
/*!80001 SET @@session.original_commit_timestamp=1660382450601605*/
--------------
--------------
/*!80014 SET @@session.original_server_version=80025*/
--------------
--------------
/*!80014 SET @@session.immediate_server_version=80025*/
--------------
--------------
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
--------------
--------------
SET TIMESTAMP=1660382450
--------------
--------------
BEGIN
--------------
--------------
BINLOG '
8mz3YhMBAAAARQAAAEUFAAAAAGkAAAAAAAEAC2F0Z3VpZ3VkYjMzAAdzdHVkZW50AAMDDw8EPAAe
AAYBAQACASE/IGTT
8mz3Yh4BAAAAMQAAAHYFAAAAAGkAAAAAAAEAAgAD/wAWAAAAA2FhYQROby4xA/taIA==
'
--------------
--------------
COMMIT
--------------
--------------
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*/
--------------
--------------
/*!80001 SET @@session.original_commit_timestamp=1660382453574396*/
--------------
--------------
/*!80014 SET @@session.original_server_version=80025*/
--------------
--------------
/*!80014 SET @@session.immediate_server_version=80025*/
--------------
--------------
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'
--------------
--------------
SET TIMESTAMP=1660382453
--------------
--------------
BEGIN
--------------
--------------
BINLOG '
9Wz3YhMBAAAARQAAAHsGAAAAAGkAAAAAAAEAC2F0Z3VpZ3VkYjMzAAdzdHVkZW50AAMDDw8EPAAe
AAYBAQACASHBAPJL
9Wz3Yh4BAAAAMQAAAKwGAAAAAGkAAAAAAAEAAgAD/wAXAAAAA2FhYQROby4x11XCaw==
'
--------------
--------------
COMMIT
--------------
--------------
SET @@SESSION.GTID_NEXT= 'AUTOMATIC'
--------------
--------------
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/
--------------
--------------
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/
--------------
[root@centos7-mysql-1 mysql]# 

结果如下

mysql> select * from student;
+----+---------+--------+
| id | name    | class  |
+----+---------+--------+
|  1 | 张三3   | 一班   |
|  3 | 李四1   | 一班   |
|  6 | jerry   | 一班   |
|  8 | 王五    | 二班   |
| 11 | Tim     | 一班   |
| 15 | 赵六    | 二班   |
| 17 | Tom1    | 三班   |
| 18 | Jerry   | 四班   |
| 20 | 钱七    | 三班   |
| 21 | aaa     | No.1   |
| 22 | aaa     | No.1   |
| 23 | aaa     | No.1   |
+----+---------+--------+
12 rows in set (0.00 sec)
mysql> 
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2天前
|
SQL 存储 关系型数据库
Mysql并发控制和日志
通过深入理解和应用 MySQL 的并发控制和日志管理技术,您可以显著提升数据库系统的效率和稳定性。
25 10
|
12天前
|
关系型数据库 MySQL 数据库
Python处理数据库:MySQL与SQLite详解 | python小知识
本文详细介绍了如何使用Python操作MySQL和SQLite数据库,包括安装必要的库、连接数据库、执行增删改查等基本操作,适合初学者快速上手。
81 15
|
5天前
|
SQL 关系型数据库 MySQL
数据库数据恢复—Mysql数据库表记录丢失的数据恢复方案
Mysql数据库故障: Mysql数据库表记录丢失。 Mysql数据库故障表现: 1、Mysql数据库表中无任何数据或只有部分数据。 2、客户端无法查询到完整的信息。
|
12天前
|
关系型数据库 MySQL 数据库
数据库数据恢复—MYSQL数据库文件损坏的数据恢复案例
mysql数据库文件ibdata1、MYI、MYD损坏。 故障表现:1、数据库无法进行查询等操作;2、使用mysqlcheck和myisamchk无法修复数据库。
|
15天前
|
SQL 存储 缓存
MySQL进阶突击系列(02)一条更新SQL执行过程 | 讲透undoLog、redoLog、binLog日志三宝
本文详细介绍了MySQL中update SQL执行过程涉及的undoLog、redoLog和binLog三种日志的作用及其工作原理,包括它们如何确保数据的一致性和完整性,以及在事务提交过程中各自的角色。同时,文章还探讨了这些日志在故障恢复中的重要性,强调了合理配置相关参数对于提高系统稳定性的必要性。
|
16天前
|
SQL 关系型数据库 MySQL
MySQL导入.sql文件后数据库乱码问题
本文分析了导入.sql文件后数据库备注出现乱码的原因,包括字符集不匹配、备注内容编码问题及MySQL版本或配置问题,并提供了详细的解决步骤,如检查和统一字符集设置、修改客户端连接方式、检查MySQL配置等,确保导入过程顺利。
|
SQL Java 数据库连接
MySQL---数据库从入门走向大神系列(十五)-Apache的DBUtils框架使用
MySQL---数据库从入门走向大神系列(十五)-Apache的DBUtils框架使用
195 0
MySQL---数据库从入门走向大神系列(十五)-Apache的DBUtils框架使用
|
SQL 关系型数据库 MySQL
MySQL---数据库从入门走向大神系列(六)-事务处理与事务隔离(锁机制)
MySQL---数据库从入门走向大神系列(六)-事务处理与事务隔离(锁机制)
146 0
MySQL---数据库从入门走向大神系列(六)-事务处理与事务隔离(锁机制)
|
存储 SQL 关系型数据库
MySQL---数据库从入门走向大神系列(五)-存储过程
MySQL---数据库从入门走向大神系列(五)-存储过程
143 0
MySQL---数据库从入门走向大神系列(五)-存储过程
|
数据库
MySQL---数据库从入门走向大神系列(四)-子查询、表与表之间的关系(3)
MySQL---数据库从入门走向大神系列(四)-子查询、表与表之间的关系
211 0
MySQL---数据库从入门走向大神系列(四)-子查询、表与表之间的关系(3)
下一篇
DataWorks