CentOS7下配置redis主從關係

JustDI-CM發表於2018-07-06

1.新建的兩臺虛擬機器,進行配置,1主2從,因步驟關聯,所以寫的命令都是用的相對路徑。

    主機與從機

    主機   10.0.0.7  master

    從機   10.0.0.8  slave

2.安裝redis,兩臺虛擬機器上的操作一樣

    1)關閉防火牆和selinux

    關閉selinux、防火牆

    #sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

    #setenforce 0

    #systemctl stop firewalld

    #systemctl disable firewalld

    2)安裝依賴

    製作本地yum源(https://blog.csdn.net/justdi0209/article/details/80936621

    #yum install –y gcc gcc-c++ make vim

    3)上傳、解壓安裝包,並進入解壓後的目錄,安裝redis

    #mkdir -p /root/soft

    #cd /root/soft

    #tar -xzvf redis-4.0.2.tar.gz

    #cd redis-4.0.2

    #make

3.主機上的配置

     1)複製檔案

    #mkdir –p /root/redis

    #cp redis.conf sentinel.conf /root/redis/

    #cd src/

    #cp redis-cli redis-sentinel redis-server /root/redis/

     2)修改檔案

    #cd /root/redis/

    #vim redis.conf

        修改內容如下:

        將bind 127.0.0.1這一行註釋掉

        將protected-mode yes改為protected-mode no

        將daemonize no改為daemonize yes

        將logfile ""改為logfile"/var/log/redis.log"

    #vim sentinel.conf

        將sentinel monitor mymaster 127.0.0.1 6379 2改為sentinel monitor mymaster 10.0.0.7 6379 2

        新增以下幾行:

        daemonize yes

        protected-mode no

        logfile "/var/log/sentinel.log"

4.從機上的配置

     1)複製檔案

    #mkdir –p /root/redis

    #cp redis.conf sentinel.conf /root/redis/

    #cd src/

    #cp redis-cli redis-sentinel redis-server /root/redis/

    #cd /root/redis/

     2)修改redis.conf

    #vim redis.conf

    修改內容如下:

        將bind 127.0.0.1註釋掉

        將protected-mode yes改為protected-mode no

        將daemonize no改為daemonize yes

        將logfile ""改為logfile "/var/log/redis.log"

        新增一行slaveof   10.0.0.7        6379

     3)修改sentinel.conf檔案

    # vim sentinel.conf

    修改內容如下:

        將sentinel monitor mymaster 127.0.0.1 6379 2修改為sentinel monitor mymaster 10.0.0.7 6379 2

        新增以下幾行:

        daemonize yes

        protected-mode no

        logfile "/var/log/sentinel.log"

    4)複製另一個從機的檔案,並修改

    #cp redis.conf redis6380.conf

    #vim redis6380.conf

    修改內容如下:

        將port 6379改為port 6380

        將pidfile /var/run/redis_6379.pid改為pidfile /var/run/redis_6380.pid

        將logfile "/var/log/redis.log"改為logfile "/var/log/redis6380.log"

    #cp sentinel.conf sentinel26380.conf

    #vim sentinel26380.conf

    修改內容如下:

        將port 26379改為port 26380

        將logfile "/var/log/sentinel.log"改為logfile "/var/log/sentinel26380.log"

5.啟動主機

    #./redis-server ./redis.conf

    #./redis-sentinel ./sentinel.conf

    #./redis-cli

6.啟動從機

    #./redis-server ./redis.conf

    #./redis-sentinel ./sentinel.conf

    #./redis-server ./redis6380.conf

    #./redis-sentinel ./sentinel26380.conf

    #./redis-cli(預設啟動6379埠的從機)

7.測試

    主機上設定一個鍵值對

    #set a 1

    6379埠從機獲取

    #get a

    測試6380埠從機

    #quit

    #./redis-cli -p 6380

    #get a

    從機是隻讀的

    #set b 2

8.測試主從切換

     1)模擬主機掛了,關閉master

    #quit

    #ps -ef | grep redis

    #kill -9 13951

     2)過幾分鐘後,檢視先前兩臺從機的資訊

    #info


    發現6380埠的從機role是slave,切換到6379埠的從機,檢視role是master

    #quit

    #./redis-cli

    #info

    再啟動先前的master,檢視資訊,role是slave

    #./redis-server ./redis.conf

    #./redis-sentinel ./sentinel.conf

    #./redis-cli

    redis主從配置成功


相關文章