redis master-slave replication process and implementation of master-slave replication

1. The process of redis master-slave replication synchronization

1. Send a sync synchronization command from the service to the primary service to require full synchronization
 2. When the primary service receives sync synchronization commands from the service, a subprocess of fork executes bgsave command (non-blocking) snapshot saving in the background, generates an RDB file, and
 RDB file sent to slave service
 3. Load the received RDB file from the service into its own redis memory
 4. After loading the RDB from the slave service, the master service sends all write commands to the slave service
 5. Complete data synchronization by loading all write commands from the primary service into memory from the service
 6. From the next time the service needs to synchronize data, it only needs to send its offset location (equivalent to the location of mysql binlog) to synchronize only the newly added data, no more need to synchronize in full

2. Synchronize redis master-slave via command line

master 192.168.1.9
slave 192.168.1.9
 Note: It is recommended that the versions of master-slave redis be consistent to avoid making a master-slave connection impossible due to version differences

1. Set master's profile

[root@localhost ~]# vim /app/redis/etc/redis.conf
...
bind 127.0.0.0.1 192.168.1.9 #Bind Listening Address
...
port 6379 #Default open port number
....
daemonize yes  #Start as a daemon

supervised systemd  #Start and stop of services are managed by systemd.

pidfile /app/redis/run/redis_6379.pid  #pid save path of process

logfile "/app/redis/log/redis_6379.log"  #Log file save path
save 2 1              #Save snapshots every 2 seconds
stop-writes-on-bgsave-error yes    #Writing redis is disabled when snapshot save fails, defaulting to yes and typically changing to no not open
dbfilename dump.rdb    #File name to save snapshot
dir /app/redis/data/             #Path to save snapshot     
requirepass 123456     #Set the connection password for redis login
...

2. Open the redis main service and view in-memory data

[root@localhost ~]# redis-server /app/redis/etc/redis.conf 
[root@localhost ~]# redis-cli 
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> keys *
1) "wang"
2) "dfd"
3) "lady"
4) "ddsds"
5) "name"
6) "zhang"
127.0.0.1:6379> 

3. View slave Slave slave service status first, and if not set, slave Slave service defaults to master master master service

127.0.0.1:6379> info replication
#Replication
role:master
connected_slaves:0
master_replid:177a7792d02eb8e89c07b5441825a0f50d8ae8b8
master_replid2:37938cb9db73839bfc72e3fc805d42c56dc6352d
master_repl_offset:2240
second_repl_offset:2241
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:57
repl_backlog_histlen:2184

4. Set slave Slave profile from service

[root@localhost ~]# vim /app/redis/etc/redis.conf
...
bind 127.0.0.0.1 192.168.1.106 #Bind Listening Address
...
port 6379 #Default open port number
....
daemonize yes  #Start as a daemon

dir /app/redis/data/             #Path to save snapshot     
requirepass 123456     #Set the connection password for redis login
...

5.slave implements master-slave connections from service command line operations

[root@localhost ~]# redis-server /app/redis/etc/redis.conf
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> slaveof 192.168.1.9 6379   #Slave service set to another service
127.0.0.1:6379>config set masterauth 123456 #Configure the connection password that matches the primary service
127.0.0.1:6379> info replication   #Viewing master-slave connections was successful enough
#Replication
role:slave                       #Switched to slave service
master_host:192.168.1.9  #ip of master service host
master_port:6379
master_link_status:up  #Primary-Slave Connection Established Successfully
.....
.....
27.0.0.1:6379> keys *  #View data synchronized from master server
1) "wang"
2) "name"
3) "dfd"
4) "zhang"
5) "ddsds"
6) "lady"

6. Master-slave synchronization can also be canceled at the command line

127.0.0.1:6379> slaveof no one   #Command to cancel master-slave synchronization
OK
127.0.0.1:6379> info replication  #Check to see if cancellation was successful
#Replication
role:master               #Switched to master
connected_slaves:0
master_replid:77eab93a7015b0d338d1c44040d632e698d0159b
master_replid2:37938cb9db73839bfc72e3fc805d42c56dc6352d
master_repl_offset:4004
second_repl_offset:4005
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2241
repl_backlog_histlen:1764  

3. Master-slave synchronization through configuration files

1. Simply configure the slave service profile

[root@localhost ~]# vim /app/redis/etc/redis.conf
.....
slaveof 192.168.1.9 6379  #Add a slave service belonging to a host
.....
masterauth 123456  #Password to connect master from service
...... 
slave-read-only yes  #Read-only from service, cannot write data on command line
......

2. Restart slave service to achieve master-slave connection

[root@localhost ~]# redis-server /app/redis/etc/redis.conf
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info replication
#Replication
role:slave
master_host:192.168.1.9
master_port:6379
master_link_status:up
......
......

Keywords: Linux Redis snapshot vim MySQL

Added by viveleroi0 on Sat, 15 Jun 2019 20:11:18 +0300