Redis主从安装部署
先按照之前安装redis的文章安装两个redis节点
资源清单
10.0.0.100 主
10.0.0.101 从
redis配置文件修改
port 6379 #端口
daemonize yes # 后台启动
pidfile /data/redis.pid #pid存放路径
logfile "/data/redis.log" #redis日志路径
dbfilename dump.rdb #持久化文件名字
dir /data/ #持久化文件存放路径
requirepass 123 #redis密码
masterauth 123 #主从连接密码&reids连接密码
protected-mode no #关闭保护模式
bind 0.0.0.0 #任何人都可以访问
启动两个节点redis
10.0.0.100 redis-server /usr/local/redis/redis.conf
10.0.0.101 redis-server /usr/local/redis/redis.conf
手动开启主从
#从库执行
redis-cli -h 10.0.0.101 -p 6379 -a 123 SLAVEOF 10.0.0.100 6379 #这里的ip跟端口是主库的
查询主从状态
主节点
[root@redis01 redis]# redis-cli -h 10.0.0.100 -p 6379 -a 123 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.101,port=6379,state=online,offset=98,lag=1
master_failover_state:no-failover
master_replid:4f4291fd54446202170717bddfa5c127572ef8dc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:112
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:112
从节点
[root@redis02 redis]# redis-cli -h 10.0.0.101 -p 6379 -a 123 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.100
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:168
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:4f4291fd54446202170717bddfa5c127572ef8dc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:168
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:168
模拟主库宕机
pkill redis-server
#从库日志
7109:S 08 Aug 2022 16:34:04.489 * MASTER <-> REPLICA sync started
7109:S 08 Aug 2022 16:34:04.489 # Error condition on socket for SYNC: Connection refused
7109:S 08 Aug 2022 16:34:05.514 * Connecting to MASTER 10.0.0.100:6379
7109:S 08 Aug 2022 16:34:05.514 * MASTER <-> REPLICA sync started
7109:S 08 Aug 2022 16:34:05.514 # Error condition on socket for SYNC: Connection refused
7109:S 08 Aug 2022 16:34:06.540 * Connecting to MASTER 10.0.0.100:6379
7109:S 08 Aug 2022 16:34:06.540 * MASTER <-> REPLICA sync started
7109:S 08 Aug 2022 16:34:06.541 # Error condition on socket for SYNC: Connection refused
7109:S 08 Aug 2022 16:34:07.567 * Connecting to MASTER 10.0.0.100:6379
7109:S 08 Aug 2022 16:34:07.567 * MASTER <-> REPLICA sync started
7109:S 08 Aug 2022 16:34:07.567 # Error condition on socket for SYNC: Connection refused
切换从库为主库
redis-cli -h 10.0.0.101 -p 6379 -a 123 replicaof no one
#日志输出
7109:M 08 Aug 2022 16:35:55.097 * Discarding previously cached master state.
7109:M 08 Aug 2022 16:35:55.097 # Setting secondary replication ID to 4f4291fd54446202170717bddfa5c127572ef8dc, valid up to offset: 533. New replication ID is a02d32a32685211066b44650c4b5fa72c24b62ce
7109:M 08 Aug 2022 16:35:55.097 * MASTER MODE enabled (user request from 'id=7 addr=10.0.0.101:39062 laddr=10.0.0.101:6379 fd=7 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=36 qbuf-free=40918 argv-mem=14 obl=0 oll=0 omem=0 tot-mem=61478 events=r cmd=replicaof user=default redir=-1')
网友评论