本篇內(nèi)容介紹了“MySQL互為主從的環(huán)境為什么會出現(xiàn)數(shù)據(jù)不一致”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、網(wǎng)站建設(shè)、宛城網(wǎng)絡(luò)推廣、微信小程序開發(fā)、宛城網(wǎng)絡(luò)營銷、宛城企業(yè)策劃、宛城品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供宛城建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:jinyejixie.com
m1:
begin;
update t1 set c2='b1' where c1=2;
commit;
m2:
begin;
update t1 set c2='b2' where c1=2;
commit;
m1和m2同時提交,復(fù)制不會報錯,但是m1和m2的數(shù)據(jù)不一致,為什么?
因為sql_thread線程根據(jù)主鍵更新數(shù)據(jù),不會校驗行數(shù)據(jù)
如何避免這種問題:
只在單節(jié)點進(jìn)行寫入,如 keepalived+雙主,MGR,PXC如果多節(jié)點寫入都有這種問題發(fā)生。
例1:
表有主鍵和自增的情況:
root@localhost [testdb]>show create table t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`c1` int(11) NOT NULL AUTO_INCREMENT,
`c2` varchar(10) DEFAULT NULL,
PRIMARY KEY (`c1`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
m1: | m2: |
root@localhost [testdb]>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | aaa | | 2 | bbb | | 3 | ccc | | 4 | ccc | | 6 | ddd | | 8 | eee | +----+------+ | root@localhost [testdb]>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | aaa | | 2 | bbb | | 3 | ccc | | 4 | ccc | | 6 | ddd | | 8 | eee | +----+------+ |
root@localhost [testdb]>begin; | root@localhost [testdb]>begin; |
root@localhost [testdb]>update t1 set c2='b1' where c1=2; | root@localhost [testdb]>update t1 set c2='b2' where c1=2; |
root@localhost [testdb]>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | aaa | | 2 | b1 | | 3 | ccc | | 4 | ccc | | 6 | ddd | | 8 | eee | +----+------+ | root@localhost [testdb]>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | aaa | | 2 | b2 | | 3 | ccc | | 4 | ccc | | 6 | ddd | | 8 | eee | +----+------+ |
root@localhost [testdb]>commit; | root@localhost [testdb]>commit; |
root@localhost [testdb]>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | aaa | | 2 | b2 | | 3 | ccc | | 4 | ccc | | 6 | ddd | | 8 | eee | +----+------+ | root@localhost [testdb]>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | aaa | | 2 | b1 | | 3 | ccc | | 4 | ccc | | 6 | ddd | | 8 | eee | +----+------+ |
總結(jié):update一條記錄同時提交,有主鍵的情況下,sql_thread是根據(jù)主鍵匹配行記錄,不會校驗行數(shù)據(jù),所以m1更新了m2中表的記錄,m2更新了m1中表的記錄。
例2:有沒有主鍵同時更新一行數(shù)據(jù)的情況:
root@localhost [testdb]>show create table t2\G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`c1` int(11) DEFAULT NULL,
`c2` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
m1 | m2 |
root@localhost [testdb]>select * from t2; +------+------+ | c1 | c2 | +------+------+ | 1 | aaa | | 2 | bbb | +------+------+ | root@localhost [testdb]>select * from t2; +------+------+ | c1 | c2 | +------+------+ | 1 | aaa | | 2 | bbb | +------+------+ |
root@localhost [testdb]>begin; | root@localhost [testdb]>begin; |
root@localhost [testdb]>update t2 set c2='b1' where c1=2; | root@localhost [testdb]>update t2 set c2='b2' where c1=2; |
root@localhost [testdb]>select * from t2; +------+------+ | c1 | c2 | +------+------+ | 1 | aaa | | 2 | b1 | +------+------+ | root@localhost [testdb]>select * from t2; +------+------+ | c1 | c2 | +------+------+ | 1 | aaa | | 2 | b2 | +------+------+ |
root@localhost [testdb]>commit; | root@localhost [testdb]>commit; |
root@localhost [testdb]>select * from t2; +------+------+ | c1 | c2 | +------+------+ | 1 | aaa | | 2 | b1 | +------+------+ | root@localhost [testdb]>select * from t2; +------+------+ | c1 | c2 | +------+------+ | 1 | aaa | | 2 | b2 | +------+------+ |
root@localhost [testdb]>show slave status\G Last_Errno: 1032 Last_Error: Could not execute Update_rows event on table testdb.t2; Can't find record in 't2', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000013, end_log_pos 759 | root@localhost [testdb]>show slave status\G Last_Errno: 1032 Last_Error: Could not execute Update_rows event on table testdb.t2; Can't find record in 't2', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's master log mysql-bin.000026, end_log_pos 3064 |
總結(jié):update一條記錄同時提交,有沒有主鍵的情況下,sql_thread是根據(jù)全表掃描匹配行記錄,所以m1更新在m2中找不到需要更新的行,報1032錯誤,m2更新在m1中找不到需要更新的行,也報1032錯誤。
“mysql互為主從的環(huán)境為什么會出現(xiàn)數(shù)據(jù)不一致”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
文章題目:mysql互為主從的環(huán)境為什么會出現(xiàn)數(shù)據(jù)不一致
標(biāo)題URL:http://jinyejixie.com/article30/pggspo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、云服務(wù)器、標(biāo)簽優(yōu)化、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)