標籤:redis 主從
redis與mysql的主從很相似,而且比mysql設定主從更加簡單:
分析:
1、redis主從複製特點:
(1)、master可以擁有多個slave
(2)、多個slave可以串連同一個master外,還可以串連到其他slave
(3)、主從複製不會阻塞master,在同步資料時,master可以繼續處理client請求
(4)、提高系統的伸縮性
2、redis主從複製過程:
當配置好slave後,slave與master建立串連,然後發送sync命令。無論是第一次串連還是重新串連,master都會啟動一個後台進程,將資料庫快照集儲存到檔案中,同時master主進程會開始收集新的寫命令並緩衝。後台進程完成寫檔案後,master就傳送檔案給slave,slave將檔案儲存到硬碟上,再載入到記憶體中,接著master就會把緩衝的命令轉寄給slave,後續master將收到的寫命令發送給slave。如果master同時收到多個slave發來的同步串連命令,master只會啟動一個進程來寫資料庫鏡像,然後發送給所有的slave。
3、如何配置
配置slave伺服器很簡單,只需要在slave的設定檔中加入如下配置
slaveof 192.168.1.1 6379 #指定master的ip和連接埠
下面我們做一個實驗來示範如何搭建一個主從環境:
# slaveof <masterip> <masterport> slaveof localhost 6379
我們在一台機器上啟動主庫(連接埠6379),從庫(連接埠6378)
啟動後主庫控制台日誌如下:
[[email protected] 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
啟動後從庫控制台日誌如下:
[[email protected] 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... [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync started: SYNC sent [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: receiving 10 bytes from master [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Loading DB in memory [7066] 09 Aug 20:13:20 * MASTER <-> SLAVE sync: Finished with success [7068] 09 Aug 20:13:20 * SYNC append only file rewrite performed [7066] 09 Aug 20:13:20 * Background append only file rewriting started by pid 7068 [7066] 09 Aug 20:13:21 * Background append only file rewriting terminated with success [7066] 09 Aug 20:13:21 * Parent diff flushed into the new append log file with success (0 bytes) [7066] 09 Aug 20:13:21 * Append only file successfully rewritten. [7066] 09 Aug 20:13:21 * The new append only file was selected for future appends. [7066] 09 Aug 20:13:25 - 1 clients connected (0 slaves), 547396 bytes in use
註:日誌的檢查很重要
驗證資料的同步性:
1、主庫上設定一對索引值對
redis 127.0.0.1:6379> set name HongWan OK redis 127.0.0.1:6379>
2、從庫上取一下這個鍵
redis 127.0.0.1:6378> get name "HongWan" redis 127.0.0.1:6378>
說明主從是同步正常的.
調用info這個命令就可以得到主從的資訊:
redis 127.0.0.1:6378> info . . . role:slave master_host:localhost master_port:6379 master_link_status:up master_last_io_seconds_ago:10 master_sync_in_progress:0 db0:keys=1,expires=0 redis 127.0.0.1:6378>
裡面有一個角色標識,來判斷是主庫還是從庫,對於本例是一個從庫,同時還有一個master_link_status用於標明主從是否非同步,如果此值=up,說明同步正常;如果此值=down,說明同步非同步;
db0:keys=1,expires=0, 用於說明資料庫有幾個key,以及到期key的數量。
本文出自 “郭煒君工作學習記錄” 部落格,請務必保留此出處http://visonguo.blog.51cto.com/510379/1539445