1. 程式人生 > >資料庫Redis主從複製

資料庫Redis主從複製

工作原理

slave向master傳送sync命名---master啟動後臺存檔(收集所有修改資料命令)---完成後臺存檔後,傳送整個資料檔案到slave---slave接受資料檔案後,載入到記憶體,首次完成完全同步---有新的資料產生時,master繼續將新收集到的資料修改命令依次傳遞給slave,完成同步

缺點是網路或者系統繁忙,會產生資料同步延時問題

注:從庫不需要和主庫資料一致,配置完主從複製之後,從庫會自動同步資料並覆蓋自己的資料

       從庫不作額外操作,客戶端只能在從庫上檢視資料,並不能寫入

結構模式

一主一從  一主多從  主從從

配置主從複製

                                             拓  撲  結  構

master伺服器                     複製/同步 

                                slave伺服器

192.168.4.51/24      ------------------------------------------->  192.168.4.52/24

           |                                                                                       |

----------------------------------------------------------------------------------------------

                                                       |

                                           clien 客戶機 192.168.4.50/24

配置從庫   進入redis 執行slaveof  主機IP    埠(這種配置是臨時的,重啟就失效了!)

                 永久配置,修改配置檔案/etc/redis/6379.conf            282 # slaveof <masterip> <masterport>

[[email protected] ~]# redis-cli  -h 192.168.4.52 -p 6352
192.168.4.52:6352> SLAVEOF 192.168.4.51 6351
OK

192.168.4.52:6352> info replication          //檢視主從配置資訊
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
[[email protected] ~]# redis-cli -h 192.168.4.51 -p  6351
192.168.4.51:6351> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.4.52,port=6352,state=online,offset=168,lag=1
master_replid:0c141f6f2be45cc6760898c0be1e2c16fc7f869e
192.168.4.51:6351> set x 123
OK
192.168.4.52:6352> keys *                     //在52上檢視資訊,已經同步
1) "x"
[[email protected] ~]# /etc/init.d/redis_6379 stop
[[email protected] ~]# vim /etc/redis/6379.conf 
########### REPLICATION ###########
 ...
282 slaveof 192.168.4.51  6351
[[email protected] ~]# /etc/init.d/redis_6379 start
[[email protected] ~]# redis-cli -h 192.168.4.52 -p 6352 
192.168.4.52:6352> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up
...

主庫宕機後,可以將從庫設定為主庫

命令列   slaveof no one             配置檔案在註釋掉(永久為主庫)

192.168.4.52:6352> slaveof no one
OK
192.168.4.52:6352> info replication
# Replication
role:master

配置帶驗證的主從配置

配置master主機,設定密碼

192.168.4.51:6351> shutdown
not connected> exit
[[email protected] ~]# vim /etc/redis/6379.conf 
...
############# SECURITY #############
... 
501 requirepass 123456

[[email protected] ~]# vim /etc/init.d/redis_6379 
...
REDISPORT="6351 -h 192.168.4.51 -a 123456"
...

[[email protected] ~]# /etc/init.d/redis_6379 start 
Starting Redis server...
[[email protected]~]# redis-cli -h 192.168.4.51 -p  6351
192.168.4.51:6351> keys *
(error) NOAUTH Authentication required.                     //需輸入密碼才可以檢視
192.168.4.51:6351> auth 123456
OK
192.168.4.51:6351> set y 123

配置 從庫主機,指定主庫IP,設定連結密碼(289 # masterauth <master-password>)

[[email protected] ~]# redis-cli -h 192.168.4.52 -p 6352 
192.168.4.52:6352> INFO replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:down                 //可以看到狀態已經關了
...
192.168.4.52:6352> keys *               //資料也沒有同步
1) "x"

[[email protected] ~]# /etc/init.d/redis_6379  stop
[[email protected] ~]# vim /etc/redis/6379.conf 
...
 289 masterauth 123456
...
[[email protected] ~]# /etc/init.d/redis_6379  start
[[email protected] ~]# redis-cli -h 192.168.4.52 -p 6352 
192.168.4.52:6352> INFO replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up
...
192.168.4.52:6352> keys *
1) "x"
2) "y"

配置一主兩從 

在上面的基礎上新增53為51的從庫

[[email protected] ~]# /etc/init.d/redis_6379 stop 
[[email protected] ~]# vim /etc/redis/6379.conf 
...
############ REPLICATION ############
...
282 slaveof 192.168.4.51 6351
...
289 masterauth  123456
[[email protected] ~]# /etc/init.d/redis_6379 start 
[[email protected] ~]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> info replication
# Replication
role:slave
master_host:192.168.4.51
master_port:6351
master_link_status:up

在51上驗證從庫,還有資料同步

[[email protected] ~]# redis-cli -h 192.168.4.51 -p  6351
192.168.4.51:6351> auth 123456
OK
192.168.4.51:6351> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.4.52,port=6352,state=online,offset=1204,lag=0
slave1:ip=192.168.4.53,port=6353,state=online,offset=1204,lag=0
...
192.168.4.51:6351> set z 123
OK

在53上檢視資料是否同步

192.168.4.53:6353> keys *
1) "y"
2) "z"
3) "x"

客戶端連結主庫51,寫入資料,在53上再次檢視

[[email protected] ~]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
192.168.4.51:6351> keys *
1) "y"
2) "x"
3) "z"
192.168.4.51:6351> set a 001
OK


[[email protected] ~]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> keys *
1) "y"
2) "a"
3) "z"
4) "x"

配置主從從結構

先將剛才用的從庫53還原為獨立的主庫

[[email protected] ~]# /etc/init.d/redis_6379 stop
[[email protected] ~]# vim /etc/redis/6379.conf 
############## REPLICATION #############
...
282 #slaveof 192.168.4.51 6351                //註釋
...
289 #masterauth  123456                       //註釋
[[email protected] ~]# /etc/init.d/redis_6379 start
[[email protected] ~]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> INFO replication
# Replication
role:master
192.168.4.53:6353> FLUSHALL
OK
192.168.4.53:6353> keys *
(empty list or set)

修改配置檔案,設定為52的從庫

[[email protected] ~]# /etc/init.d/redis_6379 stop
[[email protected] ~]# vim /etc/redis/6379.conf 
...
slaveof 192.168.4.52 6352
[[email protected] ~]# /etc/init.d/redis_6379 start
[[email protected] ~]# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> info replication
# Replication
role:slave
master_host:192.168.4.52
master_port:6352
master_link_status:up
192.168.4.53:6353> keys *
1) "x"
2) "a"
3) "y"
4) "z"

客戶端連結51,並寫入資料,驗證從庫52和53

[[email protected] ~]# redis-cli -h 192.168.4.51 -p 6351 -a 123456
192.168.4.51:6351> set b 002
OK
192.168.4.52:6352> keys *
1) "y"
2) "z"
3) "a"
4) "b"
5) "x"


192.168.4.53:6353> keys *
1) "x"
2) "a"
3) "b"
4) "z"
5) "y"

哨兵(sentinel)模式 ----監控主庫

所用結構(主從從51 52 53)

編寫哨兵服務的配置檔案(在52上設定)

sentinel monitor <master-name> <ip> <redis-port> <quorum>

sentinel auth-pass <master-name> <password>

[[email protected] ~]# cd redis/redis-4.0.8/
[[email protected] redis-4.0.8]# cp sentinel.conf   /etc/
[[email protected] redis-4.0.8]# vim /etc/sentinel.conf
 ...
 15 bind 0.0.0.0
 21 port 26379                               //預設埠
 ...
 52 # sentinel monitor <master-name> <ip> <redis-port> <quorum>   //在下面新增
   sentinel monitor  redis51 192.168.4.51  6351 1               
 ...
 71 # sentinel auth-pass <master-name> <password>                 //在下面新增
   sentinel auth-pass  redis51  123456
 ...


啟動哨兵服務

[[email protected] ~]# redis-sentinel  /etc/sentinel.conf 
...
          _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 3841
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

3841:X 24 Dec 11:55:57.666 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3841:X 24 Dec 11:55:57.693 # Sentinel ID is 53ddd54e5bbba74e70927a758807995453a076ec
3841:X 24 Dec 11:55:57.694 # +monitor master mymaster 127.0.0.1 6379 quorum 2
3841:X 24 Dec 11:55:57.694 # +monitor master redis51 192.168.4.51 6351 quorum 1
3841:X 24 Dec 11:55:57.695 * +slave slave 192.168.4.52:6352 192.168.4.52 6352 @ redis51 192.168.4.51 6351
3841:X 24 Dec 11:56:27.674 # +sdown master mymaster 127.0.0.1 6379
[[email protected] ~]# redis-sentinel  /etc/sentinel.conf  &   //可以放到後臺

測試(51宕機,52自動成為主庫)

[[email protected] ~]# redis-cli -h 192.168.4.51 -p  6351
192.168.4.51:6351> auth 123456
192.168.4.51:6351> shutdown
not connected> 
[[email protected] ~]# redis-cli -h 192.168.4.52  -p 6352
192.168.4.52:6352> INFO replication
# Replication
role:master
connected_slaves:1

[[email protected] ~]#  vim /etc/sentinel.conf                 //檔案的內容已經相應改變
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
sentinel myid 53ddd54e5bbba74e70927a758807995453a076ec
...
# sentinel auth-pass <master-name> <password>
sentinel config-epoch mymaster 0

[[email protected] ~]# redis-sentinel  /etc/sentinel.conf 
...
3841:X 24 Dec 12:00:19.914 * +slave slave 192.168.4.53:6353 192.168.4.53 6353 @ redis51 192.168.4.52 6352
3841:X 24 Dec 12:00:49.023 # +sdown slave 192.168.4.51:6351 192.168.4.51 6351 @ redis51 192.168.4.52 6352

啟動51,自動成為52的從庫

[[email protected] ~]# /etc/init.d/redis_6379 start

[[email protected] ~]# redis-cli -h 192.168.4.52  -p 6352
192.168.4.52:6352> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.4.53,port=6353,state=online,offset=573865,lag=1
slave1:ip=192.168.4.51,port=6351,state=online,offset=573865,lag=0
...