在一臺機(jī)器上運(yùn)行2個及以上的redis,是redis支持多實例的功能,基于端口號的不同,就能夠運(yùn)行多個相互獨(dú)立的redis數(shù)據(jù)庫,當(dāng)我們的一個redis服務(wù)掛掉之后,另外一個可以馬上接管,繼續(xù)提供服務(wù),也就是一主多從的關(guān)系,下面來介紹一下,如何來搭建
什么是多實例?
就是機(jī)器上運(yùn)行了多個redis相互獨(dú)立的進(jìn)程
互不干擾的獨(dú)立的數(shù)據(jù)庫
叫做多個redis數(shù)據(jù)庫的實例,基于配置文件區(qū)分即可
1:準(zhǔn)備好2個配置文件,分別寫入如下內(nèi)容
#第一個
[root@localhost etc]# vim s25-master.redis.conf
port 6379
daemonize yes
pidfile /s25/6379/redis.pid
loglevel notice
logfile "/s25/6379/redis.log"
dbfilename dump.rdb
dir /s25/6379
protected-mode no
2:準(zhǔn)備第2個配置文件,分別寫入如下內(nèi)容
#第二個
[root@localhost etc]# vim s25-slave.redis.conf
port 6389
daemonize yes
pidfile /s25/6389/redis.pid
loglevel notice
logfile "/s25/6389/redis.log"
dbfilename dump.rdb
dir /s25/6389
protected-mode no
slaveof 127.0.0.1 6379 #也可直接在配置文件中,定義好復(fù)制關(guān)系,啟動后,立即就會建立復(fù)制
3:分別創(chuàng)建對應(yīng)的文件夾
[root@localhost etc]# mkdir -p /src/{6379,6389}
4:分別啟動2個數(shù)據(jù)庫,查看他們的復(fù)制關(guān)系
[root@localhost s25]# redis-server /etc/s25-master.redis.conf
[root@localhost s25]# redis-server /etc/s25-slave.redis.conf
效果圖

5:分別查看他們的進(jìn)程已經(jīng)復(fù)制關(guān)系
[root@localhost s25]# redis-cli -p 6379 info replication
[root@localhost s25]# redis-cli -p 6389 info replication

可以看到主從關(guān)系已經(jīng)建立了!??!
PS:通過一條命令,配置他們的復(fù)制關(guān)系,注意,這個命令只是臨時配置redis的復(fù)制關(guān)系,想要永久修改,還得修改配置文件
#手動建立復(fù)制關(guān)系哦
redis-cli -p 6389 slaveof 127.0.0.1 6379
6.此時6379已然是主庫,6389已然是從庫
此時可以向6379中寫入數(shù)據(jù),能夠同步到6389中
6389是一個只讀的數(shù)據(jù)庫,無法寫入數(shù)據(jù)

測試環(huán)節(jié)
在6379中設(shè)置key值,可以看到在6389中已經(jīng)自動可以獲取到所有設(shè)置的key值了

OK,效果還不錯
下面來試下一主多從的形式,以及主從復(fù)制故障切換
1.再創(chuàng)建一個配置文件,port是6399,且加入到一主一從的復(fù)制關(guān)系中去
vim s25-salve2-redis.conf
port 6399
daemonize yes
pidfile /s25/6399/redis.pid
loglevel notice
logfile "/s25/6399/redis.log"
dbfilename dump.rdb
dir /s25/6399
protected-mode no
slaveof 127.0.0.1 6379
2.創(chuàng)建數(shù)據(jù)文件夾
mkdir -p /s25/6399
3.啟動6399的數(shù)據(jù)庫,查看他的身份復(fù)制關(guān)系
[root@localhost s25]# redis-server /etc/s25-salve2.redis.conf
4.分別查看他們的進(jìn)程已經(jīng)復(fù)制關(guān)系
[root@localhost etc]# redis-cli -p 6399 info replication

好了,接下來就是測試環(huán)節(jié)了

5:先查看下本機(jī)啟動的所有redis進(jìn)程服務(wù)
[root@localhost etc]# ps -ef | grep redis
root 27407 1 0 18:26 ? 00:00:05 redis-server *:6379
root 27930 1 0 19:54 ? 00:00:00 redis-server 127.0.0.1:6389
root 28035 1 0 20:12 ? 00:00:00 redis-server 127.0.0.1:6399
root 28057 27704 0 20:16 pts/0 00:00:00 grep --color=auto redis
[root@localhost etc]#
6:分別查看復(fù)制關(guān)系
[root@localhost etc]# redis-cli -p 6379 info replication
[root@localhost etc]# redis-cli -p 6389 info replication
[root@localhost etc]# redis-cli -p 6399 info replication

7:此時直接干掉主庫即可
[root@localhost etc]# kill -9 27407
8:此時一位從庫,不樂意了,翻身農(nóng)奴做主人,去掉自己的從庫身份,沒有這個從庫的枷鎖,我就是我自己的主人
[root@localhost etc]# redis-cli -p 6399 slaveof no one
9:此時6399已然是主庫了,修改6389的復(fù)制信息,改為6399即可
[root@localhost etc]# redis-cli -p 6389 slaveof 127.0.0.1 6399
效果圖

10:此時可以向主庫6399寫入數(shù)據(jù),6389查看數(shù)據(jù)即可
測試寫入數(shù)據(jù)

11:從庫掛掉,無所謂,重新再建立一個從庫,加入主從復(fù)制即可,。,。
OK,大功告成~~~~~~~~~