redis單機(jī)多實(shí)例_主從復(fù)制
上一篇講到了redis單機(jī)單實(shí)例的安裝http://my.oschina.net/xinxingegeya/blog/389155,
這一篇主要安裝單機(jī)多實(shí)例,以及主從復(fù)制的配置。這就是一個(gè)redis的集群了。
先把7000這個(gè)端口關(guān)閉。
接下來按照redis的官方推薦的方式來安裝redis。
執(zhí)行以下命令,把redis-server腳本和redis-cli腳本放到bin目錄中
接下來建立目錄以存放redis的配置文件或是redis數(shù)據(jù),
mkdir /etc/redis ——redis的配置目錄
mkdir /var/redis ——redis的存放數(shù)據(jù)的目錄
mkdir /var/redis/log ——redis的日志文件的存放目錄
mkdir /var/redis/run ——redis的pid文件的存放目錄
mkdir /var/redis/redis_7000 ——redis實(shí)例的工作目錄
1 2 3 4 5 | [root@121 redis_7000] # mkdir /etc/redis ——redis的配置目錄 [root@121 redis_7000] # mkdir /var/redis ——redis的存放數(shù)據(jù)的目錄 [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實(shí)例的工作目錄 |
然后復(fù)制配置文件的模板到/etc/redis/目錄中,
修改配置文件,修改的有以下內(nèi)容,
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目錄中,
然后打開腳本,/etc/init.d/redis_7000
按照上面的配置修改參數(shù)如下,
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
下面開始啟動(dòng)redis,配置的端口為7000,如下,
1 2 3 4 5 6 7 8 9 | [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> |
啟動(dòng)成功了。
如何啟動(dòng)多個(gè)實(shí)例,也就是說一個(gè)實(shí)例要對(duì)應(yīng)著一個(gè)配置文件。如果使用init.d下的腳本的話也要一個(gè)實(shí)例對(duì)應(yīng)一個(gè)腳本。(也可以寫成一個(gè)啟動(dòng)腳本)
如下,復(fù)制腳本,新建目錄。
先新建多個(gè)實(shí)例的目錄。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [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 |
復(fù)制配置文件,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [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 |
修改配置文件中成相應(yīng)的配置。只需要把原來的端口7000,替換成相應(yīng)的端口,可以使用相應(yīng)的vim的命令,
如下,全文替換7000為9000。
%s/7000/9000/g
語法為 :[addr]s/源字符串/目的字符串/[option]
全局替換命令為::%s/源字符串/目的字符串/g
[addr] 表示檢索范圍,省略時(shí)表示當(dāng)前行。
如:“1,20” :表示從第1行到20行;
“%” :表示整個(gè)文件,同“1,$”;
“. ,$” :從當(dāng)前行到文件尾;
s : 表示替換操作
[option] : 表示操作類型
如:g 表示全局替換;
c 表示進(jìn)行確認(rèn)
p 表示替代結(jié)果逐行顯示(Ctrl + L恢復(fù)屏幕);
省略option時(shí)僅對(duì)每行第一個(gè)匹配串進(jìn)行替換;
如果在源字符串和目的字符串中出現(xiàn)特殊字符,需要用”\”轉(zhuǎn)義
復(fù)制啟動(dòng)腳本,
1 2 3 4 5 6 7 8 | [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 |
啟動(dòng)腳本也要進(jìn)行相應(yīng)的替換
替換完成了,把所有的實(shí)例都啟動(dòng)起來,如下,
1 2 3 4 5 6 7 | [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] # |
先把原來啟動(dòng)的7000端口殺掉,啟動(dòng)所有的實(shí)例,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | [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 |
好了,有點(diǎn)小激動(dòng),所有的實(shí)例都啟動(dòng)成功了。這時(shí)可以檢查一下配置的日志文件,pid文件,是否都存在,如下,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [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 |
嗯,配置也應(yīng)該沒有問題。
這塊其實(shí)可以寫到一個(gè)腳本里,有點(diǎn)麻煩也沒寫過太多的腳本,現(xiàn)就這樣吧。
下面就來看一下如何進(jìn)行主從復(fù)制的配置。要把7001和7002端口的redis實(shí)例配成7000端口的redis的slave,7000端口的redis為master。其他依次類推。
slave 7001
slave 7002
master 7000
可以看到主從復(fù)制也配置好了。這時(shí)候可以到redis實(shí)例的文件找一個(gè)dump.rdb的文件,這個(gè)文件和主從復(fù)制有關(guān)系。
redis不能主主復(fù)制
這里首先需要說明的是,在Redis中配置Master-Slave模式真是太簡單了。相信在閱讀完這篇Blog之后你也可以輕松做到。這里我們還是先列出一些理論性的知識(shí),后面給出實(shí)際操作的案例。
下面的列表清楚的解釋了Redis Replication的特點(diǎn)和優(yōu)勢(shì)。
1). 同一個(gè)Master可以同步多個(gè)Slaves。
2). Slave同樣可以接受其它Slaves的連接和同步請(qǐng)求,這樣可以有效的分載Master的同步壓力。因此我們可以將Redis的Replication架構(gòu)視為圖結(jié)構(gòu)。
3). Master Server是以非阻塞的方式為Slaves提供服務(wù)。所以在Master-Slave同步期間,客戶端仍然可以提交查詢或修改請(qǐng)求。
4). Slave Server同樣是以非阻塞的方式完成數(shù)據(jù)同步。在同步期間,如果有客戶端提交查詢請(qǐng)求,Redis則返回同步之前的數(shù)據(jù)。
5). 為了分載Master的讀操作壓力,Slave服務(wù)器可以為客戶端提供只讀操作的服務(wù),寫服務(wù)仍然必須由Master來完成。即便如此,系統(tǒng)的伸縮性還是得到了很大的提高。
6). Master可以將數(shù)據(jù)保存操作交給Slaves完成,從而避免了在Master中要有獨(dú)立的進(jìn)程來完成此操作。
在Slave啟動(dòng)并連接到Master之后,它將主動(dòng)發(fā)送一個(gè)SYNC命令。此后Master將啟動(dòng)后臺(tái)存盤進(jìn)程(redis-cli save),同時(shí)收集所有接收到的用于修改數(shù)據(jù)集的命令,在后臺(tái)進(jìn)程執(zhí)行完畢后,Master將傳送整個(gè)數(shù)據(jù)庫文件到Slave,以完成一次完全同步。而Slave服務(wù)器在接收到數(shù)據(jù)庫文件數(shù)據(jù)之后將其存盤并加載到內(nèi)存中。此后,Master繼續(xù)將所有已經(jīng)收集到的修改命令,和新的修改命令依次傳送給Slaves,Slave將在本次執(zhí)行這些數(shù)據(jù)修改命令,從而達(dá)到最終的數(shù)據(jù)同步。
如果Master和Slave之間的鏈接出現(xiàn)斷連現(xiàn)象,Slave可以自動(dòng)重連Master,但是在連接成功之后,一次完全同步將被自動(dòng)執(zhí)行。
===============================END===============================
聯(lián)系客服