Redis主從持久化測試

來源:互聯網
上載者:User

1:redis主從環境,均未開啟持久化;
當主執行個體宕機,從執行個體上的資料不受影響;
當主恢複後,主執行個體上的資料將會繼續同步到從執行個體,即原來的值將變為空白值;

[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 info |grep -A 3 'Replication'
# Replication
role:master
connected_slaves:1
slave0:192.168.1.113,6379,online [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123
redis 192.168.1.112:6379> set 1 a
OK
redis 192.168.1.112:6379> get 1
"a" [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 shutdown [root@server12 ~]# tail -f /var/log/messages
Dec  3 15:27:34 server12 redis[32151]: Connecting to MASTER...
Dec  3 15:27:34 server12 redis[32151]: MASTER <-> SLAVE sync started
Dec  3 15:27:34 server12 redis[32151]: Error condition on socket for SYNC: Connection refused [root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123
redis 192.168.1.113:6379> get 1
"a" [root@server11 ~]# /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123
redis 192.168.1.112:6379> get 1
(nil)
redis 192.168.1.112:6379> exit [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123
redis 192.168.1.113:6379> get 1
(nil)
2:redis主從環境,從執行個體開啟快照持久化
當主執行個體宕機,從執行個體上的資料不受影響;
當主恢複後,主執行個體上的資料將會繼續同步到從執行個體,即原來的值將變為空白值;
 [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 
redis 192.168.1.112:6379> get 1
(nil)
redis 192.168.1.112:6379> set 1 a
OK
redis 192.168.1.112:6379> set 2 b
OK
redis 192.168.1.112:6379> exit [root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 shutdown [root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 
redis 192.168.1.113:6379> get 1
"a"
redis 192.168.1.113:6379> get 2
"b"
redis 192.168.1.113:6379> exit
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 
redis 192.168.1.112:6379> get 1
(nil)
redis 192.168.1.112:6379> get 2
(nil)
redis 192.168.1.112:6379> exit [root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 
redis 192.168.1.113:6379> get 1
(nil)
redis 192.168.1.113:6379> get 2
(nil)
redis 192.168.1.113:6379> exit
   3:推進一層,當主,從執行個體均宕機的情況下會如何呢? 
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 
redis 192.168.1.112:6379> set 1 a
OK
redis 192.168.1.112:6379> set 2 b
OK
redis 192.168.1.112:6379> set 3 c
OK
redis 192.168.1.112:6379> exit
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 
redis 192.168.1.113:6379> get 1
"a"
redis 192.168.1.113:6379> get 2
"b"
redis 192.168.1.113:6379> get 3
"c"
redis 192.168.1.113:6379> exit

這次先關閉從執行個體,再關閉主執行個體!啟動則先啟動從執行個體,測試資料;再啟動主執行個體,再測試資料!
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 shutdown
[root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 shutdown
[root@server12 ~]# /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf

[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 
redis 192.168.1.113:6379> get 1
"a"
redis 192.168.1.113:6379> get 2
"b"
redis 192.168.1.113:6379> get 3
"c"

[root@server11 ~]# /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 
redis 192.168.1.112:6379> get 1
(nil)
redis 192.168.1.112:6379> get 2
(nil)
redis 192.168.1.112:6379> get 3
(nil)
redis 192.168.1.112:6379> exit

[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 
redis 192.168.1.113:6379> get 1
(nil)
redis 192.168.1.113:6379> get 2
(nil)
redis 192.168.1.113:6379> get 3
(nil)
redis 192.168.1.113:6379> exit

實踐證明,在redis主從讀寫分離條件下,快照持久化只有開在主執行個體側才可以保證資料可以跨越執行個體重啟!

[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 
redis 192.168.1.112:6379> set 1 a
OK
redis 192.168.1.112:6379> set 2 b
OK
redis 192.168.1.112:6379> set 3 c
OK
redis 192.168.1.112:6379> set 4 d
OK
redis 192.168.1.112:6379> exit
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 
redis 192.168.1.113:6379> get 4
"d"
redis 192.168.1.113:6379> exit

[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123 shutdown
[root@server12 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123 shutdown
[root@server11 ~]# /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf
[root@server12 ~]# /usr/local/redis2/bin/redis-server /usr/local/redis2/etc/redis.conf

[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.112 -a 123  get 4
"d"
[root@server11 ~]# /usr/local/redis2/bin/redis-cli -h 192.168.1.113 -a 123  get 4
"d"

本文出自 “斬月” 部落格,謝絕轉載!

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.