在MySQL中主从复制有MHA,而在redis中也有类似的角色sentinel
先设定主节点的持久化方式
127.0.0.1:6379> CONFIG set appendonly yes
OK
127.0.0.1:6379> CONFIG GET appendonly
1) "appendonly"
2) "yes"
127.0.0.1:6379> CONFIG REWRITE
OK
设定从节点监听在一个对外通信的端口上
编辑配置文件/etc/redis.conf
bind 0.0.0.0
在命令行中敲入SLAVEOF 192.168.31.200 6379
但此时还不能复制数据,需要通过主节点的认证密码
127.0.0.1:6379> CONFIG SET masterauth lvqing
OK
127.0.0.1:6379> CONFIG REWRITE
OK
查看从节点的数据
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.31.200
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:323
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
开启redis的sentinel功能,修改配置文件添加
bind 0.0.0.0
sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel auth-pass <master-name> <password>
然后在每一台redis主机上执行
systemctl start redis-sentinel
连接上sentinel
redis-cli -h 192.168.31.200 -p 26379
查看信息
192.168.31.200:26379> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "192.168.31.205"
5) "port"
6) "6379"
7) "runid"
8) "afbd317b620fad2474c5a24b803078702e7624b8"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "988"
19) "last-ping-reply"
20) "988"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "3477"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "2062131"
29) "config-epoch"
30) "13"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "3"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
redis的cluster配置
redis有许多集群技术,这里只介绍官方的redis cluster
修改配置文件/etc/redis.conf
找到REDIS CLUSTER配置段
每一台redis上有两个主从节点,需要复制一份配置文件,修改里面绑定的端口以及cluster的配置文件,然后不使用systemctl而是用/usr/bin/redis-server -C config-file
来启动第二个实例
网友评论