MySQL抑制binlog日誌中的BINLOG部分,mysql抑制binlog日誌

來源:互聯網
上載者:User

MySQL抑制binlog日誌中的BINLOG部分,mysql抑制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--建立表t1mysql> 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 eventsmysql> 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 |
+------+-------+


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.