环境: Rocky 8.4
Redis版本要一致
10.0.0.101 : Master
10.0.0.102,10.0.0.103: Slave
====主节点=== 需要做简单配置即可
[21:38:47 root@localhost ~]#sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass 123456" -e "/logfile .*/c logfile /var/log/redis/redis-6379.log" /etc/redis.conf
[21:39:53 root@localhost ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set key1 master-101
OK
127.0.0.1:6379> get key1
"master-101"
10.0.0.101:6379> info
# Replication
role:master
connected_slaves:0
master_replid:2cd2ebc06adb66d63d75d851f08812e143bd5b39
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
=======修改Slave
127.0.0.1:6379> set key1 slave-102
OK
127.0.0.1:6379> get key1
"slave-102"
127.0.0.1:6379> replicaof 10.0.0.101 6379\
(error) ERR value is not an integer or out of range
127.0.0.1:6379> replicaof 10.0.0.101 6379
OK
127.0.0.1:6379> config set masterauth 123456
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.101
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:0
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8ea91cf70efca3bd149d45aeff44bba70326b227
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0
127.0.0.1:6379> get key1 -》 已变成主节点的信息
"master-101"
127.0.0.1:6379> set key2 1 -》 从节点支持读,不支持写
(error) READONLY You can't write against a read only replica.
另一部slave可修改配置文件使其生效
# replicaof <masterip> <masterport
replicaof 10.0.0.101 6379
# 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.
#
# masterauth <master-password>
masterauth 123456
=======在master查看主从信息
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.102,port=6379,state=online,offset=28,lag=0
master_replid:8ea91cf70efca3bd149d45aeff44bba70326b227
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.102,port=6379,state=online,offset=210,lag=1
slave1:ip=10.0.0.103,port=6379,state=online,offset=210,lag=1
master_replid:8ea91cf70efca3bd149d45aeff44bba70326b227
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:210
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:210