您好,登錄后才能下訂單哦!
mysql5.6開始支持延時復制,默認master_delay為0秒,
CHANGE MASTER TO MASTER_DELAY = N;
表示延時N秒
原理:延時復制的本質是sql_thread需要等待延時時間之后才能執行。
延時復制適用場景:
(1)防止主庫誤操作,在復制同步之前,可以停止同步;
(2)用作測試,不用模擬負載就可以實現主從延遲;
(3)用來檢查數據庫以前的數據,如延遲設置為1周,這樣不需要備份恢復就可以看到比對一周以前的數據
(4)reset slave會把SQL_delay的值清零,并且還會把Master_Log_File等值清空,但是不影響復制;
創建一個延時復制: slave: root@localhost [testdb]>stop slave; root@localhost [testdb]>change master to master_delay=60; root@localhost [testdb]>start slave; root@localhost [testdb]>show slave status\G ...... SQL_Delay: 60 --延時時間 SQL_Remaining_Delay: 56 --剩余時間 Slave_SQL_Running_State: Waiting until MASTER_DELAY seconds after master executed event --等待延時 ...... master: root@localhost [testdb]>delete from t1 where c1=4; slave: root@localhost [testdb]>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | aaa | | 2 | bbb | | 3 | ccc | | 4 | ddd | +----+------+ root@localhost [testdb]>show processlist; +----+-------------+-----------+--------+---------+------+----------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------+--------+---------+------+----------------------------------------------------------------+------------------+ | 26 | root | localhost | testdb | Query | 0 | starting | show processlist | | 27 | system user | | NULL | Connect | 345 | Waiting for master to send event | NULL | | 28 | system user | | NULL | Connect | 10 | Waiting until MASTER_DELAY seconds after master executed event | NULL | +----+-------------+-----------+--------+---------+------+----------------------------------------------------------------+------------------+ #在沒有達到60秒之前查看relay-log日志,發現已經寫入relay-lo中,說明延時是阻塞SQL_thread線程 [root@Darren1 data]# mysqlbinlog -vv --base64-output=decode-rows relay-bin.000003 BEGIN /*!*/; # at 452 #170409 22:12:27 server id 330622 end_log_pos 5624 CRC32 0x86f7edf4 Table_map: `testdb`.`t1` mapped to number 147 # at 502 #170409 22:12:27 server id 330622 end_log_pos 5668 CRC32 0x697c52ed Delete_rows: table id 147 flags: STMT_END_F ### DELETE FROM `testdb`.`t1` ### WHERE ### @1=3 /* INT meta=0 nullable=0 is_null=0 */ ### @2='ccc' /* VARSTRING(30) meta=30 nullable=1 is_null=0 */ root@localhost [testdb]>select * from t1; +----+------+ | c1 | c2 | +----+------+ | 1 | aaa | | 2 | bbb | | 3 | ccc | +----+------+
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。