在使用RBR也就是行格式的时候,去解析binlog,需要逆向才能分析出对应的原始SQL是什么,而且,里面对应的是每一条具体行变更的内容。当然,你可以开启general log,但如果我们需要的只是记录对应的行变更,而不需要记录这些select普通的查询,因为general log 会将线上所有的操作都记录下来,这种功能适合于我们审核统计,但是不适合我们对事务进行判断,故此,我们使用binlog_rows_query_log_events进行查看。在官网中的解析如下

binlog_rows_query_log_events

The binlog_rows_query_log_events system variable affects row-based logging only. When enabled, it causes a MySQL 5.6.2 or later server
to write informational log events such as row query log events into its binary log. This information can be used for debugging and related purposes;
such as obtaining the original query issued on the master when it cannot be reconstructed from the row updates. These events are normally ignored by MySQL programs reading the binary log and so cause no issues when replicating or restoring from backup.
To view them, increase the verbosity level by using mysqlbinlog's --verbose option twice, either as "-vv" or "--verbose --verbose".  

可以看到,它是一个动态的,全局,会话型的变量,即可以通过SQL模式进行关闭开启。

在未开启状态中的使用

[root@localhost][boss]> flush logs;
Query OK, 0 rows affected (0.90 sec) [root@localhost][boss]> flush logs;
Query OK, 0 rows affected (0.18 sec) [root@gzx-master-01 logs]# ll
total 8977656
-rw-r----- 1 mysql mysql 79405 Apr 24 08:42 error.log
-rw-r----- 1 mysql mysql 217 Mar 10 10:42 mysql_bin.000013
-rw-r----- 1 mysql mysql 658363086 Apr 24 08:43 mysql_bin.000027
-rw-r----- 1 mysql mysql 241 Apr 24 08:43 mysql_bin.000028
-rw-r----- 1 mysql mysql 194 Apr 24 08:43 mysql_bin.000029
-rw-r----- 1 mysql mysql 194 Apr 24 08:53 mysql_bin.000030 -rw-r----- 1 mysql mysql 132 Apr 24 08:43 mysql_bin.index -rw-r----- 1 mysql mysql 8534643198 Apr 24 08:43 slow.log  

目前最新的binlog切换到30的文件中,做对应的DML

[root@localhost][boss]> select * from t;
+------+
| id |
+------+
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 2 |
| 2 |
| 2 |
| 3 |
| 3 |
| 7 |
+------+
11 rows in set (0.00 sec) [root@localhost][boss]> update t set id = 8 where id=7;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0 [root@localhost][boss]> select * from t;
+------+
| id |
+------+
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 2 |
| 2 |
| 2 |
| 3 |
| 3 |
| 8 |
+------+
11 rows in set (0.00 sec) 

查看binlog

[root@gzx-master- logs]# mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql_bin.
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at
# :: server id end_log_pos CRC32 0x48e80ee3 Start: binlog v , server v 5.7.-log created ::
# Warning: this binlog is either in use or was not closed properly.
# at
# :: server id end_log_pos CRC32 0xaf41def6 Previous-GTIDs
# a0c06ec7-fef0-11e6-9f85-525400a7d662:-
# at
# :: server id end_log_pos CRC32 0xac26b3b1 GTID last_committed=sequence_number=
SET @@SESSION.GTID_NEXT= 'a0c06ec7-fef0-11e6-9f85-525400a7d662:813'/*!*/;
# at
# :: server id end_log_pos CRC32 0xb56db594 Query thread_id= exec_time= error_code=
SET TIMESTAMP=/*!*/;
SET @@session.pseudo_thread_id=/*!*/;
SET @@session.foreign_key_checks=, @@session.sql_auto_is_null=, @@session.unique_checks=, @@session.autocommit=/*!*/;
SET @@session.sql_mode=/*!*/;
SET @@session.auto_increment_increment=, @@session.auto_increment_offset=/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=,@@session.collation_connection=,@@session.collation_server=/*!*/;
SET @@session.lc_time_names=/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at
# :: server id end_log_pos CRC32 0x88acf1be Rows_query # at
# :: server id end_log_pos CRC32 0xf2505403 Update_rows: table id flags: STMT_END_F
### UPDATE `boss`.`t`
### WHERE
### @= /* INT meta=0 nullable=1 is_null=0 */
### SET
### @= /* INT meta=0 nullable=1 is_null=0 */
# at
# :: server id end_log_pos CRC32 0xefebd387 Xid =
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@localhost][(none)]> set global binlog_rows_query_log_events=on;

再次执行大致的SQL,查询日志发现

[root@localhost][boss]> set global binlog_rows_query_log_events=on;
Query OK, 0 rows affected (0.00 sec) [root@localhost][boss]> flush logs;
Query OK, 0 rows affected (0.41 sec) [root@localhost][boss]> update t set id = 9 where id=8;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
[root@gzx-master-01 logs]# mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql_bin.000031
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#170424 9:03:47 server id 37306 end_log_pos 123 CRC32 0xa04cc14a Start: binlog v 4, server v 5.7.17-log created 170424 9:03:47
# Warning: this binlog is either in use or was not closed properly.
# at 123
#170424 9:03:47 server id 37306 end_log_pos 194 CRC32 0xc3c3b085 Previous-GTIDs
# a0c06ec7-fef0-11e6-9f85-525400a7d662:1-813
# at 194
#170424 9:03:55 server id 37306 end_log_pos 259 CRC32 0xbf9d2c93 GTID last_committed=sequence_number=1
SET @@SESSION.GTID_NEXT= 'a0c06ec7-fef0-11e6-9f85-525400a7d662:814'/*!*/;
# at 259
#170424 9:03:55 server id 37306 end_log_pos 331 CRC32 0xeddbf07c Query thread_id=34 exec_time=0 error_code=0
SET TIMESTAMP=1492995835/*!*/;
SET @@session.pseudo_thread_id=34/*!*/;
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=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 331
#170424 9:03:55 server id 37306 end_log_pos 385 CRC32 0x912f4086 Rows_query
# update t set id = 9 where id=8
# at 385
#170424 9:03:55 server id 37306 end_log_pos 429 CRC32 0x16eec7d7 Table_map: `boss`.`t` mapped to number 118
# at 429
#170424 9:03:55 server id 37306 end_log_pos 475 CRC32 0x920862af Update_rows: table id 118 flags: STMT_END_F
### UPDATE `boss`.`t`
### WHERE
### @1=8 /* INT meta=0 nullable=1 is_null=0 */
### SET
### @1=9 /* INT meta=0 nullable=1 is_null=0 */
# at 475
#170424 9:03:55 server id 37306 end_log_pos 506 CRC32 0xa37b82e5 Xid = 2447
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*/;

看下划线的地方,可以比较清楚的知道,对应的SQL是什么。

MySQL中binlog参数:binlog_rows_query_log_events的更多相关文章

  1. MySQL中binlog参数:binlog_rows_query_log_events-记录具体的SQL【转】

    在使用RBR也就是行格式的时候,去解析binlog,需要逆向才能分析出对应的原始SQL是什么,而且,里面对应的是每一条具体行变更的内容.当然,你可以开启general log,但如果我们需要的只是记录 ...

  2. MYSQL 中binlog 参数的记录

    http://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html binlog_cache_size Command ...

  3. maxwell实时同步mysql中binlog

    概述 Maxwell是一个能实时读取MySQL二进制日志binlog,并生成 JSON 格式的消息,作为生产者发送给 Kafka,Kinesis.RabbitMQ.Redis.Google Cloud ...

  4. mysql中binlog与存储引擎的2PC

    mysql内部的2PC mysql开启binlog后实际上可以认为其数据有两份,binlog中一份,引擎中一份(这里先把存储引擎中数据看成整体的单独一份,另外也可以把binlog看成是一个引擎).既然 ...

  5. MySQL中bin-log使用

    操作命令:show binlog events ; reset master 删除所有的二进制日志 flush logs 产生一个新的binlog日志文件 show master logs; 或者 s ...

  6. mysql中max_allowed_packet参数的配置方法(避免大数据写入或者更新失败)

    修改方法 1.修改配置文件 可以编辑my.cnf来修改(windows下my.ini),在[mysqld]段或者mysql的server配置段进行修改. 代码如下: max_allowed_packe ...

  7. 【转】【MySQL】mysql 通过bin-log恢复数据方法详解

    mysql中bin-log在mysql默认状态下是没有打开的,我们要先打开mysql 开启bin-log功能,然后再通过备份的bin-log进行数据库恢复了. 具体的操作是通过mysqlbinlog这 ...

  8. sql点滴37—mysql中的错误Data too long for column '' at row 1

    原文:sql点滴37-mysql中的错误Data too long for column '' at row 1   1.MYSQL服务 我的电脑——(右键)管理——服务与应用程序——服务——MYSQ ...

  9. (4.6)mysql备份还原——深入解析二进制日志(2)binlog参数配置解析

    关键词:binlog配置,binlog参数,二进制日志配置,二进制文件参数配置 关键词:binlog缓存,binlog 刷新 0.bin写入流程 写binlog流程如下:# 数据操作buffer po ...

随机推荐

  1. Android ORMLite 框架的入门用法

    大家在Android项目中或多或少的都会使用数据库,为了提高我们的开发效率,当然少不了数据库ORM框架了,尤其是某些数据库操作特别频繁的app:本篇博客将详细介绍ORMLite的简易用法. 下面开始介 ...

  2. PHP语言开发微信公众平台(订阅号)之curl命令

    在开发过程中,经常会遇到要求用curl命令调用接口的情况 那么,什么是curl,简单来说curl是一个利用url语法规定来传输文件和哦数据的工具,支持很多协议,如 http.ftp.telent 等, ...

  3. TCP的连接和建立 图解

    前言 在没有理解TCP连接是如何建立和终止之前,我想你可能并不会使用connect,accept,close这三个函数并且使用netstat程序来调试应用.所以掌握TCP连接的建立和终止势在必行. 三 ...

  4. 用js控制css属性

    在用js控制css属性时,行内css属性可以任意控制,但若是在<style></style>中写的css属性,均不能用alert读取,但是赋值却有几种现象, 第一种:无法读取, ...

  5. 【原】老生常谈-从输入url到页面展示到底发生了什么

    刚开始写这篇文章还是挺纠结的,因为网上搜索“从输入url到页面展示到底发生了什么”,你可以搜到一大堆的资料.而且面试这道题基本是必考题,二月份面试的时候,虽然知道这个过程发生了什么,不过当面试官一步步 ...

  6. Android开发遇到手机无法弹出Toast

    今天遇到了一个很奇怪的问题,一个很简单的程序,就是点击按钮弹出一个Toast,但在手机上运行起来,却没有正常弹出Toast 第一反应就是看看是否调用了show方法,很显然,并不是这个低级问题,为了确定 ...

  7. Page-Object设计模式

    自动化脚本初写之际一定是只求完成功能测试,页面by.id.by.name.by.xpath满篇飞.业务逻辑代码重复率也是越来越高.慢慢的写着写着开始重构,开始封装一些方法.代码量好一些的人会在代码开始 ...

  8. 为什么要学Python

    人生苦短,我用python.在大学四年的本科学习中,Python是我接触过语法最简单,功能最为强大的语言,拥有众多第三方库的支持的语言.如果要选一门编程语言作为入门,建议使用Python.但是为了更加 ...

  9. Weblogic+apache多虚拟主机

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  10. 大数据测试之hadoop集群配置和测试

    大数据测试之hadoop集群配置和测试   一.准备(所有节点都需要做):系统:Ubuntu12.04java版本:JDK1.7SSH(ubuntu自带)三台在同一ip段的机器,设置为静态IP机器分配 ...