MySQL抑制binlog日志中的BINLOG部分

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:     MySQL通过binlog来记录整个数据的变更过程,因此我们只要有MySQL的binlog日志即可完整的还原数据库。MySQL binlog日志记录有3种不同的方式,即:STATEMENT,MIXED,ROW。

    MySQL通过binlog来记录整个数据的变更过程,因此我们只要有MySQL的binlog日志即可完整的还原数据库。MySQL binlog日志记录有3种不同的方式,即:STATEMENT,MIXED,ROW。对于不同的日志模式,生成的binlog有不同的记录方式。对于MIXED(部分SQL语句)和ROW模式是以base-64方式记录,会以BINLOG开头,是一段伪SQL,我们可以用使用base64-output参数来抑制其显示。本文对此给出了描述及演示。

    有关mysqlbinlog的用法,请参考:使用mysqlbinlog提取二进制日志

 

1、mysqlbinlog之base64-output参数

--base64-output=value

This option determines when events should be displayed encoded as base-64 strings using BINLOG statements. The option has these permissible values (not case sensitive):

  •  AUTO ("automatic") or UNSPEC ("unspecified") displays  BINLOG  statements automatically when necessary (that is, for format description events and row events). If no  --base64-output  option is given, the effect is the same as  --base64-output=AUTO .
  •  NEVER causes  BINLOG  statements not to be displayed.  mysqlbinlog  exits with an error if a row event is found that must be displayed using  BINLOG .
  • DECODE-ROWS specifies to  mysqlbinlog  that you intend for row events to be decoded and displayed as commented SQL statements by also specifying the  --verbose  option. Like NEVERDECODE-ROWS suppresses display of  BINLOG  statements, but unlike NEVER, it does not exit with an error if a row event is found.

以上描述对于binlog日志中的BINLOG部分,如果要过虑掉需要指定DECODE-ROWS 以及--verbose选项。

 

The SQL statements produced by --verbose for row events are much more readable than the corresponding BINLOG statements. However, they do not correspond exactly to the original SQL statements that generated the events. The following limitations apply:

    --verbose选项可以获取更多的可读信息,但是并不是一个原始的SQL语句(类似的)

·         The original column names are lost and replaced by @N, where N is a column number.

·         Character set information is not available in the binary log, which affects string column display:

  • There is no distinction made between corresponding binary and nonbinary string types ( BINARY  and  CHAR , VARBINARY  and  VARCHAR ,  BLOB  and  TEXT ). The output uses a data type of STRING for fixed-length strings andVARSTRING for variable-length strings.
  • For multibyte character sets, the maximum number of bytes per character is not present in the binary log, so the length for string types is displayed in bytes rather than in characters. For example, STRING(4) will be used as the data type for values from either of these column types:

CHAR(4) CHARACTER SET latin1

CHAR(2) CHARACTER SET ucs2

  • Due to the storage format for events of type UPDATE_ROWS_EVENT,  UPDATE  statements are displayed with theWHERE clause preceding the SET clause.

Proper interpretation of row events requires the information from the format description event at the beginning of the binary log. Because mysqlbinlog does not know in advance whether the rest of the log contains row events, by default it displays the format description event using a BINLOG statement in the initial part of the output.

If the binary log is known not to contain any events requiring a BINLOG statement (that is, no row events), the --base64-output=NEVER option can be used to prevent this header from being written.

 

2、演示生成binlog日志

--环境
mysql> show variables like 'version';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.6.12-log |
+---------------+------------+

--如下查询binlog为row记录模式
mysql> show variables like 'binlog_for%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+

mysql> reset master;
Query OK, 0 rows affected (0.01 sec)

mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| APP01bin.000001 |      120 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+

mysql> use test;
Database changed

--创建表t1
mysql> create table t1(id smallint,val varchar(20));
Query OK, 0 rows affected (0.01 sec)

--插入单条记录
mysql> insert into t1 values(1,'robin');
Query OK, 1 row affected (0.00 sec)

--清空表
mysql> truncate table t1;
Query OK, 0 rows affected (0.01 sec)

--查看binlog events
mysql> show binlog events;
+-----------------+-----+-------------+-----------+-------------+----------------------------------------------------------+
| Log_name        | Pos | Event_type  | Server_id | End_log_pos | Info                                                     |
+-----------------+-----+-------------+-----------+-------------+----------------------------------------------------------+
| APP01bin.000001 |   4 | Format_desc |        11 |         120 | Server ver: 5.6.12-log, Binlog ver: 4                    |
| APP01bin.000001 | 120 | Query       |        11 |         238 | use `test`; create table t1(id smallint,val varchar(20)) |
| APP01bin.000001 | 238 | Query       |        11 |         310 | BEGIN                                                    |
| APP01bin.000001 | 310 | Table_map   |        11 |         358 | table_id: 74 (test.t1)                                   |
| APP01bin.000001 | 358 | Write_rows  |        11 |         402 | table_id: 74 flags: STMT_END_F                           |
| APP01bin.000001 | 402 | Xid         |        11 |         433 | COMMIT /* xid=30 */                                      |
| APP01bin.000001 | 433 | Query       |        11 |         517 | use `test`; truncate table t1                            |
+-----------------+-----+-------------+-----------+-------------+----------------------------------------------------------+
7 rows in set (0.00 sec)

--获取binlog位置
mysql> show variables like 'log_bin_basename';
+------------------+--------------------+
| Variable_name    | Value              |
+------------------+--------------------+
| log_bin_basename | /opt/data/APP01bin |
+------------------+--------------------+

3、演示提取binlog日志
#未使用base64-output选项的情形,即缺省值为AUTO
SHELL>  mysqlbinlog /opt/data/APP01bin.000001|grep truncate -B15
# at 310
#141218 16:28:05 server id 11  end_log_pos 358 CRC32 0xe0025004         Table_map: `test`.`t1` mapped to number 74
# at 358
#141218 16:28:05 server id 11  end_log_pos 402 CRC32 0x3452dcfe         Write_rows: table id 74 flags: STMT_END_F

BINLOG '                               #这个BINLOG部分是真实的SQL语句,无法看到具体内容
FZCSVBMLAAAAMAAAAGYBAAAAAEoAAAAAAAEABHRlc3QAAnQxAAICDwI8AAMEUALg
FZCSVB4LAAAALAAAAJIBAAAAAEoAAAAAAAEAAgAC//wBAAVyb2Jpbv7cUjQ=
'/*!*/;

# at 402
#141218 16:28:05 server id 11  end_log_pos 433 CRC32 0xbe26740a         Xid = 30
COMMIT/*!*/;
# at 433
#141218 16:29:00 server id 11  end_log_pos 517 CRC32 0x89c52d6a         Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1418891340/*!*/;
truncate table t1

#使用-v参数的情形,可以看到我们操作生成的SQL语句了,为insert into ..@1之类的形式,如果-vv则输出列的描述信息
#BINLOG部分依旧被显示出来

SHELL>  mysqlbinlog -v /opt/data/APP01bin.000001|grep truncate -B20
/*!*/;
# at 310
#141218 16:28:05 server id 11  end_log_pos 358 CRC32 0xe0025004         Table_map: `test`.`t1` mapped to number 74
# at 358
#141218 16:28:05 server id 11  end_log_pos 402 CRC32 0x3452dcfe         Write_rows: table id 74 flags: STMT_END_F

BINLOG '
FZCSVBMLAAAAMAAAAGYBAAAAAEoAAAAAAAEABHRlc3QAAnQxAAICDwI8AAMEUALg
FZCSVB4LAAAALAAAAJIBAAAAAEoAAAAAAAEAAgAC//wBAAVyb2Jpbv7cUjQ=
'/*!*/;

### INSERT INTO `test`.`t1`
### SET
###   @1=1
###   @2='robin'
# at 402
#141218 16:28:05 server id 11  end_log_pos 433 CRC32 0xbe26740a         Xid = 30
COMMIT/*!*/;
# at 433
#141218 16:29:00 server id 11  end_log_pos 517 CRC32 0x89c52d6a         Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1418891340/*!*/;
truncate table t1

#添加--base64-output=DECODE-ROWS选项来抑制BINLOG的显示,如下我们看不到了BINLOG部分
SHELL>  mysqlbinlog --base64-output=DECODE-ROWS -v /opt/data/APP01bin.000001|grep truncate -B20
/*!*/;
# at 238
#141218 16:28:05 server id 11  end_log_pos 310 CRC32 0x60507739         Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1418891285/*!*/;
BEGIN
/*!*/;
# at 310
#141218 16:28:05 server id 11  end_log_pos 358 CRC32 0xe0025004         Table_map: `test`.`t1` mapped to number 74
# at 358
#141218 16:28:05 server id 11  end_log_pos 402 CRC32 0x3452dcfe         Write_rows: table id 74 flags: STMT_END_F
### INSERT INTO `test`.`t1`
### SET
###   @1=1
###   @2='robin'
# at 402
#141218 16:28:05 server id 11  end_log_pos 433 CRC32 0xbe26740a         Xid = 30
COMMIT/*!*/;
# at 433
#141218 16:29:00 server id 11  end_log_pos 517 CRC32 0x89c52d6a         Query   thread_id=1     exec_time=0     error_code=0
SET TIMESTAMP=1418891340/*!*/;
truncate table t1

#此时使用mysqlbinlog做一个不完全恢复
SHELL>  mysqlbinlog --database=test --start-position="238" --stop-position="433" /opt/data/APP01bin.000001 |mysql -uroot -p --database=test
Enter password:

#查看恢复后的结果
SHELL>  mysql -uroot -p -e "select * from test.t1"
Enter password:
+------+-------+
| id   | val   |
+------+-------+
|    1 | robin |
+------+-------+

鹏城DBA总群


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3天前
|
SQL 存储 关系型数据库
MySQL慢日志的介绍以及如何使用问题
【5月更文挑战第19天】MySQL慢日志的介绍以及如何使用问题
12 2
|
4天前
|
存储 关系型数据库 MySQL
实时计算 Flink版产品使用合集之如何配置可以实现实时同步多张MySQL源表时只读取一次binlog
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStreamAPI、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
9天前
|
SQL 资源调度 关系型数据库
实时计算 Flink版产品使用合集之在抓取 MySQL binlog 数据时,datetime 字段会被自动转换为时间戳形式如何解决
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
20 2
|
10天前
|
关系型数据库 MySQL 数据处理
实时计算 Flink版产品使用合集之在同步MySQL的时候卡在某个binlog文件处如何解决
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
实时计算 Flink版产品使用合集之在同步MySQL的时候卡在某个binlog文件处如何解决
|
10天前
|
关系型数据库 MySQL 数据库
mysql数据库bin-log日志管理
mysql数据库bin-log日志管理
|
10天前
|
存储 关系型数据库 数据库
关系型数据库文件方式存储LOG FILE(日志文件)
【5月更文挑战第11天】关系型数据库文件方式存储LOG FILE(日志文件)
69 1
|
11天前
|
运维 监控 安全
Java一分钟之-Log4j与日志记录的重要性
【5月更文挑战第16天】Log4j是Java常用的日志框架,用于灵活地记录程序状态和调试问题。通过设置日志级别和过滤器,可避免日志输出混乱。为防止日志文件过大,可配置滚动策略。关注日志安全性,如Log4j 2.x的CVE-2021-44228漏洞,及时更新至安全版本。合理使用日志能提升故障排查和系统监控效率。
70 0
|
12天前
|
C++
JNI Log 日志输出
JNI Log 日志输出
55 1
|
12天前
|
存储 运维 大数据
聊聊日志硬扫描,阿里 Log Scan 的设计与实践
泛日志(Log/Trace/Metric)是大数据的重要组成,伴随着每一年业务峰值的新脉冲,日志数据量在快速增长。同时,业务数字化运营、软件可观测性等浪潮又在对日志的存储、计算提出更高的要求。
262 6
|
12天前
|
XML Java Maven
Springboot整合与使用log4j2日志框架【详解版】
该文介绍了如何在Spring Boot中切换默认的LogBack日志系统至Log4j2。首先,需要在Maven依赖中排除`spring-boot-starter-logging`并引入`spring-boot-starter-log4j2`。其次,创建`log4j2-spring.xml`配置文件放在`src/main/resources`下,配置包括控制台和文件的日志输出、日志格式和文件切分策略。此外,可通过在不同环境的`application.yml`中指定不同的log4j2配置文件。最后,文章提到通过示例代码解释了日志格式中的各种占位符含义。

推荐镜像

更多