溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何理解binlog的binlog_rows_query_log_events和binlog_row_image參數(shù)

發(fā)布時(shí)間:2021-11-16 15:12:21 來源:億速云 閱讀:350 作者:柒染 欄目:MySQL數(shù)據(jù)庫

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)如何理解binlog的binlog_rows_query_log_events和binlog_row_image參數(shù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。


mysql 5.6.2 引入的兩個(gè)參數(shù)binlog_rows_query_log_events和binlog_row_image

對(duì)于使用row格式的binlog,個(gè)人覺得很有用;

binlog_rows_query_log_events =1
    在row模式下..開啟該參數(shù),將把sql語句打印到binlog日志里面.默認(rèn)是0(off);
   雖然將語句放入了binlog,但不會(huì)執(zhí)行這個(gè)sql,就相當(dāng)于注釋一樣.但對(duì)于dba來說,在查看binlog的時(shí)候,很有用處.
binlog_row_image='minimal'
   默認(rèn)為full,在binlog為row格式下,full將記錄update前后所有字段的值,minimal時(shí),只記錄更改字段的值和where字段的值,noblob時(shí),記錄除了blob和text的所有字段的值,如果update的blob或text字段,也只記錄該字段更改后的值,更改前的不記錄;

大家都知道row格式下的binlog增長速度太快,對(duì)存儲(chǔ)空間,主從傳輸都是一個(gè)不小的壓力.因?yàn)槊總€(gè)event記錄了所有更改數(shù)據(jù)前后的值,不管數(shù)據(jù)是否有改動(dòng)都會(huì)記錄.binlog_row_image的引入就大大減少了binlog記錄的數(shù)據(jù).在結(jié)合binlog_rows_query_log_events,對(duì)于dba日常維護(hù)binlog是完全沒有壓力的,而且節(jié)省了硬盤空間開銷,減小I/O,減少了主從傳輸壓力;


下面通過舉例說明:

下面就是一個(gè)事務(wù)在row格式下的binlog日志,紅色部分就是binlog_rows_query_log_events后的效果;黃色部分就是binlog_row_image=full的結(jié)果,記錄所有字段在更改前后的值;

# at 1134
#161118 17:25:49 server id 3310  end_log_pos 1199 CRC32 0x3d0190ce      GTID    last_committed=1        sequence_number=2
SET @@SESSION.GTID_NEXT= '59194c6e-70db-11e6-b85b-5254002eb131:299'/*!*/;
# at 1199
#161118 17:25:49 server id 3310  end_log_pos 1270 CRC32 0x0bc5f8df      Query   thread_id=26    exec_time=0     error_code=0
SET TIMESTAMP=1479461149/*!*/;
BEGIN
/*!*/;
# at 1270
#161118 17:25:49 server id 3310  end_log_pos 1332 CRC32 0x7533bf59      Rows_query
# update t11 set col7 ='xxx' where id =2     ##############這一行就是啟用了binlog_rows_query_log_events后的效果;
# at 1332
#161118 17:25:49 server id 3310  end_log_pos 1396 CRC32 0x75f09d77      Table_map: `tt1`.`t11` mapped to number 137
# at 1396
#161118 17:25:49 server id 3310  end_log_pos 1929 CRC32 0xa6eb390e      Update_rows: table id 137 flags: STMT_END_F
### UPDATE `tt1`.`t11`
### WHERE
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xxx' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @3='bbbbbbbbbbbbbbbbbbbb' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @4='cccccccccccccccccccc' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @5='dddddddddddddddddddd' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @6='eeeeeeeeeeeeeeeeeeee' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @7='ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' /* BLOB/TEXT meta=2 nullable=1 is_null=0 */
###   @8='gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg' /* BLOB/TEXT meta=2 nullable=1 is_null=0 */
### SET
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xxx' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @3='bbbbbbbbbbbbbbbbbbbb' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @4='cccccccccccccccccccc' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @5='dddddddddddddddddddd' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @6='eeeeeeeeeeeeeeeeeeee' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @7='ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' /* BLOB/TEXT meta=2 nullable=1 is_null=0 */
###   @8='xxx' /* BLOB/TEXT meta=2 nullable=1 is_null=0 */
# at 1929
#161118 17:25:49 server id 3310  end_log_pos 1960 CRC32 0x9d147914      Xid = 551
COMMIT/*!*/;


看下面的例子:黃色部分就是binlog_row_image='minimal' 的日志.只記錄了更改后的值和where值.跟上面的例子一對(duì)比就發(fā)現(xiàn)減少了太多記錄;

# at 1960
#161118 17:26:25 server id 3310  end_log_pos 2025 CRC32 0x20921004      GTID    last_committed=2        sequence_number=3
SET @@SESSION.GTID_NEXT= '59194c6e-70db-11e6-b85b-5254002eb131:300'/*!*/;
# at 2025
#161118 17:26:25 server id 3310  end_log_pos 2096 CRC32 0x0c129ffb      Query   thread_id=26    exec_time=0     error_code=0
SET TIMESTAMP=1479461185/*!*/;
BEGIN
/*!*/;
# at 2096
#161118 17:26:25 server id 3310  end_log_pos 2158 CRC32 0x6a5031d0      Rows_query
# update t11 set col1 ='xxx' where id =3
# at 2158
#161118 17:26:25 server id 3310  end_log_pos 2222 CRC32 0x34de716f      Table_map: `tt1`.`t11` mapped to number 137
# at 2222
#161118 17:26:25 server id 3310  end_log_pos 2268 CRC32 0x77563057      Update_rows: table id 137 flags: STMT_END_F
### UPDATE `tt1`.`t11`
### WHERE
###   @1=3 /* INT meta=0 nullable=0 is_null=0 */
### SET
###   @2='xxx' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
# at 2268
#161118 17:26:25 server id 3310  end_log_pos 2299 CRC32 0xe9b5eaa7      Xid = 555
COMMIT/*!*/;


下面看看binlog_row_image=noblob時(shí)的情況,在noblob下,跟full唯一的區(qū)別是沒有記錄字段類型為text和blob的值,其他的都記錄了.而且在例子中特意更改了@8(col7),這是一個(gè)text類型的字段,可以看到,在set前是沒有記錄整個(gè)字段的,只是記錄該值set后的值;

# at 3691
#161118 17:27:01 server id 3310  end_log_pos 3756 CRC32 0x59fb0d65      GTID    last_committed=6        sequence_number=7
SET @@SESSION.GTID_NEXT= '59194c6e-70db-11e6-b85b-5254002eb131:304'/*!*/;
# at 3756
#161118 17:27:01 server id 3310  end_log_pos 3827 CRC32 0x9943381e      Query   thread_id=26    exec_time=0     error_code=0
SET TIMESTAMP=1479461221/*!*/;
BEGIN
/*!*/;
# at 3827
#161118 17:27:01 server id 3310  end_log_pos 3889 CRC32 0xa617db5e      Rows_query
# update t11 set col7 ='xxx' where id =4
# at 3889
#161118 17:27:01 server id 3310  end_log_pos 3953 CRC32 0x74e30167      Table_map: `tt1`.`t11` mapped to number 137
# at 3953
#161118 17:27:01 server id 3310  end_log_pos 4180 CRC32 0xd92fe8dc      Update_rows: table id 137 flags: STMT_END_F
### UPDATE `tt1`.`t11`
### WHERE
###   @1=4 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xxx' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @3='bbbbbbbbbbbbbbbbbbbb' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @4='cccccccccccccccccccc' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @5='dddddddddddddddddddd' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @6='eeeeeeeeeeeeeeeeeeee' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
### SET
###   @1=4 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xxx' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @3='bbbbbbbbbbbbbbbbbbbb' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @4='cccccccccccccccccccc' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @5='dddddddddddddddddddd' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @6='eeeeeeeeeeeeeeeeeeee' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @8='xxx' /* BLOB/TEXT meta=2 nullable=1 is_null=0 */
# at 4180
#161118 17:27:01 server id 3310  end_log_pos 4211 CRC32 0x87487ec8      Xid = 561
COMMIT/*!*/;
# at 4211


對(duì)于delete效果也是一樣的,就不一一舉例了..

BEGIN
/*!*/;
# at 4347
#161118 18:46:03 server id 3310  end_log_pos 4398 CRC32 0xd76dbd23      Rows_query
# delete from t11 where id =2
# at 4398
#161118 18:46:03 server id 3310  end_log_pos 4462 CRC32 0x75656a14      Table_map: `tt1`.`t11` mapped to number 137
# at 4462
#161118 18:46:03 server id 3310  end_log_pos 4590 CRC32 0xafec2072      Delete_rows: table id 137 flags: STMT_END_F
### DELETE FROM `tt1`.`t11`
### WHERE
###   @1=2 /* INT meta=0 nullable=0 is_null=0 */
###   @2='xxx' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @3='bbbbbbbbbbbbbbbbbbbb' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @4='cccccccccccccccccccc' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @5='dddddddddddddddddddd' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
###   @6='eeeeeeeeeeeeeeeeeeee' /* VARSTRING(60) meta=60 nullable=1 is_null=0 */
# at 4590
#161118 18:46:03 server id 3310  end_log_pos 4621 CRC32 0x179ae194      Xid = 562
COMMIT/*!*/;

上述就是小編為大家分享的如何理解binlog的binlog_rows_query_log_events和binlog_row_image參數(shù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI