首页 > 代码库 > Redis的复制(Master/Slave)

Redis的复制(Master/Slave)

是什么 :

  也就是我们所说的主从复制,主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主

能干嘛:

  读写分离,容灾恢复

怎么玩:

  1.配从(库)不配主(库)

  2.从库配置:slaveof 主库IP 主库端口

    每次与master断开之后,都需要重新连接,除非你配置进redis.conf文件

    Info replication

  3.修改配置文件细节操作

    拷贝多个redis.conf文件

    开启daemonize yes

    Pid文件名字

    指定端口

    Log文件名字

    Dump.rdb名字  

  4.常用3招:

    一主二仆

    薪火相传

    反客为主

 

在同一台机器的不同端口演示,类似于多台机器

拷贝多份配置文件

[root@node1 myredis]# cp redis.conf redis6379.conf
[root@node1 myredis]# cp redis.conf redis6380.conf
[root@node1 myredis]# cp redis.conf redis6381.conf
[root@node1 myredis]#

 

分别修改对应的配置文件

[root@node1 myredis]# vim redis6379.conf 
[root@node1 myredis]# vim redis6380.conf 
[root@node1 myredis]# vim redis6381.conf 
[root@node1 myredis]# 

 

info replication

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> 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> 

 

 

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> 

 

 

一仆二主:

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> keys *
1) "k3"
2) "k2"
3) "k1"

 

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>

 

127.0.0.1:6381> slaveof 127.0.0.1 6379
OK
127.0.0.1:6381> get k1
"v1"
127.0.0.1:6381>

 

再次输入info replication

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=291,lag=1
slave1:ip=127.0.0.1,port=6381,state=online,offset=291,lag=0
master_repl_offset:291
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:290
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:9
master_sync_in_progress:0
slave_repl_offset:333
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>

 

读写分离:

slave节点不能写入

127.0.0.1:6380> set k6 v6
(error) READONLY You can‘t write against a read only slave.
127.0.0.1:6380> 

 

 

主机挂掉时:

127.0.0.1:6379> SHUTDOWN
not connected> exit
You have new mail in /var/spool/mail/root
[root@node1 ~]#

slave节点状态:

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:753
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>

主机修复后:

[root@node1 ~]# redis-server /myredis/redis6379.conf
[root@node1 ~]# redis-cli -p 6379
127.0.0.1:6379> set k7 v7
OK
127.0.0.1:6379>

 

slave节点:

127.0.0.1:6380> get k7
"v7"
127.0.0.1:6380>

在当前这种状体下,主机挂掉后从机不会改变状态,会原地待命

从机down掉之后会需要与主机从新连接,除非写进conf配置文件

127.0.0.1:6380> slaveof 127.0.0.1 6379
OK
127.0.0.1:6380> get k7
"v7"
127.0.0.1:6380>

 

 

薪火相传:

配置6381的主节点为6380

127.0.0.1:6381> SLAVEOF 127.0.0.1 6380
OK
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=1957,lag=1
master_repl_offset:1957
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1956
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:4
master_sync_in_progress:0
slave_repl_offset:2027
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=71,lag=1
master_repl_offset:71
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:70
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:10
master_sync_in_progress:0
slave_repl_offset:99
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 no one:使当前数据库停止与其他数据库的同步,转成主数据库

127.0.0.1:6380> SLAVEOF no one
OK
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=267,lag=0
master_repl_offset:267
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:266
127.0.0.1:6380>

 

复制原理:

  Slave启动成功连接到master后会发送一个sync命令

  Master接到命令启动后台的存盘进程,同时收集所有接收到的用于修改数据集命令,在后台进程执行完毕后,master将传送整个数据文件到slave,以完成一次完全同步

  全量复制:slave服务在接收到数据库文件数据后,将其存盘并加载到内存中。

  增量复制:Master继续将新的所有收集到的修改命令依次传给slave,完成同步,但是只要是重新连接master,一次完全同步(全量复制)将被自动执行

 

哨兵模式(sentinel):

  是什么:

    反客为主的自动版,能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库。

  怎么玩:

    调整结构,6379带着80,81

    

127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6380> info repication
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:7
master_sync_in_progress:0
slave_repl_offset:3833
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:225
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:224
127.0.0.1:6380> 
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:5
master_sync_in_progress:0
slave_repl_offset:3861
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> 

    自定义的/myredis目录下新建sentinel.conf文件,名字觉不能错

      [root@node1 myredis]# touch sentinel.conf

      sentinel.conf 文件的内容:(末尾的1表示谁的票数多余1票,谁就是新的领导)

      sentinel monitor host6379 127.0.0.1 6379 1  

    启动哨兵:

      使用 redis-sentinel /myredis/sentinel.conf 启动

      

[root@node1 myredis]# redis-sentinel /myredis/sentinel.conf 
9871:X 30 Nov 09:22:52.073 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ‘‘-._                                             
      _.-``    `.  `_.  ‘‘-._           Redis 3.0.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ‘‘-._                                   
 (    ‘      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 26379
 |    `-._   `._    /     _.-‘    |     PID: 9871
  `-._    `-._  `-./  _.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |           http://redis.io        
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
 |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
 |    `-._`-._        _.-‘_.-‘    |                                  
  `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
      `-._    `-.__.-‘    _.-‘                                       
          `-._        _.-‘                                           
              `-.__.-‘                                               

9871:X 30 Nov 09:22:52.077 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
9871:X 30 Nov 09:22:52.077 # Sentinel runid is 07858ebd94d326270f4a56e0501afefe82e444b1
9871:X 30 Nov 09:22:52.079 # +monitor master host6379 127.0.0.1 6379 quorum 1
9871:X 30 Nov 09:22:53.077 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
9871:X 30 Nov 09:22:53.091 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379

 

 

    原有的master挂了:

      哨兵的监视窗口增加新的内容:

9895:X 30 Nov 09:42:54.522 # +sdown master host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:54.522 # +odown master host6379 127.0.0.1 6379 #quorum 1/1
9895:X 30 Nov 09:42:54.522 # +new-epoch 1
9895:X 30 Nov 09:42:54.522 # +try-failover master host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:54.648 # +vote-for-leader ccc2dc958a8ae1e16294b81567126ec4031bf192 1
9895:X 30 Nov 09:42:54.648 # +elected-leader master host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:54.648 # +failover-state-select-slave master host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:54.725 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:54.725 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:54.778 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:55.704 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:55.704 # +failover-state-reconf-slaves master host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:55.752 * +slave-reconf-sent slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:56.778 * +slave-reconf-inprog slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:56.778 * +slave-reconf-done slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:56.877 # +failover-end master host6379 127.0.0.1 6379
9895:X 30 Nov 09:42:56.877 # +switch-master host6379 127.0.0.1 6379 127.0.0.1 6380
9895:X 30 Nov 09:42:56.878 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6380
9895:X 30 Nov 09:42:56.878 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380

    6380变为了新的master

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

    此时6381的状态

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:125992
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> 

 

 

    问题:如果之前的master重启回来,会不会双master冲突

    启动挂掉的6379后

    哨兵监控打印:

9895:X 30 Nov 09:43:26.931 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380
9895:X 30 Nov 09:45:51.015 # -sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380
9895:X 30 Nov 09:46:00.994 * +convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380

 

    启动后,6379的状态:

    成为了当前master的salve节点

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:132805
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> keys *
1) "k7"
127.0.0.1:6379> 

 

 

 

复制的缺点:

复制延时:由于所有的写操作都是闲在Master上操作,然后同步更新到salve上,所以从Master同步到Slave机器有一定的延迟,当系统很繁忙的时候,延迟问题会更加严重,Slave机器数量的增加也会使这个问题更加严重。

 

Redis的复制(Master/Slave)