點(diǎn)擊上方“IT那活兒”公眾號,關(guān)注后了解更多內(nèi)容,不管IT什么活兒,干就完了!!!
背景概述
MySQL主從復(fù)制技術(shù)應(yīng)用非常廣泛,M-S復(fù)制架構(gòu)、keepalived+M-M復(fù)制架構(gòu)、MHA等高可用架構(gòu)都基于MySQL主從復(fù)制技術(shù)。
主從復(fù)制是基于binlog的邏輯復(fù)制,實(shí)際應(yīng)中,可能會因?yàn)楦鞣N原因出現(xiàn)主從數(shù)據(jù)不一致的情況,因此我們需要定期或不定期地開展主從復(fù)制數(shù)據(jù)一致性的校驗(yàn)和修復(fù)工作。
如果發(fā)生了mysql主從數(shù)據(jù)不一致的情況,我們應(yīng)該怎樣修復(fù)呢?
環(huán)境準(zhǔn)備
創(chuàng)建測試表t1和t2:
CREATE TABLE `t1` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`phone` int(11) NOT NULL,
`create_time` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=innodb DEFAULT CHARSET=utf8mb4;
insert into t1(username,phone,create_time) values("張三","0123456789",now());
insert into t1(username,phone,create_time) values("張三","0123456789",now());
insert into t1(username,phone,create_time) values("張三","0123456789",now());
insert into t1(username,phone,create_time) values("李四","0123456789",now());
insert into t1(username,phone,create_time) values("李四","0123456789",now());
insert into t1(username,phone,create_time) values("李四","0123456789",now());
CREATE TABLE `t2` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`phone` int(11) NOT NULL,
`create_time` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=innodb DEFAULT CHARSET=utf8mb4;
insert into t2(username,phone,create_time) values("張三","0123456789",now());
insert into t2(username,phone,create_time) values("張三","0123456789",now());
insert into t2(username,phone,create_time) values("張三","0123456789",now());
insert into t2(username,phone,create_time) values("李四","0123456789",now());
insert into t2(username,phone,create_time) values("李四","0123456789",now());
insert into t2(username,phone,create_time) values("李四","0123456789",now());
2.2 Slave節(jié)點(diǎn)
錯誤模擬
3.1 Salve節(jié)點(diǎn)執(zhí)行以下操作
set sql_log_bin=0;
delete from t1 where id=3;
3.2 Master節(jié)點(diǎn)執(zhí)行
delete from t1 where id=3;
3.3 Slave節(jié)點(diǎn)檢查復(fù)制狀態(tài)
此時slave復(fù)制已發(fā)生異常,無法應(yīng)用主庫操作。
3.4 Master持續(xù)操作,模擬業(yè)務(wù)
insert into t1(username,phone,create_time) values("張三","0123456789",now());
insert into t1(username,phone,create_time) values("張三","0123456789",now());
insert into t1(username,phone,create_time) values("張三","0123456789",now());
insert into t1(username,phone,create_time) values("李四","0123456789",now());
insert into t2(username,phone,create_time) values("李四","0123456789",now());
insert into t2(username,phone,create_time) values("李四","0123456789",now());
數(shù)據(jù)修復(fù)
4.1 Master節(jié)點(diǎn)導(dǎo)出slave異常的數(shù)據(jù)表
mysqldump -uxxx-pxxxx -P3306 -S 3306.sock --single-
transaction --master-data=2 test t1 > t1.sql
查看gtid信息并記錄。
grep -A10 GLOBAL.GTID_PURGED t1.sql
SET @@GLOBAL.GTID_PURGED=fdc161ff-5aea-11eb-ab62-000c29e9d0c1:1-81;
4.2 Master持續(xù)模擬業(yè)務(wù)
insert into t1(username,phone,create_time) values("張三","0123456789",now());
insert into t1(username,phone,create_time) values("張三","0123456789",now());
insert into t1(username,phone,create_time) values("張三","0123456789",now());
insert into t1(username,phone,create_time) values("李四","0123456789",now());
insert into t2(username,phone,create_time) values("李四","0123456789",now());
insert into t2(username,phone,create_time) values("李四","0123456789",now());
4.3 Slave節(jié)點(diǎn)導(dǎo)入數(shù)據(jù)
檢查gtid信息:
導(dǎo)入數(shù)據(jù):
導(dǎo)入數(shù)據(jù)時發(fā)生報錯,無法更新gtid信息,該錯誤不需理會,千萬不能為了解決該錯誤執(zhí)行resetmaster。
再次檢查gtid信息,未發(fā)生變化。
設(shè)置復(fù)制過濾并應(yīng)用日志:
此處設(shè)置的SQL_AFTER_GTIDS為備份文件中g(shù)tid,復(fù)制應(yīng)用日志到該gtid后停止應(yīng)用日志,此時異常的表數(shù)據(jù)已修復(fù),清除復(fù)制過濾規(guī)則后啟動sql應(yīng)用即可。
CHANGE REPLICATION FILTER REPLICATE_WILD_IGNORE_TABLE = ();
start slave sql_thread;
數(shù)據(jù)驗(yàn)證
5.1 master節(jié)點(diǎn)
5.2 slave節(jié)點(diǎn)
演示中數(shù)據(jù)較少使用count(*)和select* 即可進(jìn)行比較,生產(chǎn)中數(shù)據(jù)量大,環(huán)境復(fù)雜建議使用pt-table-checksum進(jìn)行一致性校驗(yàn)。
演示環(huán)境為啟用GTID的主從復(fù)制,同樣適用于非GTID復(fù)制環(huán)境,根據(jù)情況選用合適的startslave選項(xiàng),參考官方文檔STARTSLAVE Statement部分:
START SLAVE [thread_types] [until_option] [connection_options] [channel_option]
thread_types:
[thread_type [, thread_type] ... ]
thread_type:
IO_THREAD | SQL_THREAD
until_option:
UNTIL { {SQL_BEFORE_GTIDS | SQL_AFTER_GTIDS} = gtid_set
| MASTER_LOG_FILE = log_name, MASTER_LOG_POS = log_pos
| RELAY_LOG_FILE = log_name, RELAY_LOG_POS = log_pos
| SQL_AFTER_MTS_GAPS }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/129141.html
摘要:簡介項(xiàng)目地址主從環(huán)境下數(shù)據(jù)一致性校驗(yàn)經(jīng)常會用工具,它的原理及實(shí)施過程之前寫過一篇文章生產(chǎn)環(huán)境使用檢查數(shù)據(jù)一致性。上面的配置文件可以認(rèn)為是用于控制程序的,這個配置文件是指定要校驗(yàn)的源庫和目標(biāo)庫信息,以及要檢驗(yàn)?zāi)男┍怼? 1. 簡介 項(xiàng)目地址:https://github.com/seanlook/p... 主從環(huán)境下數(shù)據(jù)一致性校驗(yàn)經(jīng)常會用 pt-table-checksum 工具,它的原理...
摘要:通過對一些客戶的跨云遷移過程進(jìn)行總結(jié),發(fā)現(xiàn)普遍存在的挑戰(zhàn)有三點(diǎn)數(shù)據(jù)完整性和一致性挑戰(zhàn)。簡而言之,跨云遷移過程中的數(shù)據(jù)一致性主要就集中在存量數(shù)據(jù)的遷移如何保證一致。前言隨著互聯(lián)網(wǎng)業(yè)務(wù)發(fā)展對容災(zāi)以及對訪問加速、多供應(yīng)商成本控制等需求的產(chǎn)生,互聯(lián)網(wǎng)公司的多云部署和跨云遷移逐漸成為剛需,而在此過程中,最困擾運(yùn)維和研發(fā)人員的就是數(shù)據(jù)的遷移和同步。俗語說 上屋搬下屋,搬灑一籮谷 ,在業(yè)務(wù)的遷移過程中一旦...
閱讀 1346·2023-01-11 13:20
閱讀 1684·2023-01-11 13:20
閱讀 1132·2023-01-11 13:20
閱讀 1858·2023-01-11 13:20
閱讀 4100·2023-01-11 13:20
閱讀 2704·2023-01-11 13:20
閱讀 1385·2023-01-11 13:20
閱讀 3597·2023-01-11 13:20