MySQL中主從復(fù)制重復(fù)鍵問(wèn)題修復(fù)方法
-------------------quote begin------------------------ 3. If you decide that you can skip the next statement from the master, issue the following statements: mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n; mysql> START SLAVE; The value of n should be 1 if the next statement from the master does not use AUTO_INCREMENT or LAST_INSERT_ID(). Otherwise, the value should be 2. The reason for using a value of 2 for statements that use AUTO_INCREMENT or LAST_INSERT_ID() is that they take two events in the binary log of the master.
-------------------quote end------------------------
MySQL文檔中的意思是當(dāng)master傳到slave的語(yǔ)句中要用到auto_increment,或者last_insert_id()時(shí),需要skip兩個(gè)event. 但實(shí)際情況并非如此
測(cè)試過(guò)程如下: 172.16.161.26 為master 172.16.161.15 為slave 同步c2cdb,初始狀態(tài)ok
1. 在master上創(chuàng)建測(cè)試表
mysql> create table tmp_test_0208(id int not null auto_increment,name varchar(30),primary key(id)) engine=innodb; Query OK, 0 rows affected (0.20 sec)
2, 在salve上insert 3條記錄
mysql> insert into tmp_test_0208 values(1,'a'),(2,'b'),(3,'c'); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from tmp_test_0208; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | +----+------+ 3 rows in set (0.00 sec)
3, 在master上insert 3條記錄
mysql> insert into tmp_test_0208(name) values('a'),('b'),('c'); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from tmp_test_0208; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | +----+------+ 3 rows in set (0.00 sec)
4, slave 的sql thread 中止
/usr/local/mysql/bin/mysql -uroot -pxxx c2cdb -s -e "show slave status\G" |egrep "Slave_IO_Running|Sl ave_SQL_Running" Slave_IO_Running: Yes Slave_SQL_Running: No
5, skip next statemate后start slave正常
mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1 ; Query OK, 0 rows affected (0.00 sec) mysql> slave start; Query OK, 0 rows affected (0.00 sec) /usr/local/mysql/bin/mysql -uroot -pxxx c2cdb -s -e "show slave status\G" |egrep "Slave_IO_Running|Sl ave_SQL_Running" Slave_IO_Running: Yes Slave_SQL_Running: Yes
slave端errlog如下: 070208 16:07:59[ERROR] Slave: Error 'Duplicate entry '1' for key 1' on query. Default database: 'c2cdb'. Query: 'insert into tmp_te st_0208(name) values('a'),('b'),('c')', Error_code: 1062
070208 16:07:59 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'db_auction1-bin.000203' position 14215101
070208 16:09:59 [Note] Slave SQL thread initialized, starting replication in log 'db_auction1-bin.000203' at position 14215101, rela y log './db_auction1_b-relay-bin.000457' position: 200682931
master羰binlog中相應(yīng)的記錄如下:
# at 14215101 #070208 16:08:00 server id 1 log_pos 14215101 Intvar SET INSERT_ID=1; # at 14215129 #070208 16:08:00 server id 1 log_pos 14215129 Query thread_id=2744782 exec_time=0 error_code=0 SET TIMESTAMP=1170922080; insert into tmp_test_0208(name) values('a'),('b'),('c');
總結(jié):使用SET GLOBAL SQL_SLAVE_SKIP_COUNTER 命令跳過(guò)失敗的SQL
相關(guān)文章
MySQL更改默認(rèn)字符集為utf-8的全過(guò)程
這篇文章主要介紹了MySQL更改默認(rèn)字符集為utf-8的全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12MySQL與Mongo簡(jiǎn)單的查詢實(shí)例代碼
本文通過(guò)一個(gè)實(shí)例給大家用MySQL和mongodb分別寫(xiě)一個(gè)查詢,本文圖片并茂給大家介紹的非常詳細(xì),感興趣的朋友參考下吧2016-10-10Mysql 切換數(shù)據(jù)存儲(chǔ)目錄的實(shí)現(xiàn)方法
這篇文章主要介紹了Mysql 切換數(shù)據(jù)存儲(chǔ)目錄的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-07-07Mysql 數(shù)據(jù)庫(kù)雙機(jī)熱備的配置方法
mysql數(shù)據(jù)庫(kù)沒(méi)有增量備份的機(jī)制,當(dāng)數(shù)據(jù)量太大的時(shí)候備份是一個(gè)很大的問(wèn)題。還好mysql數(shù)據(jù)庫(kù)提供了一種主從備份的機(jī)制,其實(shí)就是把主數(shù)據(jù)庫(kù)的所有的數(shù)據(jù)同時(shí)寫(xiě)到備份數(shù)據(jù)庫(kù)中。2010-06-06Mysql事務(wù)并發(fā)臟讀+不可重復(fù)讀+幻讀詳解
這篇文章主要介紹了Mysql事務(wù)并發(fā)臟讀+不可重復(fù)讀+幻讀詳解,文章基于Mysql事務(wù)的相關(guān)資料展開(kāi)對(duì)主題的詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-04-04Mysql關(guān)于進(jìn)程中的死鎖和解除鎖問(wèn)題
Mysql 經(jīng)常會(huì)遇到語(yǔ)句或者存儲(chǔ)過(guò)程長(zhǎng)時(shí)間沒(méi)有反應(yīng),大概率就是掛掉了,或者死鎖了,這篇文章主要介紹了Mysql關(guān)于進(jìn)程中的死鎖和解除鎖問(wèn)題,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07