天天看点

MySQL抑制binlog日志中的BINLOG部分

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

1、mysqlbinlog之base64-output参数

<a target="_blank" href="http://dev.mysql.com/doc/refman/5.6/en/mysqlbinlog.html#option_mysqlbinlog_base64-output">--base64-output=value</a>

·         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:

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

2、演示生成binlog日志

3、演示提取binlog日志

#未使用base64-output选项的情形,即缺省值为AUTO

SHELL&gt;  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

#BINLOG部分依旧被显示出来

SHELL&gt;  mysqlbinlog -v /opt/data/APP01bin.000001|grep truncate -B20

/*!*/;

BINLOG '

### INSERT INTO `test`.`t1`

### SET

###   @1=1

###   @2='robin'

#添加--base64-output=DECODE-ROWS选项来抑制BINLOG的显示,如下我们看不到了BINLOG部分

SHELL&gt;  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

#此时使用mysqlbinlog做一个不完全恢复

SHELL&gt;  mysqlbinlog --database=test --start-position="238" --stop-position="433" /opt/data/APP01bin.000001 |mysql -uroot -p --database=test

Enter password:

#查看恢复后的结果

SHELL&gt;  mysql -uroot -p -e "select * from test.t1"

+------+-------+

| id   | val   |

|    1 | robin |

MySQL抑制binlog日志中的BINLOG部分