- 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>