Introduction
So what is delayed replication technology? Starting from MySQL 5.6, the master-slave delayed replication is supported.
Simply put, delayed replication is to set a fixed delay time,
such as N hours, so that the slave database is N hours behind the master database.
It is used to quickly restore data after database misoperation.
For example, you can set the update delay of a certain slave library and the main library by 1 hour,
so that after the main library data has a problem, it is found within 1 hour, and the slave library
can be restored to make it still correct and complete data ,
Save the time taken by the overall data recovery.
Master-slave replication configuration
Reference: Mysql gives GTID master-slave configuration
Master-slave configuration
Mysql configuration installation
[root@test1 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@test1 ~]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
[root@test1 ~]# yum install mysql-community-server
[root@test1 ~]# systemctl start mysqld
Main configuration file
vim /etc/my.cnf
# binlog 和 server id配置
server-id=1
log-bin=mysql-bin
binlog-do-db=test
binlog-ignore-db=mysql
# gtid 配置
gtid_mode=ON
enforce-gtid-consistency=true
Configuration file from library
vim /etc/my.cnf
server-id=10
# gtid 配置
gtid_mode=ON
enforce-gtid-consistency=true
# 并行复制配置
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=8
master_info_repository=TABLE
# 中继日志配置
relay_log_info_repository=TABLE
relay_log_recovery=ON
Restart Mysql to take effect after configuration
For master-slave configuration, refer to the link above https://blog.51cto.com/9025736/2500516
Delayed replication configuration
延迟复制配置,通过设置slave上的 MASTER TO MASTER_DELAY参数实现。
CHANGE MASTER TO MASTER_DELAY = N;
N为多少秒,该语句设置从数据库延时N秒后,再与主数据库进行数据同步复制。
由于是测试,这里配置延迟主库10分钟,至于生产配置多长时间,就需要自己斟酌设置了。
配置完毕主从,我们只需要停止主从,在设置MASTER_DELAY(切记时间放到晚上不活跃了在操作)
mysql> stop slave;
mysql> change master to MASTER_DELAY = 600;
mysql> start slave;
Insert data test
Delayed replication principle
mysql的延迟复制实际上影响的只是SQL线程将数据应用到从数据库,
而I/O线程早已经把主库更新到数据写入到了从库的中继日志中。
因此,在延迟复制期间即使主库down掉了,
从库到了延迟复制的时间,依然会把数据更新到和主库down机时一致。