1.Redis复制简介
我们所说的主从复制,主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主
2.Redis作用
读写分离
容灾恢复
3.Redis复制范例
1)一主二仆
(1)Redis6379.conf
[root@pluto 桌面]# cd /usr/local/bin/ [root@pluto bin]# clear [root@pluto bin]# redis-server /myredis/redis6379.conf [root@pluto bin]# redis-cli -p 6379 127.0.0.1:6379> ping PONG 127.0.0.1:6379> INFO replication # Replication role:master 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 127.0.0.1:6379> set k1 v1 ok 127.0.0.1:6379> set k2 v2 OK 127.0.0.1:6379> set k3 v3 OK 127.0.0.1:6379> get k3 "v3" 127.0.0.1:6379> keys * 1) "k2" 2) "k3" 3) "k1" 127.0.0.1:6379> set k4 v4 OK 127.0.0.1:6379> INFO replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=221,lag=1 slave1:ip=127.0.0.1,port=6381,state=online,offset=221,lag=1 master_repl_offset:221 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:220 127.0.0.1:6379> set k6 v6
|
(2)Redis6380.conf
[root@pluto 桌面]# cd /usr/local/bin/ [root@pluto bin]# clear [root@pluto bin]# redis-server /myredis/redis6380.conf [root@pluto bin]# redis-cli -p 6380 127.0.0.1:6380> ping PONG 127.0.0.1:6380> INFO replication # Replication role:master 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 127.0.0.1:6380> SLAVEOF 127.0.0.1 6379 OK 127.0.0.1:6380> get k4 "v4" 127.0.0.1:6380> get k1 "v1" 127.0.0.1:6380> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_repl_offset:235 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 127.0.0.1:6380> set k6 v66 (error) READONLY You can't write against a read only slave. 127.0.0.1:6380> set k66 v66 (error) READONLY You can't write against a read only slave. 127.0.0.1:6380> set k7 v7 (error) READONLY You can't write against a read only slave. 127.0.0.1:6380>
|
(3)Redis6381.conf
[root@pluto 桌面]# cd /usr/local/bin/ [root@pluto bin]# clear [root@pluto bin]# redis-server /myredis/redis6381.conf [root@pluto bin]# redis-cli -p 6381 127.0.0.1:6381> ping PONG 127.0.0.1:6381> INFO replication # Replication role:master 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 127.0.0.1:6381> SLAVEOF 127.0.0.1 6379 OK 127.0.0.1:6381> get k4 "v4" 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:7 master_sync_in_progress:0 slave_repl_offset:235 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 127.0.0.1:6381> set k6 v666 (error) READONLY You can't write against a read only slave. 127.0.0.1:6381>
|
[1]主机挂
127.0.0.1:6379> INFO replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=221,lag=1 slave1:ip=127.0.0.1,port=6381,state=online,offset=221,lag=1 master_repl_offset:221 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:220 127.0.0.1:6379> set k6 v6 OK 127.0.0.1:6379> SHUTDOWN not connected> exit [root@pluto bin]# redis-server /myredis/redis6379.conf [root@pluto bin]# redis-cli -p 6379 127.0.0.1:6379> keys * 1) "k2" 2) "k1" 3) "k6" 4) "k3" 5) "k4" 127.0.0.1:6379> set k7 v7 OK 127.0.0.1:6379> |
127.0.0.1:6380> keys * 1) "k2" 2) "k3" 3) "k1" 4) "k4" 5) "k6" 127.0.0.1:6380> INFO replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:684 master_link_down_since_seconds:17 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 127.0.0.1:6380> get k7 "v7" 127.0.0.1:6380> |
127.0.0.1:6381> keys * 1) "k1" 2) "k4" 3) "k6" 4) "k3" 5) "k2" 127.0.0.1:6381> INFO replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:684 master_link_down_since_seconds:29 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 127.0.0.1:6381> get k7 "v7" |
[2]从机挂
127.0.0.1:6379> set k8 v8 OK 127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=334,lag=1 master_repl_offset:334 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:333 127.0.0.1:6379> |
127.0.0.1:6380> SHUTDOWN not connected> exit [root@pluto bin]# redis-server /myredis/redis6380.conf [root@pluto bin]# redis-cli -p 6380 127.0.0.1:6380> info replication # Replication role:master 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 127.0.0.1:6380> get k8 (nil)
|
127.0.0.1:6381> get k8 "v8" 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_repl_offset:348 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 127.0.0.1:6381> |
2)薪火相传
(1)redis6379.conf
127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6381,state=online,offset=642,lag=1 slave1:ip=127.0.0.1,port=6380,state=online,offset=642,lag=1 master_repl_offset:642 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:641 127.0.0.1:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6380,state=online,offset=684,lag=0 master_repl_offset:684 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:683 127.0.0.1:6379> |
(1)redis6380.conf
127.0.0.1:6380> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6381,state=online,offset=642,lag=1 slave1:ip=127.0.0.1,port=6380,state=online,offset=642,lag=1 master_repl_offset:642 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:641 127.0.0.1:6380> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6380,state=online,offset=684,lag=0 master_repl_offset:684 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:683 127.0.0.1:6380> |
(1)redis6381.conf
127.0.0.1:6381> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6381,state=online,offset=642,lag=1 slave1:ip=127.0.0.1,port=6380,state=online,offset=642,lag=1 master_repl_offset:642 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:641 127.0.0.1:6381> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6380,state=online,offset=684,lag=0 master_repl_offset:684 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:683 127.0.0.1:6381> |
3)反客为主
(1)redis6379.conf
127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=1058,lag=1 slave1:ip=127.0.0.1,port=6381,state=online,offset=1058,lag=1 master_repl_offset:1058 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:1057 127.0.0.1:6379> SHUTDOWN not connected> exit [root@pluto bin]# redis-server /myredis/redis6379.conf [root@pluto bin]# redis-cli -p 6379 127.0.0.1:6379> info replication # Replication role:master 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 127.0.0.1:6379> |
(2)redis6380.conf
127.0.0.1:6380> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:1072 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:403 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:402 127.0.0.1:6380> SLAVEOF no one OK 127.0.0.1:6380> info replication # Replication role:master connected_slaves:0 master_repl_offset:445 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:444 127.0.0.1:6380> set k10 v10 OK 127.0.0.1:6380> |
(3)redis6381.conf
127.0.0.1:6381> SLAVEOF 127.0.0.1 6379 OK 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:8 master_sync_in_progress:0 slave_repl_offset:1072 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 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:1086 master_link_down_since_seconds:32 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 127.0.0.1:6381> SLAVEOF 127.0.0.1 6380 OK 127.0.0.1:6381> get k10 "v10" 127.0.0.1:6381> |
4.Redis复制原理
5.Redis哨兵模式
1).哨兵模式简介
反客为主的自动版,能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库
2).哨兵模式使用
[1]redis6379.conf
127.0.0.1:6379> SHUTDOWN not connected> exit [root@pluto bin]# redis-server /myredis/redis6379.conf [root@pluto bin]# redis-cli -p 6379 127.0.0.1:6379> info replication # Replication role:master 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 127.0.0.1:6379> info replication # Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:12464 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 127.0.0.1:6379> |
[2]redis6380.conf
127.0.0.1:6380> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:8 master_sync_in_progress:0 slave_repl_offset:365 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:365 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:364 127.0.0.1:6380> keys * 1) "k4" 2) "k9" 3) "k7" 4) "k3" 5) "k8" 6) "k2" 7) "k1" 8) "k6" 127.0.0.1:6380> info replication # Replication role:master connected_slaves:1 slave0:ip=127.0.0.1,port=6381,state=online,offset=8735,lag=0 master_repl_offset:8735 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:8734 127.0.0.1:6380> set k10 v10 OK 127.0.0.1:6380> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6381,state=online,offset=13689,lag=1 slave1:ip=127.0.0.1,port=6379,state=online,offset=13822,lag=0 master_repl_offset:13822 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen: |
[3]redis6381.conf
127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:379 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 127.0.0.1:6381> keys * 1) "k1" 2) "k7" 3) "k4" 4) "k6" 5) "k9" 6) "k3" 7) "k8" 8) "k2" 127.0.0.1:6381> info replication # Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:8602 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 127.0.0.1:6381> get k10 "v10" 127.0.0.1:6381> |
6.Redis复制缺点
由于所有的写操作都是先在Master上操作,然后同步更新到Slave上,所以从Master同步到Slave机器有一定的延迟,当系统很繁忙的时候,延迟问题会更加严重,Slave机器数量的增加也会使这个问题更加严重。