Redis單機多實例——主從複製redis
上一篇講到了redis單機單實例的安裝http://my.oschina.net/xinxingegeya/blog/389155,數據庫
這一篇主要安裝單機多實例,以及主從複製的配置。這就是一個redis的集羣了。vim
[root@121 redis_7000]# pwd /opt/redis-2.8.19/redis_7000 [root@121 redis_7000]# ./src/redis-cli -p 7000 127.0.0.1:7000> ping PONG 127.0.0.1:7000> shutdown not connected> quit
先把7000這個端口關閉。接下來按照redis的官方推薦的方式來安裝redis。服務器
執行如下命令,把redis-server腳本和redis-cli腳本放到bin目錄中架構
[root@121 redis_7000]# cp src/redis-server /usr/local/bin/ [root@121 redis_7000]# cp src/redis-cli /usr/local/bin/
接下來創建目錄以存放redis的配置文件或是redis數據,socket
mkdir /etc/redis ——redis的配置目錄ide
mkdir /var/redis ——redis的存放數據的目錄ui
mkdir /var/redis/log ——redis的日誌文件的存放目錄this
mkdir /var/redis/run ——redis的pid文件的存放目錄spa
mkdir /var/redis/redis_7000 ——redis實例的工做目錄
[root@121 redis_7000]# mkdir /etc/redis ——redis的配置目錄 [root@121 redis_7000]# mkdir /var/redis ——redis的存放數據的目錄 [root@121 redis_7000]# mkdir /var/redis/log ——redis的日誌文件的存放目錄 [root@121 redis_7000]# mkdir /var/redis/run ——redis pid文件的存放目錄 [root@121 redis_7000]# mkdir /var/redis/redis_7000 ——redis實例的工做目錄
而後複製配置文件的模板到/etc/redis/目錄中,
[root@121 redis_7000]# cp redis.conf /etc/redis/7000.conf
修改配置文件,修改的有如下內容,
Set daemonize to yes (by default it is set to no).
Set the pidfile to /var/run/redis_6379.pid (modify the port if needed).
Change the port accordingly. In our example it is not needed as the default port is already 6379.
Set your preferred loglevel.
Set the logfile to /var/log/redis_6379.log
Set the dir to /var/redis/6379 (very important step!)
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
pidfile /var/redis/run/redis_7000.pid
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 7000
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile /var/redis/log/redis_7000.log
# The working directory.
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
# The Append Only File will also be created inside this directory.
# Note that you must specify a directory here, not a file name.
dir /var/redis/redis_7000
而後redis_init_script腳本放到init.d目錄中,
[root@121 redis_7000]# cp utils/redis_init_script /etc/init.d/redis_7000
而後打開腳本,/etc/init.d/redis_7000
[root@121 redis_7000]# vi /etc/init.d/redis_7000
按照上面的配置修改參數以下,
REDISPORT=7000
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/redis/run/redis_${REDISPORT}.pid
CONF=/etc/redis/${REDISPORT}.conf
下面開始啓動redis,配置的端口爲7000,以下,
[root@121 redis_7000]# /etc/init.d/redis_7000 start Starting Redis server... [root@121 redis_7000]# ps -ef | grep redis root 27449 1 0 15:26 ? 00:00:00 /usr/local/bin/redis-server *:7000 root 27460 24088 0 15:26 pts/0 00:00:00 grep redis [root@121 redis_7000]# redis-cli -p 7000 127.0.0.1:7000> ping PONG 127.0.0.1:7000>
啓動成功了。
如何啓動多個實例,也就是說一個實例要對應着一個配置文件。若是使用init.d下的腳本的話也要一個實例對應一個腳本。(也能夠寫成一個啓動腳本)
以下,複製腳本,新建目錄。先新建多個實例的目錄。
[root@121 redis]# pwd /var/redis [root@121 redis]# mkdir redis_7001 redis_7002 [root@121 redis]# mkdir redis_8000 redis_8001 redis_8002 [root@121 redis]# mkdir redis_9000 redis_9001 redis_9002 [root@121 redis]# ls -l total 44 drwxr-xr-x. 2 root root 4096 Mar 20 14:38 log drwxr-xr-x. 2 root root 4096 Mar 20 15:22 redis_7000 drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_7001 drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_7002 drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_8000 drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_8001 drwxr-xr-x. 2 root root 4096 Mar 20 15:31 redis_8002 drwxr-xr-x. 2 root root 4096 Mar 20 15:32 redis_9000 drwxr-xr-x. 2 root root 4096 Mar 20 15:32 redis_9001 drwxr-xr-x. 2 root root 4096 Mar 20 15:32 redis_9002 drwxr-xr-x. 2 root root 4096 Mar 20 15:26 run
複製配置文件,
[root@121 redis]# pwd /etc/redis [root@121 redis]# ls 7000.conf [root@121 redis]# cp 7000.conf 7001.conf [root@121 redis]# cp 7000.conf 7002.conf [root@121 redis]# cp 7000.conf 8000.conf [root@121 redis]# cp 7000.conf 8001.conf [root@121 redis]# cp 7000.conf 8002.conf [root@121 redis]# cp 7000.conf 9000.conf [root@121 redis]# cp 7000.conf 9001.conf [root@121 redis]# cp 7000.conf 9002.conf [root@121 redis]# ls -l total 324 -rw-r--r--. 1 root root 36202 Mar 20 15:12 7000.conf -rw-r--r--. 1 root root 36202 Mar 20 15:34 7001.conf -rw-r--r--. 1 root root 36202 Mar 20 15:34 7002.conf -rw-r--r--. 1 root root 36202 Mar 20 15:34 8000.conf -rw-r--r--. 1 root root 36202 Mar 20 15:34 8001.conf -rw-r--r--. 1 root root 36202 Mar 20 15:34 8002.conf -rw-r--r--. 1 root root 36202 Mar 20 15:34 9000.conf -rw-r--r--. 1 root root 36202 Mar 20 15:35 9001.conf -rw-r--r--. 1 root root 36202 Mar 20 15:35 9002.conf
修改配置文件中成相應的配置。只須要把原來的端口7000,替換成相應的端口,可使用相應的vim的命令,
以下,全文替換7000爲9000。
%s/7000/9000/g
語法爲 :[addr]s/源字符串/目的字符串/[option]
全局替換命令爲::%s/源字符串/目的字符串/g
[addr] 表示檢索範圍,省略時表示當前行。
如:「1,20」 :表示從第1行到20行;
「%」 :表示整個文件,同「1,$」;
「. ,$」 :從當前行到文件尾;
s : 表示替換操做
[option] : 表示操做類型
如:g 表示全局替換;
c 表示進行確認
p 表示替代結果逐行顯示(Ctrl + L恢復屏幕);
省略option時僅對每行第一個匹配串進行替換;
若是在源字符串和目的字符串中出現特殊字符,須要用」\」轉義
複製啓動腳本,
[root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_7001 [root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_7002 [root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_8000 [root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_8001 [root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_8002 [root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_9000 [root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_9001 [root@121 redis]# cp /etc/init.d/redis_7000 /etc/init.d/redis_9002
啓動腳本也要進行相應的替換。替換完成了,把全部的實例都啓動起來,以下,
[root@121 redis_7000]# ps -ef |grep 7000 root 27449 1 0 15:26 ? 00:00:00 /usr/local/bin/redis-server *:7000 root 28424 24088 0 15:59 pts/0 00:00:00 grep 7000 [root@121 redis_7000]# redis-cli -p 7000 shutdown [root@121 redis_7000]# ps -ef |grep 7000 root 28436 24088 0 15:59 pts/0 00:00:00 grep 7000 [root@121 redis_7000]#
先把原來啓動的7000端口殺掉,啓動全部的實例,
[root@121 redis_7000]# /etc/init.d/redis_7000 start Starting Redis server... [root@121 redis_7000]# /etc/init.d/redis_7001 start Starting Redis server... [root@121 redis_7000]# /etc/init.d/redis_7002 start Starting Redis server... [root@121 redis_7000]# /etc/init.d/redis_8000 start Starting Redis server... [root@121 redis_7000]# /etc/init.d/redis_8001 start Starting Redis server... [root@121 redis_7000]# /etc/init.d/redis_8002 start Starting Redis server... [root@121 redis_7000]# /etc/init.d/redis_9000 start Starting Redis server... [root@121 redis_7000]# /etc/init.d/redis_9001 start Starting Redis server... [root@121 redis_7000]# /etc/init.d/redis_9002 start Starting Redis server... [root@121 redis_7000]# ps -ef | grep redis root 28465 1 0 16:00 ? 00:00:00 /usr/local/bin/redis-server *:7000 root 28470 1 0 16:00 ? 00:00:00 /usr/local/bin/redis-server *:7001 root 28477 1 0 16:00 ? 00:00:00 /usr/local/bin/redis-server *:7002 root 28489 1 0 16:01 ? 00:00:00 /usr/local/bin/redis-server *:8000 root 28500 1 0 16:01 ? 00:00:00 /usr/local/bin/redis-server *:8001 root 28506 1 0 16:01 ? 00:00:00 /usr/local/bin/redis-server *:8002 root 28511 1 0 16:01 ? 00:00:00 /usr/local/bin/redis-server *:9000 root 28516 1 0 16:01 ? 00:00:00 /usr/local/bin/redis-server *:9001 root 28523 1 0 16:01 ? 00:00:00 /usr/local/bin/redis-server *:9002 root 28535 24088 0 16:01 pts/0 00:00:00 grep redis
全部的實例都啓動成功了。這時能夠檢查一下配置的日誌文件,pid文件,是否都存在,以下,
[root@121 log]# pwd /var/redis/log [root@121 log]# ls -l total 44 -rw-r--r--. 1 root root 768 Mar 20 14:30 redis_6379.log -rw-r--r--. 1 root root 7015 Mar 20 16:00 redis_7000.log -rw-r--r--. 1 root root 1864 Mar 20 16:00 redis_7001.log -rw-r--r--. 1 root root 1864 Mar 20 16:00 redis_7002.log -rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_8000.log -rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_8001.log -rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_8002.log -rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_9000.log -rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_9001.log -rw-r--r--. 1 root root 1864 Mar 20 16:01 redis_9002.log [root@121 run]# pwd /var/redis/run [root@121 run]# ls -l total 36 -rw-r--r--. 1 root root 6 Mar 20 16:00 redis_7000.pid -rw-r--r--. 1 root root 6 Mar 20 16:00 redis_7001.pid -rw-r--r--. 1 root root 6 Mar 20 16:00 redis_7002.pid -rw-r--r--. 1 root root 6 Mar 20 16:01 redis_8000.pid -rw-r--r--. 1 root root 6 Mar 20 16:01 redis_8001.pid -rw-r--r--. 1 root root 6 Mar 20 16:01 redis_8002.pid -rw-r--r--. 1 root root 6 Mar 20 16:01 redis_9000.pid -rw-r--r--. 1 root root 6 Mar 20 16:01 redis_9001.pid -rw-r--r--. 1 root root 6 Mar 20 16:01 redis_9002.pid
嗯,配置也應該沒有問題。這塊其實能夠寫到一個腳本里,有點麻煩也沒寫過太多的腳本,先就這樣吧。
下面就來看一下如何進行主從複製的配置。要把7001和7002端口的redis實例配成7000端口的redis的slave,7000端口的redis爲master。其餘依次類推。
slave 7001
[root@121 run]# redis-cli -p 7001 127.0.0.1:7001> slaveof 127.0.0.1 7000 OK 127.0.0.1:7001> get testkey (nil) 127.0.0.1:7001> get testkey "testdata" 127.0.0.1:7001>
slave 7002
[root@121 ~]# redis-cli -p 7002 127.0.0.1:7002> slaveof 127.0.0.1 7000 OK 127.0.0.1:7002> get testkey (nil) 127.0.0.1:7002> get testkey "testdata" 127.0.0.1:7002>
master 7000
[root@121 ~]# redis-cli -p 7000 127.0.0.1:7000> get testkey (nil) 127.0.0.1:7000> set testkey testdata OK 127.0.0.1:7000>
能夠看到主從複製也配置好了。這時候能夠到redis實例的文件找一個dump.rdb的文件,這個文件和主從複製有關係。
[root@121 redis_7000]# pwd /var/redis/redis_7000 [root@121 redis_7000]# ls dump.rdb
redis不能主主複製
這裏首先須要說明的是,在Redis中配置Master-Slave模式真是太簡單了。相信在閱讀完這篇Blog以後你也能夠輕鬆作到。這裏咱們仍是先列出一些理論性的知識,後面給出實際操做的案例。
下面的列表清楚的解釋了Redis Replication的特色和優點。
1). 同一個Master能夠同步多個Slaves。
2). Slave一樣能夠接受其它Slaves的鏈接和同步請求,這樣能夠有效的分載Master的同步壓力。所以咱們能夠將Redis的Replication架構視爲圖結構。
3). Master Server是以非阻塞的方式爲Slaves提供服務。因此在Master-Slave同步期間,客戶端仍然能夠提交查詢或修改請求。
4). Slave Server一樣是以非阻塞的方式完成數據同步。在同步期間,若是有客戶端提交查詢請求,Redis則返回同步以前的數據。
5). 爲了分載Master的讀操做壓力,Slave服務器能夠爲客戶端提供只讀操做的服務,寫服務仍然必須由Master來完成。即使如此,系統的伸縮性仍是獲得了很大的提升。
6). Master能夠將數據保存操做交給Slaves完成,從而避免了在Master中要有獨立的進程來完成此操做。
在Slave啓動並鏈接到Master以後,它將主動發送一個SYNC命令。此後Master將啓動後臺存盤進程(redis-cli save),同時收集全部接收到的用於修改數據集的命令,在後臺進程執行完畢後,Master將傳送整個數據庫文件到Slave,以完成一次徹底同步。而Slave服務器在接收到數據庫文件數據以後將其存盤並加載到內存中。此後,Master繼續將全部已經收集到的修改命令,和新的修改命令依次傳送給Slaves,Slave將在本次執行這些數據修改命令,從而達到最終的數據同步。
若是Master和Slave之間的連接出現斷連現象,Slave能夠自動重連Master,可是在鏈接成功以後,一次徹底同步將被自動執行。
================END================