Redis master-slave replication
- 1. Redis master-slave replication:
- 2. What can Redis master-slave replication do?
- 3. Related operation commands
- 3.1 SLAVEOF host port
- 3.2 ROLE
- 3.3 info replication
- 4. Example of read-write separation
- 4.1, with slave library but not with master library
- 4.2. Configuration from the library:
- 4.3 Modify the detailed operation of the configuration file
- 4.3.1 Copy multiple redis.conf files
- 4.3.2 Enable daemonize yes
- 4.3.3 Modify the name of the pid file
- 4.3.4 Designated Port
- 4.3.5 Set Log file name
- 4.3.6 Modify the name of the Dump.rdb file
- 5. Master-slave replication
- 5.1 One master and two servants
- 5.1.1 Start and view master-slave replication information
- 5.1.2 Configure port 6380 and 6381 as slave server of port 6379
- 5.1.3 Log View
- 5.1.4 Can the slave machine write? (Verify read-write separation)
- 5.1.5 What happens after SHUTDOWN? Is the slave on the upper position or on standby?
- 5.1.6 After the master comes back, the master adds a new record, can the slave replicate smoothly?
- 5.1.7 After a slave machine goes down, can it keep up with the big army according to the original situation?
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.
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
Copy the redis.conf file:
cp redis.conf redis6379.conf cp redis.conf redis6380.conf cp redis.conf redis6381.conf
Here only demonstrate the modification of redis6379.conf configuration, the other two are to replace 6379 with 6380 and 6381 respectively.
4.3.2 Enable daemonize yes
4.3.3 Modify the name of the pid file
4.3.4 Designated Port
4.3.5 Set Log file name
4.3.6 Modify the name of the Dump.rdb file
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
Set several values:
set k1 v1 set k2 v2 set k3 v3 set k3 v4
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
slaveof 127.0.0.1 6379
is no problem to take the value set by the main server
SLAVEOF 127.0.0.1 6379
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
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
set k6 v6
set k6 v66
set k6 v666
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):
check the master-slave configuration information of the slave:
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
to get k7:
6381 from the machine :
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:
set the host (6379) settings
set k8 v8
Port 6380 is down. After restarting, we will get k8 to
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.
Try to get k8
Configure 6380 as a slave of 6379 again:
slaveof 127.0.0.1 6379
Get k8 again