MySQL GTID主備不一致的修復(fù)方案
方案一:重建 Replicas
MySQL 5.6及以上版在復(fù)制中引入了新的全局事務(wù)ID(GTID)支持。 在啟用了GTID模式的情況下執(zhí)行MySQL和MySQL 5.7的備份時(shí),Percona XtraBackup會(huì)自動(dòng)將GTID值存儲(chǔ)在xtrabackup_binlog_info中。 該信息可用于創(chuàng)建新的(或修復(fù)損壞的)基于GTID的副本。
前提條件
MySQL 機(jī)器上需要安裝 percona xtrabackup
優(yōu)點(diǎn)
比較安全,操作簡單
缺點(diǎn)
- 數(shù)據(jù)量較大的時(shí)候備份所需的時(shí)間比較久
- 當(dāng)數(shù)據(jù)庫有做讀寫分離的時(shí)候,Slave 承擔(dān)的讀請求需要轉(zhuǎn)移到 Master
操作步驟
Master
在 Master 上使用 xtrabackup 工具對當(dāng)前的數(shù)據(jù)庫進(jìn)行備份,執(zhí)行該命令的用戶需要有讀取 MySQL data 目錄的權(quán)限
innobackupex --default-file=/etc/my.cnf --user=root -H 127.0.0.1 --password=[PASSWORD] /tmp
將該備份文件拷貝到 Slave 機(jī)器上
Slave
在 Slave 機(jī)器上執(zhí)行該命令,準(zhǔn)備備份文件
innobackupex --default-file=/etc/my.cnf --user=root -H 127.0.0.1 --password=[PASSWORD] --apply-log /tmp/[TIMESTAMP]
備份并刪除 Slave data目錄
systemctl stop mysqld mv /data/mysql{,.bak}
將備份拷貝到目標(biāo)目錄,并賦予相應(yīng)的權(quán)限,然后重啟 Slave
innobackupex --default-file=/etc/my.cnf --user=root -H 127.0.0.1 --password=[PASSWORD] --copy-back /tmp/[TIMESTAMP] chmod 750 /data/mysql chown mysql.mysql -R /data/mysql systemctl start mysqld
查看當(dāng)前備份已經(jīng)執(zhí)行過的最后一個(gè)的GTID,如下示例
$ cat /tmp/[TIMESTAMP]/xtrabackup_binlog_info mysql-bin.000002 1232 c777888a-b6df-11e2-a604-080027635ef5:1-4
這個(gè)GTID也會(huì)在 innobackupex 備份完成后打印出來
innobackupex: MySQL binlog position: filename 'mysql-bin.000002', position 1232, GTID of the last change 'c777888a-b6df-11e2-a604-080027635ef5:1-4'
使用 root 登錄 MySQL,進(jìn)行如下配置
NewSlave > RESET MASTER; NewSlave > SET GLOBAL gtid_purged='c777888a-b6df-11e2-a604-080027635ef5:1-4'; NewSlave > CHANGE MASTER TO MASTER_HOST="$masterip", MASTER_USER="repl", MASTER_PASSWORD="$slavepass", MASTER_AUTO_POSITION = 1; NewSlave > START SLAVE;
查看 Slave 的復(fù)制狀態(tài)是否正常
NewSlave > SHOW SLAVE STATUS\G [..] Slave_IO_Running: Yes Slave_SQL_Running: Yes [...] Retrieved_Gtid_Set: c777888a-b6df-11e2-a604-080027635ef5:5 Executed_Gtid_Set: c777888a-b6df-11e2-a604-080027635ef5:1-5
我們可以看到副本已檢索到編號為5的新事務(wù),因此從1到5的事務(wù)已在此副本上了。這樣我們就完成了一個(gè)新 replicas 的搭建。
方案二:使用percona-toolkit進(jìn)行數(shù)據(jù)修復(fù)
PT工具包中包含pt-table-checksum和pt-table-sync兩個(gè)工具,主要用于檢測主從是否一致以及修復(fù)數(shù)據(jù)不一致情況。
前提條件
MySQL 機(jī)器上需要安裝 percona-toolkit 工具
優(yōu)點(diǎn)
修復(fù)速度快,不需要停止從庫
缺點(diǎn)
操作復(fù)雜,操作前最后先備份數(shù)據(jù)庫
待修復(fù)的表需要具有 unique constraint
操作步驟
背景示例
IP 關(guān)系對應(yīng)
| IP | Role | | ---- | ---- | | 192.168.100.132 | Master | | 192.168.100.131 | Slave |
假設(shè)待恢復(fù)的表結(jié)構(gòu)如下所示
mysql> show create table test.t; +-------+------------------------------------- | Table | Create Table | +-------+------------------------------------- | t | CREATE TABLE `t` ( `id` int(11) NOT NULL, `content` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+-------------------------------------
正常主備一致的情況下,Master 和 Slave 的數(shù)據(jù)均為如下所示
mysql> select * from test.t; +----+---------+ | id | content | +----+---------+ | 1 | a | | 2 | b | +----+---------+ 2 rows in set (0.00 sec)
在極端情況下,假如出現(xiàn)了如下主備不一致的情況,情形如下:
- Master 新增了一條 id 為 3 的記錄,如下所示,但并沒有同步到 Slave,同時(shí)自動(dòng) failover 到了 Slave。
- Old Slave 作為 New Master 在服務(wù)了一段時(shí)間后,表中增加了新的記錄。
重新啟動(dòng) Old Master 后,Old Master的數(shù)據(jù)如下所示:
old_master> select * from test.t; +----+---------+ | id | content | +----+---------+ | 1 | a | | 2 | b | | 3 | c | +----+---------+ 3 rows in set (0.00 sec)
New Master 的數(shù)據(jù)如下所示:
new_master> select * from test.t; +----+---------+ | id | content | +----+---------+ | 1 | a | | 2 | b | | 3 | cc | | 4 | dd | +----+---------+ 4 rows in set (0.00 sec)
此時(shí)如果將 old master 配置為 new master 的slave,則會(huì)報(bào)錯(cuò),比如出現(xiàn)如下報(bào)錯(cuò)
...Last_IO_Error: binary log: 'Slave has more GTIDs than the master has, using the master's SERVER_UUID.
可以看到 Old Master 的 GTID 已到 255
Executed_Gtid_Set: 5b750c75-86c2-11eb-af71-000c2973a2d5:1-10, 60d082ee-86c2-11eb-a9df-000c2988edab:1-255
而 New Master 的 GTID才到254
mysql> show master status\G *************************** 1. row *************************** File: mysql-bin.000001 Position: 4062 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 5b750c75-86c2-11eb-af71-000c2973a2d5:1-2, 60d082ee-86c2-11eb-a9df-000c2988edab:1-254 1 row in set (0.00 sec)
此時(shí)我們配置 Old Master 跳過錯(cuò)誤,將 Old Master 恢復(fù)成可以正常從 New Master 復(fù)制的狀態(tài)
old_master> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) old_master> set gtid_next='60d082ee-86c2-11eb-a9df-000c2988edab:254'; --Specify the version of the next transaction,the GTID you want to skip Query OK, 0 rows affected (0.00 sec) old_master> begin; Query OK, 0 rows affected (0.00 sec) old_master> commit; -- Inject an empty transaction Query OK, 0 rows affected (0.00 sec) old_master> set gtid_next='AUTOMATIC'; -- Restore to automaic GTID Query OK, 0 rows affected (0.00 sec) old_master> start slave; Query OK, 0 rows affected (0.13 sec)
然后我們在 Old Master 上可以看到復(fù)制在正常進(jìn)行
mysql> show slave status\G ... Slave_IO_Running: Yes Slave_SQL_Running: Yes ... Executed_Gtid_Set: 5b750c75-86c2-11eb-af71-000c2973a2d5:1-10, 60d082ee-86c2-11eb-a9df-000c2988edab:1-255 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
最后我們在 New Master 上清除 slave_master_info
new_master> reset slave all for channel ''; Query OK, 0 rows affected (0.00 sec) new_master> show slave status\G; Empty set (0.01 sec)
校驗(yàn)一致性
接下來我們要校驗(yàn)主從一致性,在 New Master上執(zhí)行 pt-table-checksum,ROWS為4,存在一條DIFFS
[root@localhost ~]# pt-table-checksum h='127.0.0.1',u='mha',p='[PASSWORD]',P=3306 --no-check-binlog-format --databases test Checking if all tables can be checksummed ... Starting checksum ... TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE 03-29T19:24:18 0 1 4 1 1 0 0.322 test.t
雙向同步(同步操作會(huì)修改數(shù)據(jù),操作前進(jìn)行數(shù)據(jù)備份)
在同步過程中,pt-table-sync 會(huì)在 Master 上進(jìn)行數(shù)據(jù)修改,pt-table-sync的參數(shù)作用如下
pt-table-sync --databases test --bidirectional --conflict-column='*' --conflict-comparison 'newest' h='192.168.100.132',u='mha',p='[PASSWORD]',P=3306 h='192.168.100.131' --print --database 指定待執(zhí)行的數(shù)據(jù)庫 --bidirectional 為雙向同步 --conflict-column 對比該列當(dāng)沖突發(fā)生時(shí) --conflict-comparison 沖突對比策略 --print 輸出對比結(jié)果 --dry-run 測試運(yùn)行 --execute 執(zhí)行測試 # 左邊的DSN為 Slave # 右邊的DSN為 Master
這里我們指定—conflict-name='content'作為對比列,一般使用業(yè)務(wù)主鍵作為該列。可以看到打印出了待執(zhí)行的語句
[root@localhost ~]# pt-table-sync --databases test --bidirectional --conflict-column='content' --conflict-comparison 'newest' h='192.168.100.132',u='mha',p='[PASSWORD]',P=3306 h='192.168.100.131' --print /*192.168.100.132:3306*/ UPDATE `test`.`t` SET `content`='cc' WHERE `id`='3' LIMIT 1; /*192.168.100.132:3306*/ INSERT INTO `test`.`t`(`id`, `content`) VALUES ('4', 'dd');
接下來執(zhí)行語句
[root@localhost ~]# pt-table-sync --databases test --bidirectional --conflict-column='content' --conflict-comparison 'newest' h='192.168.100.132',u='mha',p='[PASSWORD]',P=3306 h='192.168.100.131' --execute
然后在 Master 上再次執(zhí)行數(shù)據(jù)對比,可以看到數(shù)據(jù)正常了
[root@localhost ~]# pt-table-checksum h='127.0.0.1',u='mha',p='[PASSWORD]',P=3306 --no-check-binlog-format --databases test Checking if all tables can be checksummed ... Starting checksum ... TS ERRORS DIFFS ROWS DIFF_ROWS CHUNKS SKIPPED TIME TABLE 03-30T12:09:57 0 0 4 0 1 0 0.330 test.t
以上就是MySQL GTID主備不一致的修復(fù)方案的詳細(xì)內(nèi)容,更多關(guān)于MySQL GTID主備不一致修復(fù)的資料請關(guān)注腳本之家其它相關(guān)文章!
- MySQL主從復(fù)制之GTID模式詳細(xì)介紹?
- MySQL在線開啟或禁用GTID模式
- MySQL GTID全面總結(jié)
- MYSQL數(shù)據(jù)庫GTID實(shí)現(xiàn)主從復(fù)制實(shí)現(xiàn)(超級方便)
- MySQL5.6 GTID模式下同步復(fù)制報(bào)錯(cuò)不能跳過的解決方法
- Mysql GTID Mha配置方法
- MySQL5.7不停業(yè)務(wù)將傳統(tǒng)復(fù)制變更為GTID復(fù)制的實(shí)例
- 詳解MySQL主從復(fù)制實(shí)戰(zhàn) - 基于GTID的復(fù)制
- MySQL基于GTID主從搭建
相關(guān)文章
mysql數(shù)據(jù)備份與恢復(fù)實(shí)現(xiàn)方法分析
這篇文章主要介紹了mysql數(shù)據(jù)備份與恢復(fù)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了mysql數(shù)據(jù)備份與恢復(fù)常見實(shí)現(xiàn)方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-04-04MySQL創(chuàng)建用戶以及用戶權(quán)限詳細(xì)圖文教程
在MySQL中可以通過創(chuàng)建用戶來管理數(shù)據(jù)庫的訪問權(quán)限,下面這篇文章主要給大家介紹了關(guān)于MySQL創(chuàng)建用戶以及用戶權(quán)限的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06更新text字段時(shí)出現(xiàn)Row size too large報(bào)錯(cuò)應(yīng)付措施
個(gè)人建議:表的text字段很多建議建表時(shí)加上 row_format = dynamic當(dāng)然,回過頭來MySQL的報(bào)錯(cuò)也是有誤導(dǎo)性的,感興趣的你可以參考下本文2013-03-03master and slave have equal MySQL server ids
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids2013-07-07asp+mysql+utf8 網(wǎng)頁出現(xiàn)亂碼問題的解決方法
無論在網(wǎng)頁中加多少UTF8的定義,顯示的結(jié)果都還是會(huì)有一些小格子,反而定義為gb2312,顯示完全正常2014-03-03Mysql中的超時(shí)時(shí)間設(shè)置方式
這篇文章主要介紹了Mysql中的超時(shí)時(shí)間設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01MySQL優(yōu)化全攻略-相關(guān)數(shù)據(jù)庫命令
MySQL優(yōu)化全攻略-相關(guān)數(shù)據(jù)庫命令...2006-11-11mysql5.7單實(shí)例自啟動(dòng)服務(wù)配置過程
這篇文章主要介紹了mysql5.7單實(shí)例自啟動(dòng)服務(wù)配置的過程,附含配置源碼,有需要的朋友可以借鑒參考下,希望可以有所幫助,感謝閱讀2021-09-09win10家庭版64位下mysql 8.0.15 安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了win10家庭版64位下mysql 8.0.15 安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03