MySQL抑制binlog日志中的BINLOG部分

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 VARCHARBLOB 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_EVENTUPDATE 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 [email protected]之类的形式,如果-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 |
+------+-------+

时间: 2024-10-27 09:44:21

MySQL抑制binlog日志中的BINLOG部分的相关文章

查看MySQL还原出来的binlog日志中内容方法

用mysqlbinlog查出需要查看的数据后,可以用more来查看: [[email protected] data]# more recover_sakila.sql | grep --ignore-case -E 'insert' -A2 -B2 | grep yoon 如果表名包含yoon_log,yoon_order,只想导出yoon表的话,+个-w[[email protected] data]# more recover_sakila.sql | grep --ignore-case

MySQL grant用户授权 和 MYSQL bin-log日志 实操讲解

这一次我来简单地实际操作讲解下grant用户授权和bin-log日志,博文中配了很多操作过程中的图作为步骤流程讲解,大家跟着过程自己在电脑操作一遍,基本上就懂这方面的相关知识了.不多说,直接进入知识讲解. 远程主机连接用户授权 例:192.168.1.111mysql服务器 和192.168.1.112mysql服务器 一.MySQL grant用户授权  192.168.1.111登陆mysql ,给用户授权 查询下是否添加此用户 这样就成功给IP为192.168.1.112的主机授权mysq

MYSQL bin-log日志

MYSQL bin-log日志主要用于数据备份和恢复:其文件中保存了数据库的所有增删改操作 1.开启bin-log日志: 2.查看bin-log日志 连接成功数据库后 可以查看log_bin是否开启成功 查看bin-log日志 3.与bin_log有关的日志刷新 此时就会多一个最新的bin_log日志 可以再/usr/local/mysql/var中看到多了个mysql-bin.xxxxxx 查看最后一个bin-log日志 清空所有bin-log日志

MySQL binlog日志优化

mysql中日志类型有慢查询日志,二进制日志,错误日志,默认情况下,系统只打开错误日志,因为开启日志会产生较大的IO性能消耗. 一般情况下,生成系统中很少打开二进制日志(bin log),bin log日志的优化策略: mysql> show variables like '%binlog%'; +-----------------------------------------+----------------------+ | Variable_name                  

MySQL 的 binlog 日志

binlog 基本认识 MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事务安全型的. 一般来说开启二进制日志大概会有1%的性能损耗(参见MySQL官方中文手册 5.1.24版).二进制有两个最重要的使用场景: 其一:MySQL Replication在Master端开启binlog,Mster把它的二进制日志传递给slaves来达到master-slave数据一致

MySQL的binlog日志<转>

binlog 基本认识 MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事务安全型的. 一般来说开启二进制日志大概会有1%的性能损耗(参见MySQL官方中文手册 5.1.24版).二进制有两个最重要的使用场景: 其一:MySQL Replication在Master端开启binlog,Mster把它的二进制日志传递给slaves来达到master-slave数据一致

Mysql之binlog日志说明及利用binlog日志恢复数据操作记录

众所周知,binlog日志对于mysql数据库来说是十分重要的.在数据丢失的紧急情况下,我们往往会想到用binlog日志功能进行数据恢复(定时全备份+binlog日志恢复增量数据部分),化险为夷! 废话不多说,下面是梳理的binlog日志操作解说: 一.初步了解binlogMySQL的二进制日志binlog可以说是MySQL最重要的日志,它记录了所有的DDL和DML语句(除了数据查询语句select),以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事务安全型的. DDL-

Mysql binlog日志及binlog恢复数据库操作

初识MySQL 日志binlogMySQL重要log,二进制日志文件,记录所有DDL和DML语句(除select),事件形式记录,包含语句所执行的消耗时间,事务安全型.DDL(数据库定义语言),主要命令有create.alter.drop等.DDL主要定义或改变表table的结构.数据类型.建表时使用.MDL(数据操纵语言),主要命令有select.update.insert.delete. mysqlbinlog常见选项:--start-datetime:从二进制中读取指定时间戳.--stop

mysql查看binlog日志

MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事务安全型的.一般来说开启二进制日志大概会有1%的性能损耗(参见MySQL官方中文手册 5.1.24版).二进制有两个最重要的使用场景:       其一:MySQL Replication在Master端开启binlog,Mster把它的二进制日志传递给slaves来达到master-slave数据一致的目的.