時間:2020-08-09來源:www.farandoo.com作者:電腦系統城
之前搭建了主從,但沒有設置讀寫分離,從庫也能寫數據。于是想測試下在從庫寫數據會導致同步怎么樣。 結果發現,slave_sql_running為no,slava_IO_running仍然為yes.
由于從庫寫數據,導致主從數據不一致,如果在主庫寫入和從庫同樣的數據,會導致sql線程終止,查看mysql錯誤日志如下:
2020-08-01T10:58:19.623077Z 135 [ERROR] Slave SQL for channel '': Could not execute Write_rows event on table shy_dep.zp_test; , Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000001, end_log_pos 882496, Error_code: 1062
2020-08-01T10:58:19.623101Z 135 [Warning] Slave: Error_code: 1062
2020-08-01T10:58:19.623110Z 135 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000001' position 882218
CHANGE MASTER TO master_host = '192.168.164.84',
MASTER_PORT = 3306,
master_user = 'root',
master_password = 'root',
master_log_file = 'mysql-bin.000001',
master_log_pos = 902262;#這里記錄master最新的position
通過以上步驟,可以實現主從重新開始同步。
PS: 這里在重新啟動從庫同步時,假設主庫沒有進行寫操作。因為如果進行了寫操作,則剛才記錄的主庫position位置可能會變。
所以一般需要把主庫臨時加鎖不讓寫。
在從庫執行以下命令:
stop slave;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
SHOW SLAVE STATUS
經測試,以上方法也可以。
用解決方法一存在一個問題。比如在從庫寫入一條數據11, 在主庫寫入一條數據12,我們知道由于主從不同步會導致slave_sql_running停了。如果通過第一種方法重新連接啟動后,再把12這條數據刪除,會報以下錯誤:
2020-08-01T11:00:38.853703Z 17564 [ERROR] Slave SQL for channel '': Could not execute Delete_rows event on table shy_dep.zp_test; Unknown error 1032, Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000001, end_log_pos 883098, Error_code: 1032
2020-08-01T11:00:38.853717Z 17564 [Warning] Slave: Unknown error 1032 Error_code: 1032
2020-08-01T11:00:38.853721Z 17564 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000001' position 882828
從庫由于找不到12這條記錄進行刪除從而會終止slave_sql_running這個線程,需要再重新連接主庫的binlog最新位置進行同步。
而解決方法二,即使刪除了12這條記錄,仍然會保持同步。所以這里給我感覺是,第二種方式要好一些。
set global sql_slave_skip_counter=N #這里的N是指跳過N個event
官方解釋:
This statement skips the next N events from the master. This is useful for recovering from replication stops caused by a statement.
個人理解,就是跳過當前從master中不能執行的事件
2022-03-09
MySQL存儲過程圖文實例講解教程2022-03-01
千萬級用戶系統SQL調優實戰分享2022-03-01
mysql遠程跨庫聯合查詢的示例為什么要主從同步? 主從同步的原理 MYSQL數據庫進行主從同步 創建兩個MYSQL數據庫 初始化,安裝主機數據庫 配置從機 測試主從同步 ...
2022-03-01
這篇文章主要介紹了銀河麒麟V10安裝MySQL8028的圖文教程,并詳細介紹了遠程訪問的實現方法,本文通過圖文命令給大家介紹的非常詳細...
2022-02-28