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)中,可能會(huì)因?yàn)楦鞣N原因出現(xiàn)主從數(shù)據(jù)不一致的情況,因此我們需要定期或不定期地開(kāi)展主從復(fù)制數(shù)據(jù)一致性的校驗(yàn)和修復(fù)工作;
如果發(fā)生了mysql主從數(shù)據(jù)不一致的情況,我們應(yīng)該怎樣修復(fù)呢?本次分享一種不影響主庫(kù)業(yè)務(wù)的修復(fù)思路。
Master節(jié)點(diǎn)
創(chuàng)建測(cè)試表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()); |
Slave節(jié)點(diǎn)
檢查復(fù)制是否正常,確保復(fù)制正常
Salve節(jié)點(diǎn)執(zhí)行以下操作
set sql_log_bin=0; delete from t1 where id=3; |
Master節(jié)點(diǎn)執(zhí)行
delete from t1 where id=3; |
Slave節(jié)點(diǎn)檢查復(fù)制狀態(tài)
此時(shí)slave復(fù)制已發(fā)生異常,無(wú)法應(yīng)用主庫(kù)操作
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()); |
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; |
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()); |
Slave節(jié)點(diǎn)導(dǎo)入數(shù)據(jù)
檢查gtid信息
導(dǎo)入數(shù)據(jù)
導(dǎo)入數(shù)據(jù)時(shí)發(fā)生報(bào)錯(cuò),無(wú)法更新gtid信息,該錯(cuò)誤不需理會(huì),千萬(wàn)不能為了解決該錯(cuò)誤執(zhí)行resetmaster;
再次檢查gtid信息,未發(fā)生變化
設(shè)置復(fù)制過(guò)濾并應(yīng)用日志
此處設(shè)置的SQL_AFTER_GTIDS為備份文件中g(shù)tid,復(fù)制應(yīng)用日志到該gtid后停止應(yīng)用日志,此時(shí)異常的表數(shù)據(jù)已修復(fù),清除復(fù)制過(guò)濾規(guī)則后啟動(dòng)sql應(yīng)用即可
CHANGE REPLICATION FILTER REPLICATE_WILD_IGNORE_TABLE = (); start slave sql_thread; |
master節(jié)點(diǎn)
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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/130013.html
摘要:簡(jiǎn)介項(xiàng)目地址主從環(huán)境下數(shù)據(jù)一致性校驗(yàn)經(jīng)常會(huì)用工具,它的原理及實(shí)施過(guò)程之前寫(xiě)過(guò)一篇文章生產(chǎn)環(huán)境使用檢查數(shù)據(jù)一致性。上面的配置文件可以認(rèn)為是用于控制程序的,這個(gè)配置文件是指定要校驗(yàn)的源庫(kù)和目標(biāo)庫(kù)信息,以及要檢驗(yàn)?zāi)男┍怼? 1. 簡(jiǎn)介 項(xiàng)目地址:https://github.com/seanlook/p... 主從環(huán)境下數(shù)據(jù)一致性校驗(yàn)經(jīng)常會(huì)用 pt-table-checksum 工具,它的原理...
摘要:通過(guò)對(duì)一些客戶的跨云遷移過(guò)程進(jìn)行總結(jié),發(fā)現(xiàn)普遍存在的挑戰(zhàn)有三點(diǎn)數(shù)據(jù)完整性和一致性挑戰(zhàn)。簡(jiǎn)而言之,跨云遷移過(guò)程中的數(shù)據(jù)一致性主要就集中在存量數(shù)據(jù)的遷移如何保證一致。前言隨著互聯(lián)網(wǎng)業(yè)務(wù)發(fā)展對(duì)容災(zāi)以及對(duì)訪問(wèn)加速、多供應(yīng)商成本控制等需求的產(chǎn)生,互聯(lián)網(wǎng)公司的多云部署和跨云遷移逐漸成為剛需,而在此過(guò)程中,最困擾運(yùn)維和研發(fā)人員的就是數(shù)據(jù)的遷移和同步。俗語(yǔ)說(shuō) 上屋搬下屋,搬灑一籮谷 ,在業(yè)務(wù)的遷移過(guò)程中一旦...
閱讀 1356·2023-01-11 13:20
閱讀 1707·2023-01-11 13:20
閱讀 1215·2023-01-11 13:20
閱讀 1906·2023-01-11 13:20
閱讀 4165·2023-01-11 13:20
閱讀 2757·2023-01-11 13:20
閱讀 1402·2023-01-11 13:20
閱讀 3671·2023-01-11 13:20