作用
- 数据副本
- 扩展读性能
一主多从,一从只能有一个主节点
数据从 主节点 单向流向 从节点
实现方式
slaveof命令 (取消slaveof no one)- 配置
1 | slaveof ip port |
测试
一般不建议将 主从 放在同一主机上
redis-6379.conf 示例1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49bind 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis-6379.pid
loglevel notice
logfile /var/log/redis/redis-6379.log
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump-6379.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly-6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
redis-6380.conf
1 | slaveof 127.0.0.1 6379 |
查看信息1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35root@tj:~# redis-server redis-6380.conf
root@tj:~# redis-cli -p 6380 info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:210
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:fa8a9a58b8cd9209163293f46f5d4b534a157163
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
root@tj:~# redis-cli info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=336,lag=0
master_replid:fa8a9a58b8cd9209163293f46f5d4b534a157163
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:336
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:336
查看同步1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28root@tj:~# redis-cli
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> exit
root@tj:~# redis-cli -p 6380 get hello
"world"
root@tj:~# redis-cli -p 6380 set hello java
(error) READONLY You can't write against a read only replica.
root@tj:~# redis-cli -p 6380 slaveof no one # 取消
OK
root@tj:~# redis-cli -p 6380 info replication
# Replication
role:master
connected_slaves:0
master_replid:a4ff8594101ef9742847344ca7e6ae80deb3cff7
master_replid2:fa8a9a58b8cd9209163293f46f5d4b534a157163
master_repl_offset:997
second_repl_offset:941
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:997
root@tj:~# redis-cli -p 6380 set hello java
OK
runId 与 偏移量
runId 标识,会在重启后发生过改变1
redis-cli -p 6379 info server |grep run
备节点 感知主节点 runId 变化后,会全量复制
偏移量: 主备 偏移量达到一致的时候,数据一致