redis master-slave replication - one master and two servants

Master-slave replication

This article is applicable to the redis course in Silicon Valley, which comes from
32p of the video is the version without password. I have a password version. I have encountered some problems in the experiment. I hereby sort it out for your reference.

Instructions before experiment:

① My current computer redis The path of the configuration file is /etc/redis.conf
② My current redis The password is set to 123456.csz
③ To understand the problem of work path, make sure you enter the correct path every time
④ Make sure your redis.conf The configuration file has been changed to the following
# Allow any host to connect and access
bind Change to bind
# Turn off protection mode
protected-mode yes Change to protected-mode no
# After the command line is started, it is still allowed to run in the background
daemonize no Change to daemonize yes

Common commands

mkdir /folder  New folder
cd /folder Enter a folder
vim filename Edit a file
ps -ef | grep redis  see redis process
kill -9 process id    Force to kill a process
redis-server redis6381.conf  Start according to the specified configuration file redis
redis-cli -p 6381 -a 123456.csz  redis Start the client with password
redis-cli -p 6379    redis Start client without password
info replication  see redis Host operation (in redis-cli (used in the client)
Step 1: create a folder / myredis

Current working directory: / root

mkdir /myredis
Step 2: Add / etc / redis The conf configuration file is copied to / myredis, and the copied file is called redis conf

You must have a redis Conf file, you need to know your redis Where is the conf configuration file

cp /etc/redis.conf /myredis/redis.conf
Step 3: create three configuration files in / myredis
vi /myredis/redis6379.conf
vi /myredis/redis6380.conf
vi /myredis/redis6381.conf

Copy the following contents into three configuration files and change each 6379 to 63806381

include /myredis/redis.conf
pidfile /var/run/
port 6379
dbfilename dump6379.rdb

If the redis password is set: add a line under the configuration file of the slave (redis6380.conf,redis6381.conf) (both slaves need it): pass is your own password

masterauth password

As shown below:

Step 4: start three redis according to these three configuration files
redis-server /myredis/redis6379.conf
redis-server /myredis/redis6380.conf
redis-server /myredis/redis6381.conf

Check whether the process is created:

ps -ef | grep redis

The process started correctly as follows

root     29853     1  0 21:27 ?        00:00:00 redis-server *:6379
root     29883     1  0 21:27 ?        00:00:00 redis-server *:6380
root     29919     1  0 21:27 ?        00:00:00 redis-server *:6381
root     29979   489  0 21:27 pts/0    00:00:00 grep --color=auto redis
Step 5: enter the redis cli client and check the operation of the three hosts

① With password:

redis-cli -p 6381 -a 123456.csz 

② No password:

redis-cli -p 6379

Check the operation of three hosts

info replication

At this time, the identity of the three hosts is master

Step 6: configure 6380 and 6381 as slave servers

First enter the redis client of 6380 and 6381, and then execute the following command

slaveof 6379

As shown in the figure, there are two slaves under the host of port 6379. The role is master, which represents the host

Under port 6380, the role is slave, which means slave 1 and the connected host (ip:, port: 6379)

Under port 6381, the role is slave, which means slave 2 and the connected host (ip:, port: 6379)

Step 7: Test

Test content:
① Write to the host, read from the host, and read from the slave.

② slave write

Conclusion: the host is readable and writable, while the slave is read-only and not written

② Turn off the host to see if the slave can read data. Restart the host to see if it is consistent

Conclusion: the master hangs up and the slave remains. Restart the host and restore as before.

③ The slave hangs up and needs to be reset after restart
(if the master auth password is configured in the slave configuration file, you don't need to perform this step. After the slave is restarted, it will be restored as before.)

slaveof 6379

③ The slave machine hangs up and needs to be reset after restart (if the master auth password is configured in the slave machine configuration file, there is no need to perform the next step, and the slave machine will be restored after restart)

slaveof 6379

If you can help us, please give us a praise 👍.

Tags: Java Redis

Posted by toms on Fri, 15 Apr 2022 01:23:55 +0930