您好,登錄后才能下訂單哦!
一、部署環(huán)境
系統(tǒng):centos7
通過在Linux系統(tǒng)上啟動兩個不同的redis實例來完成主從集群的部署
yum源已部署
二、redis的下載與安裝
1、下載:官網下載
2、安裝
創(chuàng)建/app/目錄,redis安裝在/app/目錄下
[root@liyg ~]# mkdir /app
[root@liyg ~]# cd /usr/local/src/
[root@liyg src]# ls
redis-4.0.11.tar.gz
[root@liyg src]# tar zxf redis-4.0.11.tar.gz -C /app/
[root@liyg~]# yum install -y gcc*
[root@liyg ~]# cd /app/redis-4.0.11/
[root@liyg redis-4.0.11]# make
[root@liyg redis-4.0.11]# make install
3、通過配置不同端口,來啟動不同實例
[root@liyg ~]# mkdir /app/redis_replication
[root@liyg ~]# mkdir /app/redis_replication/7001_master
[root@liyg ~]# mkdir /app/redis_replication/7002_slave
[root@liyg ~]# cp /app/redis-4.0.11/redis.conf /app/redis_replication/7001_master/
[root@liyg ~]# cp /app/redis-4.0.11/redis.conf /app/redis_replication/7002_slave/
編輯master配置文件
[root@liyg ~]# cd /app/redis_replication/
[root@liyg redis_replication]# vim 7001_master/redis.conf
#修改端口6379為7001
dir /app/redis_replication/7001_master
pidfile /var/run/redis_7001.pid
logfile "/app/redis_replication/7001_master/7001.log"
protected-mode no ##yes改為no 關閉安全模式
daemonize yes ##以守護進程方式啟動
requirepass qwer1234 ##開啟密碼認證
masterauth qwer1234 ##當開啟哨兵模式時需要添加,以免slave升級為master時不能同步
編輯slave配置文件
[root@liyg redis_replication]# vim 7001_master/redis.conf
#端口改為7002
dir /app/redis_replication/7002_slave
pidfile /var/run/redis_7002.pid
logfile "/app/redis_replication/7002_slave/7002.log"
protected-mode no
masterauth qwer1234 ##同步master時所需密碼
requirepass qwer1234 ##開啟密碼認證
slaveof 127.0.0.1 7001 ##新增
啟動redis
[root@liyg redis_replication]# redis-server 7001_master/redis.conf
2521:C 03 Dec 15:50:54.660 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2521:C 03 Dec 15:50:54.661 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=2521, just started
2521:C 03 Dec 15:50:54.661 # Configuration loaded
[root@liyg redis_replication]# redis-server 7002_slave/redis.conf
2530:C 03 Dec 15:51:07.037 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2530:C 03 Dec 15:51:07.037 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=2530, just started
2530:C 03 Dec 15:51:07.037 # Configuration loaded
4、驗證
[root@liyg redis_replication]# redis-cli -p 7001 -a qwer1234 info replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7002,state=online,offset=70,lag=1
[root@liyg redis_replication]# redis-cli -p 7002 -a qwer1234 info replication
master_host:127.0.0.1
master_port:7001
...
在master創(chuàng)建KEY查看slave是否同步
[root@liyg ~]# redis-cli -p 7001 -a qwer1234 set k1 t1
OK
[root@liyg ~]# redis-cli -p 7002 -a qwer1234 get k1
"t1"
至此,redis主從配置完成
三、配置redis主從哨兵模式
[root@liyg app]# cp redis-4.0.11/sentinel.conf redis_replication/
[root@liyg app]# vim redis_replication/sentinel.conf
protected-mode no ##關閉安全模式
sentinel monitor mymaster 127.0.0.1 7002 1
sentinel auth-pass mymaster qwer1234 ##當redis主從集群有密碼驗證時開啟
啟動
[root@liyg app]# redis-sentinel redis_replication/sentinel.conf
驗證
關閉master7001端口,查看7002端口是否通過哨兵升級為master
[root@liyg redis_replication]# redis-cli -p 7001 -a qwer1234 shutdown
當master關閉時,可看到哨兵的輸出內容
...
2138:X 04 Dec 09:59:15.625 # +failover-end master mymaster 127.0.0.1 7001
2138:X 04 Dec 09:59:15.625 # +switch-master mymaster 127.0.0.1 7001 127.0.0.1 7002
2138:X 04 Dec 09:59:15.625 * +slave slave 127.0.0.1:7001 127.0.0.1 7001 @ mymaster 127.0.0.1 7002
2138:X 04 Dec 09:59:18.645 # +sdown slave 127.0.0.1:7001 127.0.0.1 7001 @ mymaster 127.0.0.1 7002
[root@liyg redis_replication]# redis-cli -p 7002 -a qwer1234 info replication
role:master
connected_slaves:0
可以看到原來的slave7002已升級為master,再次啟動7001實例
[root@liyg redis_replication]# redis-server 7001_master/redis.conf
[root@liyg redis_replication]# redis-cli -p 7001 -a qwer1234 info replication
role:slave
master_host:127.0.0.1
master_port:7002
7001已變?yōu)?002的從庫,至此,驗證成功
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。