Redis master-slave replication (top)

Redis master-slave replication


Next link: https://blog.csdn.net/qq_43753724/article/details/117428696

1. Redis master-slave replication:

Redis supports simple and useful master-slave replication (master-slave replication) function, this function can make the slave server (slave server) become an exact replica of the master server (master server).

2. What can Redis master-slave replication do?

  • Read and write separation
  • Disaster recovery

3. Related operation commands

3.1 SLAVEOF host port

By executing the SLAVEOF host port command, the current server can be transformed into a slave server of the specified server.

If the current server is already a slave server of a master server, executing SLAVEOF host port
will stop the current server from synchronizing with the old master server, discard the old data set, and start synchronizing with the new master server. In addition, executing the command SLAVEOF NO
ONE on a slave server will make the slave server close the replication function and switch from the slave server back to the master server. The data set from the original synchronization will not be discarded. Using the feature of "SLAVEOF NO ONE
will not discard the data set obtained by synchronization", the slave server can be used as the new master server when the master server fails, so as to achieve uninterrupted operation.

3.2 ROLE

Returns the role played by the instance in replication. This role can be master, slave, or sentinel.

In addition to the role, the command will also return other information related to the role, among which: The master server will return the IP address and port of the subordinate server.
The slave server will return the IP address, port, connection status, and replication offset of the master server that it is replicating.
Sentinel will return the list of master servers it is monitoring.

3.3 info replication

View master/slave replication information

4. Example of read-write separation

4.1, with slave library but not with master library

4.2. Configuration from the library:

slaveof 主库IP 主库端口

Note: Every time you disconnect from the master, you need to reconnect, unless you configure the redis.conf file

4.3 Modify the detailed operation of the configuration file

4.3.1 Copy multiple redis.conf files

cd /usr/local/bin
ll
Insert picture description here


Copy the redis.conf file:

cp redis.conf redis6379.conf
cp redis.conf redis6380.conf
cp redis.conf redis6381.conf
Insert picture description here


Here only demonstrate the modification of redis6379.conf configuration, the other two are to replace 6379 with 6380 and 6381 respectively.

vi redis6379.conf

4.3.2 Enable daemonize yes

Insert picture description here

4.3.3 Modify the name of the pid file

Insert picture description here

4.3.4 Designated Port

Insert picture description here

4.3.5 Set Log file name

Insert picture description here

4.3.6 Modify the name of the Dump.rdb file

Insert picture description here

5. Master-slave replication

That is, one master server, two slave servers

5.1 One master and two servants

Here you need to start three terminals, and use the 6379, 6380, 6381 ports configured above respectively

5.1.1 Start and view master-slave replication information

Port 6379:

Insert picture description here


Insert picture description here
Insert picture description here


Set several values:

set k1 v1
set k2 v2
set k3 v3
set k3 v4

Port 6380:

Insert picture description here


Port 6381:

Insert picture description here
You can see that before the configuration, the value of the role is master, that is, the master server

5.1.2 Configure port 6380 and 6381 as slave server of port 6379

Port 6380:

slaveof 127.0.0.1 6379
Insert picture description here


Insert picture description here


Value: It

Insert picture description here


is no problem to take the value set by the main server

Port 6381:

 SLAVEOF 127.0.0.1 6379
Insert picture description here


Insert picture description here
You can see that the value of the role configuration item of the two slave servers has changed to slave

5.1.3 Log View

cd /usr/local/bin
ll
Insert picture description here

5.1.4 Can the slave machine write? (Verify read-write separation)

Assuming that all three servers need to set k6 (assuming that the host is set first) the
main server:

set k6 v6
Insert picture description here


6380:

set k6 v66
Insert picture description here


6381:

set k6 v666
Insert picture description here


Judging from the results, the slave cannot write, which is in line with the separation of read and write

5.1.5 What happens after SHUTDOWN? Is the slave on the upper position or on standby?

Turn off the host first (6379):

Insert picture description here


check the master-slave configuration information of the slave:

Insert picture description here


Insert picture description here


you can see whether the slave is the slave, the salted fish is turned over or the salted fish, the connection status becomes down, and the slave is on standby

5.1.6 After the master comes back, the master adds a new record, can the slave replicate smoothly?

Restart the host and set the value set k7 v7

Insert picture description here


to get k7:
6380:

Insert picture description here


6381 from the machine :

Insert picture description here
As you can see from the result, the slave can copy

5.1.7 After a slave machine goes down, can it keep up with the big army according to the original situation?

Disconnect the slave of port 6380:

Insert picture description here


set the host (6379) settingsset k8 v8

Insert picture description here

Port 6381:
No problem.

Insert picture description here


Port 6380 is down. After restarting, we will get k8 to

Insert picture description here


Insert picture description here


view the master-slave configuration information: From the picture below, this is the role of port 6380 that has been programmed to master, and it is disconnected from the original 6379 host.

Insert picture description here


Try to get k8

get k8
Insert picture description here


Configure 6380 as a slave of 6379 again:

slaveof 127.0.0.1 6379

Get k8 again

get k8
Insert picture description here