暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

MySQL日常运维之MySQL binlog日志内容分析

数据与人 2020-12-15
678

MySQL日常运维之MySQL binlog日志内容分析


1> binlog日志

MySQL的binlog 日志对于生产环境非常有用,任何时间对数据库的修改都会记录在binglog中;
当数据发生增删改,创建数据库对象都会记录到binlog中,数据库的复制也是基于binlog进行同步数据;
和SQL SERVER 数据库开启完整模式的原理一样,每一次的数据的变动都会记录在案;(对数据库的select,show这些操作不会记录在binlog)

下面介绍mysqlbinlog工具查看 二进制日志内容;

示例
1> 产生新的binlog日志 flush logs

1> 测试数据

    create table test1 (id int);
    insert into test1 values (1);
    insert into test1 values (2);
    insert into test1 values (3);
    delete from test1 where id=2;

    2> 最新的binlog mysqlbinlog.000012


      [root@localhost][(none)]> show binlog events in 'mysqlbinlog.000012';
      +--------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------+
      | Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
      +--------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------+
      | mysqlbinlog.000012 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.25-log, Binlog ver: 4 |
      | mysqlbinlog.000012 | 123 | Previous_gtids | 1 | 194 | 6e854266-e13f-11e9-9630-000c29ec17fe:8-40225 |
      | mysqlbinlog.000012 | 194 | Gtid | 1 | 259 | SET @@SESSION.GTID_NEXT= '6e854266-e13f-11e9-9630-000c29ec17fe:40226' |
      | mysqlbinlog.000012 | 259 | Query | 1 | 334 | BEGIN |
      | mysqlbinlog.000012 | 334 | Rows_query | 1 | 386 | # insert into test1 values (1) |
      | mysqlbinlog.000012 | 386 | Table_map | 1 | 437 | table_id: 111 (ecology.test1) |
      | mysqlbinlog.000012 | 437 | Write_rows | 1 | 477 | table_id: 111 flags: STMT_END_F |
      | mysqlbinlog.000012 | 477 | Xid | 1 | 508 | COMMIT * xid=24229 */ |
      | mysqlbinlog.000012 | 508 | Gtid | 1 | 573 | SET @@SESSION.GTID_NEXT= '6e854266-e13f-11e9-9630-000c29ec17fe:40227' |
      | mysqlbinlog.000012 | 573 | Query | 1 | 648 | BEGIN |
      | mysqlbinlog.000012 | 648 | Rows_query | 1 | 700 | # insert into test1 values (2) |
      | mysqlbinlog.000012 | 700 | Table_map | 1 | 751 | table_id: 111 (ecology.test1) |
      | mysqlbinlog.000012 | 751 | Write_rows | 1 | 791 | table_id: 111 flags: STMT_END_F |
      | mysqlbinlog.000012 | 791 | Xid | 1 | 822 | COMMIT * xid=24230 */ |
      | mysqlbinlog.000012 | 822 | Gtid | 1 | 887 | SET @@SESSION.GTID_NEXT= '6e854266-e13f-11e9-9630-000c29ec17fe:40228' |
      | mysqlbinlog.000012 | 887 | Query | 1 | 962 | BEGIN |
      | mysqlbinlog.000012 | 962 | Rows_query | 1 | 1014 | # insert into test1 values (3) |
      | mysqlbinlog.000012 | 1014 | Table_map | 1 | 1065 | table_id: 111 (ecology.test1) |
      | mysqlbinlog.000012 | 1065 | Write_rows | 1 | 1105 | table_id: 111 flags: STMT_END_F |
      | mysqlbinlog.000012 | 1105 | Xid | 1 | 1136 | COMMIT * xid=24231 */ |
      | mysqlbinlog.000012 | 1136 | Gtid | 1 | 1201 | SET @@SESSION.GTID_NEXT= '6e854266-e13f-11e9-9630-000c29ec17fe:40229' |
      | mysqlbinlog.000012 | 1201 | Query | 1 | 1276 | BEGIN |
      | mysqlbinlog.000012 | 1276 | Rows_query | 1 | 1329 | # delete from test1 where id =2 |
      | mysqlbinlog.000012 | 1329 | Table_map | 1 | 1380 | table_id: 111 (ecology.test1) |
      | mysqlbinlog.000012 | 1380 | Delete_rows | 1 | 1420 | table_id: 111 flags: STMT_END_F |
      | mysqlbinlog.000012 | 1420 | Xid | 1 | 1451 | COMMIT * xid=24234 */ |
      +--------------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------+


      3> 用mysqlbinlog 工具查看

        [mysql@mysql mysqlbinlog]$ which mysqlbinlog
        /mysql/mysql57/bin/mysqlbinlog

        所有的内容全部转换

          mysqlbinlog --base64-output=decode-rows -v mysqlbinlog.000012 > mysqlbinlog.000012.sql

          也可以筛选时间

            mysqlbinlog --no-defaults --database=ecology --base64-output=decode-rows -v --start-datetime='2019-11-19 00:00:00' --stop-datetime='2019-11-19 15:00:00' mysqlbinlog.000012 | more

            如下


              [mysql@mysql mysqlbinlog]$ cat mysqlbinlog.000012.sql 
              /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
              /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
              DELIMITER /*!*/;
              # at 4
              #191119 3:41:22 server id 1 end_log_pos 123 CRC32 0xece7d88c Start: binlog v 4, server v 5.7.25-log created 191119 3:41:22
              # Warning: this binlog is either in use or was not closed properly.
              # at 123
              #191119 3:41:22 server id 1 end_log_pos 194 CRC32 0x5061dfbc Previous-GTIDs
              # 6e854266-e13f-11e9-9630-000c29ec17fe:8-40225
              # at 194
              #191119 3:42:07 server id 1 end_log_pos 259 CRC32 0xec2ec1b9 GTID last_committed=0sequence_number=1 rbr_only=yes
              /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
              SET @@SESSION.GTID_NEXT= '6e854266-e13f-11e9-9630-000c29ec17fe:40226'/*!*/;
              # at 259
              #191119 3:42:07 server id 1 end_log_pos 334 CRC32 0x9bc6ec1d Query thread_id=2011 exec_time=0 error_code=0
              SET TIMESTAMP=1574163727/*!*/;
              SET @@session.pseudo_thread_id=2011/*!*/;
              SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
              SET @@session.sql_mode=1436549120/*!*/;
              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=45/*!*/;
              SET @@session.lc_time_names=0/*!*/;
              SET @@session.collation_database=DEFAULT/*!*/;
              BEGIN
              /*!*/;
              # at 334
              # at 386
              #191119 3:42:07 server id 1 end_log_pos 437 CRC32 0xf0b1b42a Table_map: `ecology`.`test1` mapped to number 111
              # at 437
              #191119 3:42:07 server id 1 end_log_pos 477 CRC32 0x0a785505 Write_rows: table id 111 flags: STMT_END_F
              ### INSERT INTO `ecology`.`test1`
              ### SET
              ### @1=1
              # at 477
              #191119 3:42:07 server id 1 end_log_pos 508 CRC32 0x22330a62 Xid = 24229
              COMMIT/*!*/;
              # at 508
              #191119 3:42:11 server id 1 end_log_pos 573 CRC32 0x1a4a0ef6 GTID last_committed=1sequence_number=2 rbr_only=yes
              /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
              SET @@SESSION.GTID_NEXT= '6e854266-e13f-11e9-9630-000c29ec17fe:40227'/*!*/;
              # at 573
              #191119 3:42:11 server id 1 end_log_pos 648 CRC32 0xaa2c2f1c Query thread_id=2011 exec_time=0 error_code=0
              SET TIMESTAMP=1574163731/*!*/;
              BEGIN
              /*!*/;
              # at 648
              # at 700
              #191119 3:42:11 server id 1 end_log_pos 751 CRC32 0x0c3582e7 Table_map: `ecology`.`test1` mapped to number 111
              # at 751
              #191119 3:42:11 server id 1 end_log_pos 791 CRC32 0x7c5d0311 Write_rows: table id 111 flags: STMT_END_F
              ### INSERT INTO `ecology`.`test1`
              ### SET
              ### @1=2
              # at 791
              #191119 3:42:11 server id 1 end_log_pos 822 CRC32 0x21aa6cd8 Xid = 24230
              COMMIT/*!*/;
              # at 822
              #191119 3:42:14 server id 1 end_log_pos 887 CRC32 0x28978885 GTID last_committed=2sequence_number=3 rbr_only=yes
              /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
              SET @@SESSION.GTID_NEXT= '6e854266-e13f-11e9-9630-000c29ec17fe:40228'/*!*/;
              # at 887
              #191119 3:42:14 server id 1 end_log_pos 962 CRC32 0x1eb58956 Query thread_id=2011 exec_time=0 error_code=0
              SET TIMESTAMP=1574163734/*!*/;
              BEGIN
              /*!*/;
              # at 962
              # at 1014
              #191119 3:42:14 server id 1 end_log_pos 1065 CRC32 0x16e28b1c Table_map: `ecology`.`test1` mapped to number 111
              # at 1065
              #191119 3:42:14 server id 1 end_log_pos 1105 CRC32 0xe418bd7c Write_rows: table id 111 flags: STMT_END_F
              ### INSERT INTO `ecology`.`test1`
              ### SET
              ### @1=3
              # at 1105
              #191119 3:42:14 server id 1 end_log_pos 1136 CRC32 0x26c5a65d Xid = 24231
              COMMIT/*!*/;
              # at 1136
              #191119 3:42:53 server id 1 end_log_pos 1201 CRC32 0xb302f8f8 GTID last_committed=3 sequence_number=4 rbr_only=yes
              /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
              SET @@SESSION.GTID_NEXT= '6e854266-e13f-11e9-9630-000c29ec17fe:40229'/*!*/;
              # at 1201
              #191119 3:42:53 server id 1 end_log_pos 1276 CRC32 0x068cf9e9 Query thread_id=2011 exec_time=0 error_code=0
              SET TIMESTAMP=1574163773/*!*/;
              BEGIN
              /*!*/;
              # at 1276
              # at 1329
              #191119 3:42:53 server id 1 end_log_pos 1380 CRC32 0xfbcaff53 Table_map: `ecology`.`test1` mapped to number 111
              # at 1380
              #191119 3:42:53 server id 1 end_log_pos 1420 CRC32 0x5f3583dd Delete_rows: table id 111 flags: STMT_END_F
              ### DELETE FROM `ecology`.`test1`
              ### WHERE
              ### @1=2
              # at 1420
              #191119 3:42:53 server id 1 end_log_pos 1451 CRC32 0x46e9d878 Xid = 24234
              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*/;




              往期回顾


              MySQL日常运维之 MySQL安装部署(二进制)
              MySQL日常运维之 MySQL双主复制+keepalived+haproxy配置(负载均衡)
              MySQL日常运维之percona-toolkit工具pt-query-digest命令详解


              客官长按关注

              吾辈自强不息


              文章转载自数据与人,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

              评论