redis之master.slave主從複製

簡介

主機數據更新後根據配置和策略,自動同步到備機的master/slave機制,master以寫爲主,slave以讀爲主

從庫配置

配置從庫,不配主庫

配置從庫:
    格式: slaveof 主庫ip 主庫port
    注意: 每次與master斷開以後,都須要從新鏈接,除非配置到redis.conf文件
    
配置文件細節:

主從同步一--一主多從(同一臺機器上同一個redis運行在三個port上)

1.複製redis配置文件三份

[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# mkdir test-master-slave

[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# cp redis.conf test-master-slave/redis6379.conf
[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# cp redis.conf test-master-slave/redis6380.conf
[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# cp redis.conf test-master-slave/redis6381.conf

修改另外兩份配置文件
    vim redis6379.conf
        daemonize yes
        port 6379
        pidfile /var/run/redis_6379.pid
        logfile "6379.log"
        dbfilename dump6379.rdb
    vim redis6380.conf
        daemonize yes
        port 6380
        pidfile /var/run/redis_6380.pid
        logfile "6380.log"
        dbfilename dump6380.rdb

    vim redis6381.conf
        daemonize yes
        port 6381
        pidfile /var/run/redis_6381.pid
        logfile "6381.log"
        dbfilename dump6381.rdb

2.啓動三個redis

[root@izm5e2q95pbpe1hh0kkwoiz test-master-slave]# cd /alidata/redis-5.0.3/bin
[root@izm5e2q95pbpe1hh0kkwoiz bin]# pwd
/alidata/redis-5.0.3/bin
[root@izm5e2q95pbpe1hh0kkwoiz bin]# ps aux | grep redis
root     20603  0.1  0.1 156392  2284 ?        Ssl   2018 681:54 redis-server *:8686
root     21288  0.0  0.0 112680   984 pts/0    R+   10:58   0:00 grep --color=auto redis
[root@izm5e2q95pbpe1hh0kkwoiz bin]# redis-server ../test-master-slave/redis6380.conf 
[root@izm5e2q95pbpe1hh0kkwoiz bin]# ps aux | grep redis
root     20603  0.1  0.1 156392  2284 ?        Ssl   2018 681:54 redis-server *:8686
root     23628  0.0  0.1 153832  2324 ?        Ssl  10:58   0:00 redis-server 127.0.0.1:6380
root     24492  0.0  0.0 112680   980 pts/0    R+   10:58   0:00 grep --color=auto redis
[root@izm5e2q95pbpe1hh0kkwoiz bin]# redis-server ../test-master-slave/redis6381.conf 
[root@izm5e2q95pbpe1hh0kkwoiz bin]# ps aux | grep redis
root     20603  0.1  0.1 156392  2284 ?        Ssl   2018 681:54 redis-server *:8686
root     23628  0.0  0.1 153832  2324 ?        Ssl  10:58   0:00 redis-server 127.0.0.1:6380
root     27142  0.0  0.1 153832  2304 ?        Ssl  10:58   0:00 redis-server 127.0.0.1:6381
root     28470  0.0  0.0 112680   984 pts/0    R+   10:58   0:00 grep --color=auto redis
[root@izm5e2q95pbpe1hh0kkwoiz bin]# redis-server ../test-master-slave/redis6379.conf 
[root@izm5e2q95pbpe1hh0kkwoiz redis-5.0.3]# ps aux | grep redis
root      4705  0.1  0.1 153832  2592 ?        Ssl  11:11   0:00 redis-server 127.0.0.1:6380
root     20603  0.1  0.1 156392  2296 ?        Ssl   2018 681:55 redis-server *:8686
root     22492  0.0  0.1 153832  2296 ?        Ssl  11:14   0:00 redis-server 127.0.0.1:6379
root     23990  0.0  0.0 112680   980 pts/0    R+   11:14   0:00 grep --color=auto redis
root     27142  0.0  0.1 153832  2304 ?        Ssl  10:58   0:00 redis-server 127.0.0.1:6381

3.細節(暫時一主一從)

# 主機器
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:d9ff48134d9b166cb7f48f4f71a54c9eddc8531c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# 尚未配置的從機器
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:0
master_replid:e06824b502f2bc16c101653a8e36ba954cf35a53
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# 配置從redis
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
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:2
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14

# 此時master redis狀態
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=28,lag=0
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28

redis的master跟slave創建主從關係以後,slave redis會從master的redis獲取全部的key

若是master redis down掉,slave redis會繼續等待,master redis運行後,slave redis自動鏈接

若是slave redis down掉以後,slave redis從新啓動,不會自動鏈接master redis,須要從新運行slaveof命令

master redis寫入的key,slave redis不能去修改master已經存在的key,在slave中是readonly的

主從同步二--薪火相傳

簡介

上一個slave能夠是下一個slave的master,slave一樣能夠接受其餘slaves的鏈接和同步請求.
那麼該slave做爲鏈條中下一個的master,能夠有效減輕master的寫壓力

中途變動轉向:會清除以前的數據,從新創建拷貝最新的.

slaveof 新主庫ip 新主庫port

細節

# 主master
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=28,lag=0
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28

# master6379的slave6380
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:3
master_sync_in_progress:0
slave_repl_offset:2142
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=127.0.0.1,port=6381,state=online,offset=2142,lag=1
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2142
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2142

# slave6380的slave6381
127.0.0.1:6381> SLAVEOF 127.0.0.1 6380
OK
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:3
master_sync_in_progress:0
slave_repl_offset:2128
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:df2697f1f530565d1fb683f209283e319faea59f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2128
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2129
repl_backlog_histlen:0

主從複製三--反客爲主

簡介

從庫運行slaveof no one ,使當前數據庫中止與其餘數據庫的同步,轉成主數據庫
其餘的從庫使用"slaveof 新主庫ip 新主庫port",從新綁定最新的主庫

複製原理

slave啓動成功,鏈接到master後會發送一個sync命令

master接到命令啓動後臺的存盤進程,同時手機全部接收到的用於修改數據命令,

在後臺進程執行完畢以後,master將傳送整個數據文件到slave,以完成一次徹底同步

全量複製: 而slave在接收到數據庫文件以後,將其存盤並加載到內存中

增量複製: master繼續將新的全部收集到的修改命令依次傳遞給slave,完成同步

可是隻要是從新鏈接master,一次徹底同步(全量複製)將被自動執行

哨兵模式(sentinel)

參考連接: https://redis.io/topics/sentinel

簡介

反客爲主的自動版,可以後退監控主機是否故障,若是故障了根據投票數自動將從庫轉換爲主庫

如何操做

1.調整結構6379帶着6380,6381
2.自定義/alidata/redis-5.0.3/test-master-slave目錄下新建sentinel.conf文件,名字絕對不能錯
3.配置哨兵
    sentinel monitor 被監控數據庫名字(本身定義) 被監控數據庫ip 被監控數據庫port 1
    上面最後一個數字1,表示主機掛掉後slave投票讓誰接替成爲主機,得票數多少後成爲主機
4.啓動哨兵
    redis-sentinel /alidata/redis-5.0.3/test-master-slave/sentinel.conf
    若是沒有redis-sentinel,則在/alidata/redis-5.0.3/bin目錄下使用以下命令:
    redis-server ../test-master-slave/sentinel.conf --sentinel
    
    [root@izm5e2q95pbpe1hh0kkwoiz bin]# redis-server ../test-master-slave/sentinel.conf --sentinel
    16452:X 10 Jan 2020 15:56:54.511 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    16452:X 10 Jan 2020 15:56:54.511 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=16452, just started
    16452:X 10 Jan 2020 15:56:54.511 # Configuration loaded
                    _._                                                  
               _.-``__ ''-._                                             
          _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
      .-`` .-```.  ```\/    _.,_ ''-._                                   
     (    '      ,       .-`  | `,    )     Running in sentinel mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
     |    `-._   `._    /     _.-'    |     PID: 16452
      `-._    `-._  `-./  _.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |           http://redis.io        
      `-._    `-._`-.__.-'_.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-._`-.__.-'_.-'    _.-'                                   
          `-._    `-.__.-'    _.-'                                       
              `-._        _.-'                                           
                  `-.__.-'                                               
    
    16452:X 10 Jan 2020 15:56:54.516 # Sentinel ID is a7d6715b13d229862bd5082e1f0e7734897a1693
    16452:X 10 Jan 2020 15:56:54.516 # +monitor master host6379 127.0.0.1 6379 quorum 1
    16452:X 10 Jan 2020 15:56:54.517 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:56:54.520 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379

    
5.原有的master掛掉以後,自動從新選擇master,另外的slave從新掛到新的master下,6379掛掉,投票產生新的master爲6381
    16452:X 10 Jan 2020 15:59:43.676 # +odown master host6379 127.0.0.1 6379 #quorum 1/1
    16452:X 10 Jan 2020 15:59:43.676 # +new-epoch 1
    16452:X 10 Jan 2020 15:59:43.676 # +try-failover master host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:43.682 # +vote-for-leader a7d6715b13d229862bd5082e1f0e7734897a1693 1
    16452:X 10 Jan 2020 15:59:43.682 # +elected-leader master host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:43.682 # +failover-state-select-slave master host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:43.745 # +selected-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:43.745 * +failover-state-send-slaveof-noone slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:43.809 * +failover-state-wait-promotion slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:44.588 # +promoted-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:44.588 # +failover-state-reconf-slaves master host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:44.660 * +slave-reconf-sent slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:45.615 * +slave-reconf-inprog slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:45.615 * +slave-reconf-done slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6379
    16452:X 10 Jan 2020 15:59:45.691 # +failover-end master host6379 127.0.0.1 6379
    ## 切換到投票出來的新的master之6381
    16452:X 10 Jan 2020 15:59:45.691 # +switch-master host6379 127.0.0.1 6379 127.0.0.1 6381
    16452:X 10 Jan 2020 15:59:45.691 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
    16452:X 10 Jan 2020 15:59:45.691 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
    16452:X 10 Jan 2020 16:00:15.768 # +sdown slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381

    ## 6381變爲新的master,6380爲slave,只有6380一個slave
    127.0.0.1:6381> info replication
    # Replication
    role:master
    connected_slaves:1
    slave0:ip=127.0.0.1,port=6380,state=online,offset=23490630,lag=0
    master_replid:9772fad6a5ca29661aeb5e07ca72c013347c5679
    master_replid2:df2697f1f530565d1fb683f209283e319faea59f
    master_repl_offset:23490630
    second_repl_offset:23480570
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:22442055
    repl_backlog_histlen:1048576
    
    ## 6380是6381的slave
    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:1
    master_sync_in_progress:0
    slave_repl_offset:23491043
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:9772fad6a5ca29661aeb5e07ca72c013347c5679
    master_replid2:df2697f1f530565d1fb683f209283e319faea59f
    master_repl_offset:23491043
    second_repl_offset:23480570
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:22442468
    repl_backlog_histlen:1048576


6.最早掛掉的master從新運行以後,自動掛載到從新選舉出來的master下面,此時最原先的master變爲slave
    哨兵檢測到後將6379啓動,將6379變爲6381的slave:
        16452:X 10 Jan 2020 16:04:28.076 * +convert-to-slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
    6381下的slave多了6379
        127.0.0.1:6381> info replication
        # Replication
        role:master
        connected_slaves:2
        slave0:ip=127.0.0.1,port=6380,state=online,offset=23501237,lag=2
        slave1:ip=127.0.0.1,port=6379,state=online,offset=23501237,lag=1
        master_replid:9772fad6a5ca29661aeb5e07ca72c013347c5679
        master_replid2:df2697f1f530565d1fb683f209283e319faea59f
        master_repl_offset:23501237
        second_repl_offset:23480570
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:22452662
        repl_backlog_histlen:1048576
    6379的info replication
        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:1
        master_sync_in_progress:0
        slave_repl_offset:23507614
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_replid:9772fad6a5ca29661aeb5e07ca72c013347c5679
        master_replid2:0000000000000000000000000000000000000000
        master_repl_offset:23507614
        second_repl_offset:-1
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:23499724
        repl_backlog_histlen:7891

    
7.假如此時投票選舉出來的master又掛掉了,此時是什麼狀況?
    哨兵檢測到6381掛掉以後的動做
        16452:X 10 Jan 2020 16:07:53.458 # +sdown master host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:53.458 # +odown master host6379 127.0.0.1 6381 #quorum 1/1
        16452:X 10 Jan 2020 16:07:53.458 # +new-epoch 2
        16452:X 10 Jan 2020 16:07:53.458 # +try-failover master host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:53.463 # +vote-for-leader a7d6715b13d229862bd5082e1f0e7734897a1693 2
        16452:X 10 Jan 2020 16:07:53.463 # +elected-leader master host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:53.463 # +failover-state-select-slave master host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:53.523 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:53.523 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:53.581 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:53.643 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:53.643 # +failover-state-reconf-slaves master host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:53.694 * +slave-reconf-sent slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:54.323 * +slave-reconf-inprog slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:55.333 * +slave-reconf-done slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6381
        16452:X 10 Jan 2020 16:07:55.386 # +failover-end master host6379 127.0.0.1 6381
        ## 將master從6381切換爲6380
        16452:X 10 Jan 2020 16:07:55.386 # +switch-master host6379 127.0.0.1 6381 127.0.0.1 6380
        16452:X 10 Jan 2020 16:07:55.386 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ host6379 127.0.0.1 6380
        16452:X 10 Jan 2020 16:07:55.386 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6380
        16452:X 10 Jan 2020 16:08:25.418 # +sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ host6379 127.0.0.1 6380
    6380變爲master,6379變動爲6380的slave:
        127.0.0.1:6380> info replication
        # Replication
        role:master
        connected_slaves:1
        slave0:ip=127.0.0.1,port=6379,state=online,offset=23512243,lag=0
        master_replid:08b69c14d46567b6d0506e84e7c51da49a9661ec
        master_replid2:9772fad6a5ca29661aeb5e07ca72c013347c5679
        master_repl_offset:23512243
        second_repl_offset:23511276
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:22463668
        repl_backlog_histlen:1048576
    6379變爲6380的slave:
        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:23525130
        slave_priority:100
        slave_read_only:1
        connected_slaves:0
        master_replid:08b69c14d46567b6d0506e84e7c51da49a9661ec
        master_replid2:9772fad6a5ca29661aeb5e07ca72c013347c5679
        master_repl_offset:23525130
        second_repl_offset:23511276
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:23499724
        repl_backlog_histlen:25407

複製缺點

因爲全部的寫操做都是如今master上操做,而後同步更新到slave上,因此master通不到slave機器有必定的延遲,
當系統很繁忙的時候,延遲問題會更加嚴重,slave機器數量的增長也會使這個問題更加嚴重
相關文章
相關標籤/搜索