Redis 主從配置

百聯達發表於2014-10-04
一:redis 主從複製過程

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

二:如何配置
配置slave 伺服器很簡單,只需要在slave 的配置檔案中加入如下配置
slaveof 192.168.1.1 6379 #指定master 的ip 和埠
下面我們做一個實驗來演示如何搭建一個主從環境:
# slaveof
slaveof localhost 6379
我們在一臺機器上啟動主庫(埠6379),從庫(埠6378)
啟動後主庫控制檯日誌如下:
[root@localhost redis-2.2.12]# src/redis-server redis.conf
[7064] 09 Aug 20:13:12 * Server started, Redis version 2.2.12
[7064] 09 Aug 20:13:12 # WARNING overcommit_memory is set to 0! Background save may fail under
low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and
then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[7064] 09 Aug 20:13:12 * The server is now ready to accept connections on port 6379
[7064] 09 Aug 20:13:13 - 0 clients connected (0 slaves), 539512 bytes in use
[7064] 09 Aug 20:13:18 - 0 clients connected (0 slaves), 539512 bytes in use
[7064] 09 Aug 20:13:20 - Accepted 127.0.0.1:37789
[7064] 09 Aug 20:13:20 * Slave ask for synchronization
[7064] 09 Aug 20:13:20 * Starting BGSAVE for SYNC
[7064] 09 Aug 20:13:20 * Background saving started by pid 7067
[7067] 09 Aug 20:13:20 * DB saved on disk
[7064] 09 Aug 20:13:20 * Background saving terminated with success
[7064] 09 Aug 20:13:20 * Synchronization with slave succeeded
[7064] 09 Aug 20:13:23 - 0 clients connected (1 slaves), 547380 bytes in use
啟動後從庫控制檯日誌如下:
[root@localhost redis-2.2.12]# src/redis-server redis.slave
[7066] 09 Aug 20:13:20 * Server started, Redis version 2.2.12
[7066] 09 Aug 20:13:20 # WARNING overcommit_memory is set to 0! Background save may fail under
low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and
then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[7066] 09 Aug 20:13:20 * The server is now ready to accept connections on port 6378
[7066] 09 Aug 20:13:20 - 0 clients connected (0 slaves), 539548 bytes in use
[7066] 09 Aug 20:13:20 * Connecting to MASTER...

三:redis 主從複製特點:
(1)、master 可以擁有多個slave
(2)、多個slave 可以連線同一個master 外,還可以連線到其他slave
(3)、主從複製不會阻塞master,在同步資料時,master 可以繼續處理client 請求
(4)、提高系統的伸縮性


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

相關文章