以前搭建了主從,但沒有設置讀寫分離,從庫也能寫數據。因而想測試下在從庫寫數據會致使同步怎麼樣。 結果發現,slave_sql_running爲no,slava_IO_running仍然爲yes.mysql
因爲從庫寫數據,致使主從數據不一致,若是在主庫寫入和從庫一樣的數據,會致使sql線程終止,查看mysql錯誤日誌以下:sql
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位置可能會變。線程
因此通常須要把主庫臨時加鎖不讓寫。rest
在從庫執行如下命令:日誌
stop slave; set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; start slave; SHOW SLAVE STATUS
經測試,以上方法也能夠。code
用解決方法一存在一個問題。好比在從庫寫入一條數據11, 在主庫寫入一條數據12,咱們知道因爲主從不一樣步會致使slave_sql_running停了。若是經過第一種方法從新鏈接啓動後,再把12這條數據刪除,會報如下錯誤:blog
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這條記錄,仍然會保持同步。因此這裏給我感受是,第二種方式要好一些。ip
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中不能執行的事件