MySQL Binlog Mixed模式记录成Row格式
背景:
? ? ? 一個(gè)簡(jiǎn)單的主從結(jié)構(gòu),主的binlog format是Mixed模式,在執(zhí)行一條簡(jiǎn)單的導(dǎo)入語(yǔ)句時(shí),通過(guò)mysqlbinlog導(dǎo)出發(fā)現(xiàn)記錄的Binlog全部變成了Row的格式(明明設(shè)置的是Mixed),現(xiàn)在就說(shuō)說(shuō)在什么情況下Binlog的Mixed模式會(huì)轉(zhuǎn)變記錄成Row格式。
概念:
? ? ? binlog format有三種形式:Statement、Mixed、Row,具體的信息可以自行到網(wǎng)上搜查。
分析(本文碰到的案例):
查看MySQL binlog format
dba@192.168.111.4 : dba_test 02:33:39>show variables like 'binlog_format%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | binlog_format | MIXED | +---------------+-------+測(cè)試語(yǔ)句:
dba@192.168.111.4 : dba_test 02:24:14>create table tmp_test(id int,name varchar(64),age int,primary key(id)) engine = innodb; Query OK, 0 rows affected (0.05 sec)dba@192.168.111.4 : dba_test 02:24:23>insert into tmp_test values(1,'aaa',11); Query OK, 1 row affected (0.02 sec)dba@192.168.111.4 : dba_test 02:25:17>insert into tmp_test values(2,'bbb',22); Query OK, 1 row affected (0.02 sec)dba@192.168.111.4 : dba_test 02:25:23>insert into tmp_test values(3,'ccc',33); Query OK, 1 row affected (0.01 sec)dba@192.168.111.4 : dba_test 02:25:28>insert into tmp_test values(4,'ddd',44); Query OK, 1 row affected (0.01 sec)dba@192.168.111.4 : dba_test 02:25:34>insert into tmp_test values(5,'eee',55); Query OK, 1 row affected (0.01 sec)dba@192.168.111.4 : dba_test 02:25:42>select * from tmp_test; +----+------+------+ | id | name | age | +----+------+------+ | 1 | aaa | 11 | | 2 | bbb | 22 | | 3 | ccc | 33 | | 4 | ddd | 44 | | 5 | eee | 55 | +----+------+------+ 5 rows in set (0.01 sec)dba@192.168.111.4 : dba_test 02:25:50>create table tmp_test_bak(id int,name varchar(64),age int,primary key(id)) engine = innodb; Query OK, 0 rows affected (0.03 sec)dba@192.168.111.4 : dba_test 02:26:31>insert into tmp_test_bak select * from tmp_test; ###記錄成了Row模式 Query OK, 5 rows affected (0.03 sec) Records: 5 Duplicates: 0 Warnings: 0Binlog 記錄圖:
問(wèn)題來(lái)了,我想要出來(lái)的binlog format是Statement,而不是Row。而一條insert into tb select * from ta的簡(jiǎn)單語(yǔ)句在Mixed模式下記錄了Row模式的binlog。原因是什么?
首先確實(shí)在一些特定的情況下,Mixed會(huì)被轉(zhuǎn)換成Row模式:
. 當(dāng) DML 語(yǔ)句更新一個(gè) NDB 表時(shí); . 當(dāng)函數(shù)中包含 UUID() 時(shí); . 2 個(gè)及以上包含 AUTO_INCREMENT 字段的表被更新時(shí); . 執(zhí)行 INSERT DELAYED 語(yǔ)句時(shí); . 用 UDF 時(shí); . 視圖中必須要求運(yùn)用 row 時(shí),例如建立視圖時(shí)使用了 UUID() 函數(shù);上面來(lái)自網(wǎng)絡(luò),有興趣的可以自己測(cè)試測(cè)試。而對(duì)于本文中的sql,符合不了上面的條件,但binlog也記錄成了Row格式。所以還是很奇怪為什么binlog格式被轉(zhuǎn)換了,日常工作的時(shí)候有遇到過(guò)執(zhí)行一條sql,會(huì)報(bào)一個(gè)warning:
Warning: Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT難道因?yàn)檫@個(gè)導(dǎo)致轉(zhuǎn)換的?因?yàn)樯厦娴腟QL可以重現(xiàn),沒(méi)有報(bào)warning,所以這個(gè)情況排除。根據(jù)經(jīng)驗(yàn)想到了一個(gè)參數(shù):innodb_locks_unsafe_for_binlog,看到里面講到事務(wù)隔離級(jí)別,那就看看隔離級(jí)別的情況:
dba@192.168.111.4 : dba_test 05:46:56>select @@global.tx_isolation; +-----------------------+ | @@global.tx_isolation | +-----------------------+ | READ-COMMITTED | +-----------------------+ 1 row in set (0.01 sec)dba@192.168.111.4 : dba_test 06:36:45>select @@session.tx_isolation; +------------------------+ | @@session.tx_isolation | +------------------------+ | READ-COMMITTED | +------------------------+ 1 row in set (0.01 sec)看到隔離級(jí)別是提交讀,即不可重復(fù)讀。把事務(wù)隔離級(jí)別設(shè)置成默認(rèn)的?REPEATABLE READ:
dba@192.168.111.4 : dba_test 06:41:02>set session transaction isolation level REPEATABLE READ; Query OK, 0 rows affected (0.14 sec)dba@192.168.111.4 : dba_test 06:41:42>select @@session.tx_isolation; +------------------------+ | @@session.tx_isolation | +------------------------+ | REPEATABLE-READ | +------------------------+ 1 row in set (0.00 sec)再執(zhí)行測(cè)試?yán)锏腟QL,發(fā)現(xiàn)這時(shí)候Mixed的binlog記錄了Statement格式,正常了,符合預(yù)期了。難道就是這個(gè)事務(wù)隔離級(jí)別的問(wèn)題引起的?在手冊(cè)里發(fā)現(xiàn)了這句:
Note In MySQL 5.7, when READ COMMITTED isolation level is used, or the deprecated innodb_locks_unsafe_for_binlog system variable is enabled, there is no InnoDB gap locking except for foreign-key constraint checking and duplicate-key checking. Also, record locks for nonmatching rows are released after MySQL has evaluated the WHERE condition.If you use READ COMMITTED or enable innodb_locks_unsafe_for_binlog, you must use row-based binary logging.展開(kāi)可以看例子:
dba@192.168.111.4 : dba_test 06:49:27>select @@session.tx_isolation; +------------------------+ | @@session.tx_isolation | +------------------------+ | REPEATABLE-READ | +------------------------+ 1 row in set (0.01 sec)dba@192.168.111.4 : dba_test 06:49:29>create table tmp_test_bak(id int,name varchar(64),age int,primary key(id)) engine = innodb; Query OK, 0 rows affected (0.14 sec)dba@192.168.111.4 : dba_test 06:49:34>insert into tmp_test_bak values(1,'aaa',11); Query OK, 1 row affected (0.13 sec)dba@192.168.111.4 : dba_test 06:49:41>insert into tmp_test_bak values(2,'bbb',22); Query OK, 1 row affected (0.11 sec)dba@192.168.111.4 : dba_test 06:49:41>insert into tmp_test_bak values(3,'ccc',33); Query OK, 1 row affected (0.11 sec)dba@192.168.111.4 : dba_test 06:49:42>insert into tmp_test_bak values(4,'ddd',44); Query OK, 1 row affected (0.11 sec)dba@192.168.111.4 : dba_test 06:49:42>insert into tmp_test_bak values(5,'eee',55); Query OK, 1 row affected (0.13 sec)BINLOG: root@dba:/var/log/mysql# mysqlbinlog mysql-bin.000007 |grep "tmp_test_bak" create table tmp_test_bak(id int,name varchar(64),age int,primary key(id)) engine = innodb insert into tmp_test_bak values(1,'aaa',11) insert into tmp_test_bak values(2,'bbb',22) insert into tmp_test_bak values(3,'ccc',33) insert into tmp_test_bak values(4,'ddd',44) insert into tmp_test_bak values(5,'eee',55)############################# #############################dba@192.168.111.4 : dba_test 06:49:43>set session transaction isolation level READ COMMITTED; Query OK, 0 rows affected (0.01 sec)dba@192.168.111.4 : dba_test 06:50:21>flush logs; Query OK, 0 rows affected (0.21 sec)dba@192.168.111.4 : dba_test 06:50:23>create table tmp_test(id int,name varchar(64),age int,primary key(id)) engine = innodb; Query OK, 0 rows affected (0.14 sec)dba@192.168.111.4 : dba_test 06:50:49>insert into tmp_test values(1,'aaa',11); Query OK, 1 row affected (0.13 sec)dba@192.168.111.4 : dba_test 06:50:54>insert into tmp_test values(2,'bbb',22); Query OK, 1 row affected (0.11 sec)dba@192.168.111.4 : dba_test 06:50:54>insert into tmp_test values(3,'ccc',33); Query OK, 1 row affected (0.11 sec)dba@192.168.111.4 : dba_test 06:50:54>insert into tmp_test values(4,'ddd',44); Query OK, 1 row affected (0.11 sec)dba@192.168.111.4 : dba_test 06:50:54>insert into tmp_test values(5,'eee',55); Query OK, 1 row affected (0.12 sec)BINLOG: root@pubmed2:/var/log/mysql# mysqlbinlog -vv mysql-bin.000008 |grep "tmp_test" create table tmp_test(id int,name varchar(64),age int,primary key(id)) engine = innodb #160426 18:50:54 server id 1 end_log_pos 395 Table_map: `pubmed_data`.`tmp_test` mapped to number 44 ### INSERT INTO `pubmed_data`.`tmp_test` #160426 18:50:54 server id 1 end_log_pos 597 Table_map: `pubmed_data`.`tmp_test` mapped to number 44 ### INSERT INTO `pubmed_data`.`tmp_test` #160426 18:50:54 server id 1 end_log_pos 799 Table_map: `pubmed_data`.`tmp_test` mapped to number 44 ### INSERT INTO `pubmed_data`.`tmp_test` #160426 18:50:54 server id 1 end_log_pos 1001 Table_map: `pubmed_data`.`tmp_test` mapped to number 44 ### INSERT INTO `pubmed_data`.`tmp_test` #160426 18:50:55 server id 1 end_log_pos 1203 Table_map: `pubmed_data`.`tmp_test` mapped to number 44 ### INSERT INTO `pubmed_data`.`tmp_test` View Code經(jīng)過(guò)測(cè)試,在5.1、5.5、5.6都有這個(gè)情況,可能這個(gè)本身就不是問(wèn)題。:)
總結(jié):
? ? ? 除了上面說(shuō)的一些條件影響binlog的記錄格式外(Statement 到 Row),事務(wù)隔離也影響這binlog的記錄格式行為。
轉(zhuǎn)載于:https://www.cnblogs.com/zhoujinyi/p/5436250.html
總結(jié)
以上是生活随笔為你收集整理的MySQL Binlog Mixed模式记录成Row格式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 建行龙卡优享信用卡国内可以消费吗?答案是
- 下一篇: 华为、宁德时代、长安中国队造车!阿维塔1