mysql> create database test1; Query OK, 1 row affected (0.01 sec) mysql> begin; mysql> update city SET Name='Tilburo'whereid=10; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from city whereid=10; +----+---------+-------------+---------------+------------+ | ID | Name | CountryCode | District | Population | +----+---------+-------------+---------------+------------+ | 10 | Tilburo | NLD | Noord-Brabant | 193238 | +----+---------+-------------+---------------+------------+ mysql> commit;
[root@db01 /data/binlog]# mysqlbinlog mysql-bin.000007 |grep -v "SET" >/tmp/aa.txt [root@db01 ~]# mysqlbinlog /data/3307/mysql-bin.000007 |grep -v "SET" DELIMITER /*!*/; # at 4 #200415 11:09:01 server id 7 end_log_pos 123 CRC32 0x529742df Start: binlog v 4, server v 5.7.26-log created 200415 11:09:01 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' zXqWXg8HAAAAdwAAAHsAAAABAAQANS43LjI2LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAADNepZeEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA Ad9Cl1I= '/*!*/; # at 123 #200415 11:09:01 server id 7 end_log_pos 154 CRC32 0xf70987e6 Previous-GTIDs # [empty]
//从154字节开始看 # at 154 #200415 14:55:40 server id 7 end_log_pos 219 CRC32 0x5c02f208 Anonymous_GTID last_committed=0 sequence_number=1 rbr_only=no
# at 219 #200415 14:55:40 server id 7 end_log_pos 316 CRC32 0x1e0dd89b Query thread_id=5 exec_time=0 error_code=0 /*!\C utf8 *//*!*/; create database test1 /*!*/; //创建了一个库,DDL语句直接记录语句本事。
# at 316 #200415 15:04:23 server id 7 end_log_pos 381 CRC32 0x7c43eb07 Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=yes
# at 381 #200415 15:03:46 server id 7 end_log_pos 454 CRC32 0x6f398c9d Query thread_id=5 exec_time=0 error_code=0 BEGIN /*!*/; # at 454 //事务开始
#200415 15:03:46 server id 7 end_log_pos 512 CRC32 0x223247ee Table_map: `world`.`city` mapped to number 112 # at 512 #200415 15:03:46 server id 7 end_log_pos 618 CRC32 0xdac21634 Update_rows: table id 112 flags: STMT_END_F
[root@db01 ~]# mysqlbinlog --start-position=1800 --stop-position=2822 /data/3307/mysql-bin.000007 >/tmp/bin2.sql; //截取需要的日志 [root@db01 ~]# cat /tmp/bin2.sql /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #200415 11:09:01 server id 7 end_log_pos 123 CRC32 0x529742df Start: binlog v 4, server v 5.7.26-log created 200415 11:09:01 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' zXqWXg8HAAAAdwAAAHsAAAABAAQANS43LjI2LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAADNepZeEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA Ad9Cl1I= '/*!*/; # at 1800 #200415 14:55:40 server id 7 end_log_pos 1916 CRC32 0x7d0a9649 Query thread_id=5 exec_time=3793 error_code=0 SET TIMESTAMP=1586933740/*!*/; SET @@session.pseudo_thread_id=5/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=1436549152/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; create database binlog charset utf8mb4 /*!*/; # at 1916 #200415 14:55:40 server id 7 end_log_pos 1981 CRC32 0x0335ab8d Anonymous_GTID last_committed=8 sequence_number=9 rbr_only=no SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 1981 #200415 14:55:40 server id 7 end_log_pos 2082 CRC32 0xe4331fa1 Query thread_id=5 exec_time=3793 error_code=0 use `binlog`/*!*/; SET TIMESTAMP=1586933740/*!*/; create table t1(id int) /*!*/; # at 2082 #200415 14:55:40 server id 7 end_log_pos 2147 CRC32 0xd9ff1631 Anonymous_GTID last_committed=9 sequence_number=10 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 2147 #200415 14:55:40 server id 7 end_log_pos 2221 CRC32 0x1b004ae2 Query thread_id=5 exec_time=3793 error_code=0 SET TIMESTAMP=1586933740/*!*/; BEGIN /*!*/; # at 2221 #200415 14:55:40 server id 7 end_log_pos 2268 CRC32 0x9d41f6ed Table_map: `binlog`.`t1` mapped to number 117 # at 2268 #200415 14:55:40 server id 7 end_log_pos 2308 CRC32 0xcc11e52b Write_rows: table id 117 flags: STMT_END_F
BINLOG ' 7K+WXhMHAAAALwAAANwIAAAAAHUAAAAAAAEABmJpbmxvZwACdDEAAQMAAe32QZ0= 7K+WXh4HAAAAKAAAAAQJAAAAAHUAAAAAAAEAAgAB//4BAAAAK+URzA== '/*!*/; # at 2308 #200415 14:55:40 server id 7 end_log_pos 2339 CRC32 0x891d519e Xid = 105 COMMIT/*!*/; # at 2339 #200415 14:55:40 server id 7 end_log_pos 2404 CRC32 0xe5f12f2c Anonymous_GTID last_committed=10 sequence_number=11 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 2404 #200415 14:55:40 server id 7 end_log_pos 2478 CRC32 0x2f6a2f99 Query thread_id=5 exec_time=3793 error_code=0 SET TIMESTAMP=1586933740/*!*/; BEGIN /*!*/; # at 2478 #200415 14:55:40 server id 7 end_log_pos 2525 CRC32 0xc226ccc1 Table_map: `binlog`.`t1` mapped to number 117 # at 2525 #200415 14:55:40 server id 7 end_log_pos 2565 CRC32 0x7caf0d51 Write_rows: table id 117 flags: STMT_END_F
BINLOG ' 7K+WXhMHAAAALwAAAN0JAAAAAHUAAAAAAAEABmJpbmxvZwACdDEAAQMAAcHMJsI= 7K+WXh4HAAAAKAAAAAUKAAAAAHUAAAAAAAEAAgAB//4CAAAAUQ2vfA== '/*!*/; # at 2565 #200415 14:55:40 server id 7 end_log_pos 2596 CRC32 0x133f90ed Xid = 107 COMMIT/*!*/; # at 2596 #200415 14:55:40 server id 7 end_log_pos 2661 CRC32 0xc76646d7 Anonymous_GTID last_committed=11 sequence_number=12 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 2661 #200415 14:55:40 server id 7 end_log_pos 2735 CRC32 0x117fc984 Query thread_id=5 exec_time=3793 error_code=0 SET TIMESTAMP=1586933740/*!*/; BEGIN /*!*/; # at 2735 #200415 14:55:40 server id 7 end_log_pos 2782 CRC32 0x238f82b5 Table_map: `binlog`.`t1` mapped to number 117 # at 2782 #200415 14:55:40 server id 7 end_log_pos 2822 CRC32 0xa5de37c9 Write_rows: table id 117 flags: STMT_END_F
BINLOG ' 7K+WXhMHAAAALwAAAN4KAAAAAHUAAAAAAAEABmJpbmxvZwACdDEAAQMAAbWCjyM= 7K+WXh4HAAAAKAAAAAYLAAAAAHUAAAAAAAEAAgAB//4DAAAAyTfepQ== '/*!*/; ROLLBACK /* added by mysqlbinlog */ /*!*/; 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*/;
1 2
mysql> set sql_log_bin=0; #关闭当前回话的binlog记录 mysql> source /tmp/bin2.sql
验证数据是否恢复:
1 2 3 4 5 6 7 8
mysql> select id from t1; +------+ | id | +------+ | 1 | | 2 | | 3 | +------+