MySQL中binlog参数:binlog_rows_query_log_events-记录具体的SQL【转】
在使用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模式进行关闭开启。
在未开启状态中的使用
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[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]# lltotal 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<br>-rw-r----- 1 mysql mysql 194 Apr 24 08:53 mysql_bin.000030<br><br>-rw-r----- 1 mysql mysql 132 Apr 24 08:43 mysql_bin.index <br><br>-rw-r----- 1 mysql mysql 8534643198 Apr 24 08:43 slow.log |
目前最新的binlog切换到30的文件中,做对应的DML
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
[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-01 logs]# mysqlbinlog --no-defaults -v -v --base64-output=DECODE-ROWS mysql_bin.000030
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#170424 8:58:35 server id 37306 end_log_pos 123 CRC32 0x48e80ee3 Start: binlog v 4, server v 5.7.17-log created 170424 8:58:35
# Warning: this binlog is either in use or was not closed properly.
# at 123
#170424 8:58:35 server id 37306 end_log_pos 194 CRC32 0xaf41def6 Previous-GTIDs
# a0c06ec7-fef0-11e6-9f85-525400a7d662:1-812
# at 194
#170424 8:58:52 server id 37306 end_log_pos 259 CRC32 0xac26b3b1 GTID last_committed=sequence_number=1
SET @@SESSION.GTID_NEXT= 'a0c06ec7-fef0-11e6-9f85-525400a7d662:813'/*!*/;
# at 259
#170424 8:58:52 server id 37306 end_log_pos 331 CRC32 0xb56db594 Query thread_id=34 exec_time=0 error_code=0
SET TIMESTAMP=1492995532/*!*/;
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 8:58:52 server id 37306 end_log_pos 385 CRC32 0x88acf1be Rows_query # at 429
#170424 8:58:52 server id 37306 end_log_pos 475 CRC32 0xf2505403 Update_rows: table id 118 flags: STMT_END_F
### UPDATE `boss`.`t`
### WHERE
### @1=7 /* INT meta=0 nullable=1 is_null=0 */
### SET
### @1=8 /* INT meta=0 nullable=1 is_null=0 */
# at 475
#170424 8:58:52 server id 37306 end_log_pos 506 CRC32 0xefebd387 Xid = 2444
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 - 懒睡的猫熊 - 博客园 https://www.cnblogs.com/olinux/p/6755193.html
MySQL中binlog参数:binlog_rows_query_log_events-记录具体的SQL【转】的更多相关文章
- MySQL中binlog参数:binlog_rows_query_log_events
在使用RBR也就是行格式的时候,去解析binlog,需要逆向才能分析出对应的原始SQL是什么,而且,里面对应的是每一条具体行变更的内容.当然,你可以开启general log,但如果我们需要的只是记录 ...
- MYSQL 中binlog 参数的记录
http://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html binlog_cache_size Command ...
- php实现只保留mysql中最新1000条记录
这篇文章主要介绍了php实现只保留mysql中最新1000条记录的方法和相关示例及数据库结构,十分的全面,有需要的小伙伴可以参考下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...
- mysql中插入多条记录-微软批处理
当向mysql中插入大量数据时,可以使用微软的批处理方式.这样可以防止内存溢出又提高了效率.我写了一个mysql中插入多条记录的例子.赋值代码可以直接使用. 1.首先需要添加两个dll MySql.D ...
- maxwell实时同步mysql中binlog
概述 Maxwell是一个能实时读取MySQL二进制日志binlog,并生成 JSON 格式的消息,作为生产者发送给 Kafka,Kinesis.RabbitMQ.Redis.Google Cloud ...
- WINDOWS下使用Mysql 中碰到的问题记录
问题:在cmd中输入net stop mysql反馈“服务名无效” win+R打开运行窗口,输入 services.msc 查看其中mysql的服务名,比如我的是叫做MySQL80 让我们继续回到最开 ...
- mysql中binlog与存储引擎的2PC
mysql内部的2PC mysql开启binlog后实际上可以认为其数据有两份,binlog中一份,引擎中一份(这里先把存储引擎中数据看成整体的单独一份,另外也可以把binlog看成是一个引擎).既然 ...
- 分享一:关于mysql中避免重复插入记录方法
一: INSERT ON DUPLICATE KEY UPDATE 如果您指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重 ...
- mysql中删除完全重复数据的准确SQL语句
删除数据库中重复的记录,只保留一条 DELETE FROM tb_gps_records WHERE id NOT IN (SELECT bid FROM (SELECT min(id) as bid ...
随机推荐
- XenServer中虚拟机和快照导出与导入
我们在工作中经常会遇到,把Xenserver中的虚拟机或者快照导出,然后导入到另一台Xenserver,或者导出来备份下来,以防虚拟机出现故障. 下面介绍一下用xe命令如何导出/导入虚拟机或快照,当然 ...
- PL/SQL Developer连接本地Oracle 11g 64位数据库和快捷键设置
1.登录PL/SQL Developer 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Developer软件时,不要安装在Program Files ...
- Nginx记录-nginx 负载均衡5种配置方式(转载)
nginx 负载均衡5种配置方式 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight和访问比率成 ...
- Tomcat虚拟路径访问本地图片失败的问题
开发过程中,把图片放在本地一个磁盘的路径下.网上搜了下,可以使用tomcat虚拟路径访问本地图片. 这样就不用把图片放在整个项目的webapp目录下了. 1.找到tomcat的server.xml文件 ...
- CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)
https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...
- bzoj千题计划312:bzoj2119: 股市的预测(后缀数组+st表)
https://www.lydsy.com/JudgeOnline/problem.php?id=2119 题意:将给定数组差分后,求ABA形式的字串个数,要求|B|=m,|A|>0 1.后缀数 ...
- 转载--关于hdfs
原文章链接 你肯定听过Hadoop,对就是那头奔跑的小象. 图片描述 Hadoop作为大数据时代代表性的解决方案被大家所熟知,它主要包含两部分内容: HDFS分布式文件存储 MapReduce分布式计 ...
- springBoot中实现自定义属性配置、实现异步调用、多环境配置
springBoot中其他相关: 1:springBoot中自定义参数: 1-1.自定义属性配置: 在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性 ...
- 简单备份mysql数据库
对于数据量不大的业务场景,可以每天做全量备份. 实现方式:编写备份数据库脚本,然后在crontab中每天定时执行脚本进行备份. 备份脚本示例: #!/bin/bash #Author: zhangsa ...
- adduser Ubuntu添加sudo用户
第一种方法: 添加sudo用户 当你安装Ubuntu的时候,它会自动添加第一个用户到sudo组,允许这个用户通过键入其自身帐户密码来获得超级用户(root)身份.然而,系统不会再自动添加其他的用户到s ...