设置从库sql_thread延时回放,使得从库晚于主库执行
1、开启延迟复制
第一步:关闭sql_thread线程 stop slave sql_thread;
第二步:设置延时时间(单位为秒) change master to master_delay=60;
第三步:启动sql_thread线程 start slave sql_thread;
2、关闭延迟复制
第一步:关闭sql_thread线程 stop slave sql_thread;
第二步:设置延时时间(单位为秒) change master to master_delay=0;
第三步:启动sql_thread线程 start slave sql_thread;
3、作用
用户开发、后台管理员误操作(drop,delete)的恢复
第一步:停止所有从库的sql_thread线程 stop slave sql_thread;
第二步:show slave status \G;查询起点(手工回放relay log)
Relay_Log_File: localhost-relay-bin.000025 #回放文件名
Relay_Log_Pos: 407 #回放起点位置
第三步:查询回放文件的结束位置(也就是你误操作的前面)show relaylog events in 'localhost-relay-bin.000025';
mysql> show relaylog events in 'localhost-relay-bin.000025';
+----------------------------+-----+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+----------------------------+-----+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| localhost-relay-bin.000025 | 4 | Format_desc | 2 | 123 | Server ver: 5.7.36-log, Binlog ver: 4 |
| localhost-relay-bin.000025 | 123 | Previous_gtids | 2 | 194 | cdb77c1e-5769-11ed-a384-000c29274ab2:1-145 |
| localhost-relay-bin.000025 | 194 | Rotate | 1 | 0 | mysql-bin.000008;pos=4 |
| localhost-relay-bin.000025 | 241 | Format_desc | 1 | 123 | Server ver: 5.7.36-log, Binlog ver: 4 |
| localhost-relay-bin.000025 | 360 | Rotate | 0 | 407 | mysql-bin.000008;pos=194 |
| localhost-relay-bin.000025 | 407 | Gtid | 1 | 259 | SET @@SESSION.GTID_NEXT= 'cdb77c1e-5769-11ed-a384-000c29274ab2:146' |
| localhost-relay-bin.000025 | 472 | Query | 1 | 338 | BEGIN |
| localhost-relay-bin.000025 | 551 | Intvar | 1 | 370 | INSERT_ID=13 |
| localhost-relay-bin.000025 | 583 | Query | 1 | 621 | use `test`; insert into t1 values (null,'test-13','2022-10-29 23:45:16'),(null,'test-14','2022-10-29 23:45:16'),(null,'test-15','2022-10-29 23:45:16'),(null,'test-16','2022-10-29 23:45:16') |
| localhost-relay-bin.000025 | 834 | Xid | 1 | 652 | COMMIT /* xid=22 */ |
| localhost-relay-bin.000025 | 865 | Gtid | 1 | 717 | SET @@SESSION.GTID_NEXT= 'cdb77c1e-5769-11ed-a384-000c29274ab2:147' |
| localhost-relay-bin.000025 | 930 | Query | 1 | 809 | drop database test |
总结出来的结果:换句话说就是当前localhost-relay-bin.000025文件误操作的开始位置是407,结束位置是930;
第四步:截取localhost-relay-bin.000025(relay log)日志文件
mysqlbinlog --skip-gtids --start-position=407 --stop-position=930 /data/mysql/mysql3306/data/localhost-relay-bin.000025 >./2022-10-31relay.sql
第五步:关闭binlog日志 set sql_log_bin=0;
第六步:进入从库增量备份文件到从库中 source /root/2022-10-31relay.sql;
第七步:开启binlog日志 set sql_log_bin=1;
第八步:停止当前服务的从线程 stop slave;
第九步:将主库改成从库就行