slave_exec_mode=IDEMPOTENT 在MySQL復(fù)制環(huán)境中是個(gè)很有用的參數(shù):只要在備機(jī)運(yùn)行set global slave_exec_mode=IDEMPOTENT ,備機(jī)的sql thread就運(yùn)行在冥等模式下,可以讓備機(jī)在insert主鍵、唯一鍵沖突,update、delete值未找到錯(cuò)誤發(fā)生時(shí)不斷開復(fù)制而保持冥等性(當(dāng)即生效,連slave的sql線程都不用重啟喲);而類似sql_slave_skip_counter=N和slave-skip-errors = N 這樣的粗暴跳過錯(cuò)誤方法可能破壞主備一致性。但官方文檔的描述很簡(jiǎn)潔,我一直好奇slave_exec_mode=IDEMPOTENT 是如何在復(fù)制出錯(cuò)時(shí)保持一致性的--譬如主鍵沖突時(shí)是簡(jiǎn)單跳過還是覆寫,今天在Percona 5.7下做了個(gè)實(shí)驗(yàn)(binlog是row格式),實(shí)驗(yàn)過程就省略了,直接總結(jié)如下:
1.insert場(chǎng)景
此時(shí)insert into語句在備機(jī)的效果就跟replace into一樣,但卻并不是把insert into轉(zhuǎn)換成replace into來執(zhí)行,分兩種情況:
a.MySQL配置成autocommit,直接一條insert into ...
如這樣的insert
insert into test set c1='a',c2='b';
此時(shí)insert into語句在備機(jī)執(zhí)行時(shí)假如遇到主鍵沖突就先轉(zhuǎn)化為delete再insert
delete from test where c1='old_value' and c2='old_value';
insert into test set c1='a',c2='b';
假如遇到非主鍵的唯一鍵沖突就轉(zhuǎn)換為update
update test set set c1='a',c2='b' where c1='old_value' and c2='old_value';
b.當(dāng)顯示開始事務(wù)時(shí)(begin...insert into...commit;)
如這樣的sql
begin;
......
insert into test set c1='a',c2='b';
......
commit;
此時(shí)begin...commit里的insert into語句在備機(jī)執(zhí)行時(shí)假如遇到主鍵沖突、唯一鍵沖突都是先轉(zhuǎn)化為delete再insert
begin;
......
delete from test where c1='old_value' and c2='old_value';
insert into test set c1='a',c2='b';
......
commit;
2.update場(chǎng)景
當(dāng)備機(jī)不存在要更新的記錄,這條update跳過不執(zhí)行
3.delete場(chǎng)景
同update場(chǎng)景一樣,備機(jī)跳過此delete啥也不干
注意:使用冥等模式時(shí)表要有主鍵
冥等模式并不是萬能的,除了不能對(duì)DDL操作冥等,對(duì)字段長(zhǎng)度不同導(dǎo)致的錯(cuò)誤也不是冥等(譬如主機(jī)一個(gè)字段是char(20)而備機(jī)是char(10)),還有一個(gè)限制就是表有主鍵才會(huì)對(duì)insert的冥等設(shè)置有效:因?yàn)閕nsert的冥等行為是通過主鍵來判斷備機(jī)是否有重復(fù)值從而產(chǎn)生覆寫操作,如果表沒有主鍵,則備機(jī)即使設(shè)了冥等也可能會(huì)比主機(jī)多重復(fù)數(shù)據(jù)。