Home  >  Article  >  Database  >  Switch Redis master-slave instances online

Switch Redis master-slave instances online

尚
forward
2020-03-27 09:25:212125browse

Switch Redis master-slave instances online

For some reasons, we may need to replace the redis master machine. We can stop the machine and replace it, but that may affect the user experience. This article briefly describes how to migrate without downtime.

(Recommended: redis video tutorial)

System environment

CentOS 6.3 x64

redis- server 2.6.16

Two machines s1\s2

Operation steps

#1. We start a redis instance on the new redis server, The configuration is the same as the master configuration. The difference is that the configuration file is modified and enabled slave-read-only no,

so that the slave can write, because "Since Redis 2.6 by default slaves are read-only."

2. Make the new redis a slave: redis 127.0.0.1:6379>SLAVEOF s1 6379

Then you can check the log on s2. There will be a lot of synchronization information. You can also use the info command to check the status.

3. After s2 completely synchronizes the data of s1, we modify the redis ip of the game app to the ip of s2.

4. Check whether the game is normal.

5. After there are no problems with the above, execute redis 127.0.0.1:6379> SLAVEOF NO ONE on s2

6. Offline s1

PS:

SLAVEOF host port

The SLAVEOF command is used to dynamically modify the behavior of the replication function while Redis is running.

By executing the SLAVEOF host port command, the current server can be converted 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 cause the current server to stop synchronizing the old master server, discard the old data set, and start synchronizing the new one. The main server performs synchronization.

In addition, executing the command SLAVEOF NO ONE on a slave server will cause the slave server to turn off the replication function and transition from the slave server back to the master server. The original synchronized data set will not be discarded.

Using the feature of "SLAVEOF NO ONE will not discard the synchronized data set", when the main server fails, the slave server can be used as the new main server, thereby achieving uninterrupted operation.

Available versions:

>= 1.0.0

Time complexity:

SLAVEOF host port, O(N), N is to be synchronized amount of data.

SLAVEOF NO ONE, O(1).

Return value:

Always returns OK.

For more redis knowledge, please pay attention to the redis introductory tutorial column.

The above is the detailed content of Switch Redis master-slave instances online. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:ttlsa.com. If there is any infringement, please contact admin@php.cn delete