The cluster construction is a Master (one-time big brother, lifelong big brother) and two slaves.
1. First, copy three copies of the redis.conf configuration file in the bin folder of the redis installation directory, and name them as shown in the following figure.
Check the added configuration file:
2. Modify each configuration file
① Modify the redis-master.conf master node configuration file:
修改:
# 绑定本机ip地址
# bind 127.0.0.1 -::1
bind 192.168.184.128
#设置端口号
# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
Port 6380
# By default Redis does not run as a daemon. Use 'yes' if you need it.
daemonize yes
# The filename where to dump the DB
dbfilename dump6380.rdb 或者修改dir
# master自己的密码(没有密码可忽略)
requirepass 123456
② Modify the redis-slaver1.conf master node configuration file:
bind 192.168.184.128
Port 6381
daemonize yes
dbfilename dump6381.rdb 或者修改dir
#设置节点从属于master主节点
Replicaof 192.168.31.193 6380
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
# 如果redis的master设置了密码,需要添加下列认证
# masterauth <master-password>
masterauth 123456
# slaver1自己的密码(没有密码可忽略)
requirepass 123456
③ Modify the redis-slaver2.conf master node configuration file:
修改:
bind 192.168.184.128
Port 6382
daemonize yes
dbfilename dump6383.rdb 或者修改dir
Replicaof 192.168.31.193 6380
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
# 如果redis的master设置了密码,需要添加下列认证
# masterauth <master-password>
masterauth 123456
# slaver2自己的密码(没有密码可忽略)
requirepass 123456
3. Test cluster
Master can read and write, slave can only read.
Master write operations will be synchronized to the slave.
Disadvantages: The master is down, the slave will not be elected as the master, and the slave is still read-only.
①Start the services of the master node master and slave nodes slave1 and slave2 in the background, and check the startup results.
②Master node test
③Slave node slave1 test, can only read the data of the master node master, and cannot add data by itself.
④ The slave node slave2 test can only read the data of the master node master, and cannot add data by itself.
⑤ Test that the master node master is down, and directly kill the process of the master node master.
The slave nodes slave1 and slave2 can only read the master master node data, will not re-elect a new master, and cannot add data.