redis主從複製例子

rainbowbridg發表於2012-02-20

ref: http://who0168.blog.51cto.com/253401/628347


這個例子很好

[@more@]

原文地址:
redis安裝配置參見:ubuntu下redis安裝配置
一.redis主從複製特點:
1.master可以擁有多個slave
2.多個slave可以連線同一個master外,還可以連線到其他slave
3.主從複製不會阻塞master,在同步資料時,master可以繼續處理client請求
4.提高系統的伸縮性
5.可以在master禁用資料持久化,註釋掉master配置檔案中的所有save配置,只需在slave上配置資料持久化

二.redis主從複製過程:
當配置好slave後,slave與master建立連線,然後傳送sync命令。無論是第一次連線還是重新連線,master都會啟動一個後臺程式,將 資料庫快照儲存到檔案中,同時master主程式會開始收集新的寫命令並快取。後臺程式完成寫檔案後,master就傳送檔案給slave,slave將 檔案儲存到硬碟上,再載入到記憶體中,接著master就會把快取的命令轉發給slave,後續master將收到的寫命令傳送給slave。如果 master同時收到多個slave發來的同步連線命令,master只會啟動一個程式來寫資料庫映象,然後傳送給所有的slave。

三.redis主從配置:
配置具體引數含義參見:
【master】
daemonize yes
pidfile /var/run/redis.pid
port 6379
timeout 300
loglevel verbose
logfile /usr/local/redis-2.2.12/var/log/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis-2.2.12/var/data
requirepass redis
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
slowlog-log-slower-than 10000
slowlog-max-len 1024
vm-enabled no
vm-swap-file /tmp/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
hash-max-zipmap-entries 512
hash-max-zipmap-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
activerehashing yes

【slave】
daemonize yes
pidfile /var/run/redis.pid
port 6379
timeout 300
loglevel verbose
logfile /usr/local/redis-2.2.12/var/log/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /usr/local/redis-2.2.12/var/data
appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
slowlog-log-slower-than 10000
slowlog-max-len 1024
vm-enabled no
vm-swap-file /tmp/redis.swap
vm-max-memory 0
vm-page-size 32
vm-pages 134217728
vm-max-threads 4
hash-max-zipmap-entries 512
hash-max-zipmap-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
activerehashing yes
slaveof 192.168.1.189 6379
masterauth redis

四.redis複製測試
檢視master端日誌:
[8930] 31 Jul 19:16:09 - Accepted 192.168.1.136:54774
[8930] 31 Jul 19:16:09 * Slave ask for synchronization
[8930] 31 Jul 19:16:09 * Starting BGSAVE for SYNC
[8930] 31 Jul 19:16:09 * Background saving started by pid 10782
[10782] 31 Jul 19:16:09 * DB saved on disk
[8930] 31 Jul 19:16:09 * Background saving terminated with success
[8930] 31 Jul 19:16:09 * Synchronization with slave succeeded
[8930] 31 Jul 19:16:14 - DB 0: 1 keys (0 volatile) in 4 slots HT.
[8930] 31 Jul 19:16:14 - 1 clients connected (1 slaves), 807320 bytes in use

檢視slave端日誌:
[24398] 01 Aug 10:16:10 * Connecting to MASTER...
[24398] 01 Aug 10:16:10 * MASTER SLAVE sync started: SYNC sent
[24398] 01 Aug 10:16:10 * MASTER SLAVE sync: receiving 25 bytes from master
[24398] 01 Aug 10:16:10 * MASTER SLAVE sync: Loading DB in memory
[24398] 01 Aug 10:16:10 * MASTER SLAVE sync: Finished with success
[24398] 01 Aug 10:16:15 - DB 0: 1 keys (0 volatile) in 4 slots HT.
[24398] 01 Aug 10:16:15 - 1 clients connected (0 slaves), 798960 bytes in use

master端操作:
redis 127.0.0.1:6379> set k_m master
OK
slave端操作:
redis 127.0.0.1:6379> get k_m
"master"

原文地址:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7916042/viewspace-1057407/,如需轉載,請註明出處,否則將追究法律責任。

相關文章