redis主从复制

Posted 从入门到放弃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis主从复制相关的知识,希望对你有一定的参考价值。

配置从机命令 : slaveof 127.0.0.1 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> 

 

一主两从

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=1133,lag=0
slave1:ip=127.0.0.1,port=6381,state=online,offset=1133,lag=0
master_repl_offset:1133
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1132
127.0.0.1:6379>

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:6
master_sync_in_progress:0
slave_repl_offset:1175
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:407
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:406
127.0.0.1:6380>


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:3
master_sync_in_progress:0
slave_repl_offset:1203
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>

 

 

薪火相传

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=797,lag=1
master_repl_offset:797
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:796

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:9
master_sync_in_progress:0
slave_repl_offset:797
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=29,lag=1
master_repl_offset:29
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:28
127.0.0.1:6380>

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:1
master_sync_in_progress:0
slave_repl_offset:15
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> 

 

反客为主

127.0.0.1:6379> shutdown
not connected> exit
[root@hadoop100 myredis]# redis-server /myredis/redis6379.conf
[root@hadoop100 myredis]# 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> 

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:1303
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1302
127.0.0.1:6380> set k12 v12
OK
127.0.0.1:6380> 


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:2
master_sync_in_progress:0
slave_repl_offset:1539
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> 

 

[root@hadoop100 myredis]# ll
总用量 284
-rw-r--r--. 1 root root 14817 3月  14 18:36 6379.log
-rw-r--r--. 1 root root 25084 3月  14 18:36 6380.log
-rw-r--r--. 1 root root 38364 3月  14 18:36 6381.log
-rw-r--r--. 1 root root    68 3月  14 18:36 dump6379.rdb
-rw-r--r--. 1 root root    68 3月  14 18:36 dump6380.rdb
-rw-r--r--. 1 root root    68 3月  14 18:36 dump6381.rdb
-rw-r--r--. 1 root root 41420 3月  14 16:48 redis6379.conf
-rw-r--r--. 1 root root 41420 3月  14 16:49 redis6380.conf
-rw-r--r--. 1 root root 41420 3月  14 16:50 redis6381.conf
-rw-r--r--. 1 root root 41405 3月  14 11:49 redis_aof.conf
-rw-r--r--. 1 root root    43 3月  14 18:43 sentinel.conf

[root@hadoop100 myredis]# cat sentinel.conf 
sentinel monitor host6379 127.0.0.1 6379 1
[root@hadoop100 myredis]#

[root@hadoop100 bin]# ll
总用量 15464
-rw-r--r--. 1 root root 281 3月 14 13:20 appendonly.aof
-rw-r--r--. 1 root root 18 3月 14 17:07 dump.rdb
-rwxr-xr-x. 1 root root 4589155 3月 6 19:30 redis-benchmark
-rwxr-xr-x. 1 root root 22217 3月 6 19:30 redis-check-aof
-rwxr-xr-x. 1 root root 45435 3月 6 19:30 redis-check-dump
-rwxr-xr-x. 1 root root 4693114 3月 6 19:30 redis-cli
lrwxrwxrwx. 1 root root 12 3月 6 19:30 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 6466389 3月 6 19:30 redis-server
[root@hadoop100 bin]# redis-sentinel /myredis/sentinel.conf    //启动哨兵模式

哨兵模式

127.0.0.1:6379> shutdown
not connected> exit
[root@hadoop100 myredis]# redis-server /myredis/redis6379.conf
[root@hadoop100 myredis]# redis-cli -p 6379
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:2217
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> 

127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:570
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:2
repl_backlog_histlen:47596
127.0.0.1:6380> 

127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=836,lag=0
master_repl_offset:836
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:835
127.0.0.1:6381> 

 

以上是关于redis主从复制的主要内容,如果未能解决你的问题,请参考以下文章

Spring之redis主从复制(非哨兵模式)

redis实战_04_yucong_主从复制

详解Redis 主从复制及主从复制原理

redis主从复制最好采用哪种结构

redis高可用之主从复制,哨兵,集群

Redis(主从复制哨兵模式集群)概述及部署