centos7搭建redis主從復制
本環境在虛擬機進行,先測試在上線。
主機:192.168.161.179
從機:192.168.161.180
1、 安裝主redis
自己本地環境,關閉防火墻。
#sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g‘ /etc/selinux/config
#setenforce 0
#systemctl stop firewalld
#systemctl disable firewalld
安裝相關依賴
yum install –y gcc gcc-c++ make
下載redis4.0.2包
http://download.redis.io/releases/redis-4.0.2.tar.gz
下載好後上傳至服務器,進行解壓(我上傳至opt目錄)
解壓後編譯安裝
cd redis-4.0.2/
make PREFIX=/usr/local/redis install
將redis-4.0.2目錄的redis.conf sentinel.conf配置文件復制到安裝目錄/usr/local/redis
將redis-4.0.2/src目錄的redis-cli redis-sentinel redis-server復制到安裝目錄 /usr/local/redis/
修改配置
cd /usr/local/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 192.168.161.179 6379 2
添加一下幾行
daemonize yes
protected-mode no
logfile "/var/log/sentinel.log"
1、 配置redis從
安裝的方法及路徑與redis1主一樣
復制redis配置文件
cp redis.conf sentinel.conf /usr/local/redis/
cp redis-cli redis-sentinel redis-server /usr/local/redis/
cd /usr/local/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"
添加一行slaveof 192.168.161.179 6379 (主服務器ip)
修改sentinel.conf
修改內容如下:
192.168.161,179是主服務器ip
sentinel monitor mymaster 192.168.161.179 6379 2
daemonize yes
protected-mode no
logfile "/var/log/sentinel.log"
復制從文件在從機配置兩臺從,需要修改端口,默認端口為6379,但是在同一臺服務器搭建兩臺,則需要改變端口。
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"
3、 啟動主機
啟動從機,6380為從機2,
登陸時./redis-cli 進入的6379從redis 需要進入6380redis,加個端口就ok
./redis-cli 6380
1、 測試
主機上設置一個鍵值對
#set a 1
在從主機6379端口獲取
#get a
測試從主機
#quit
#./redis-cli –p 6380
#get a
從主機測試
#set b 2 (報錯因為從機是只讀)
1、 測試主從切換
模擬主掛了,關閉主
Ps –ef | grep redis
Kill -9 2074
登陸從機6380端口查看
切換為master了。
在啟動主redis查看
Cd /usr/local/redis
./redis-server ./redis.conf
./redis-sentinel ./sentinel.conf
./redis-cli
輸入info
原來的主現在變為了從,說明主從配置成功
如需設置主從帶密碼參考文檔
https://www.cnblogs.com/MYue/p/8855888.html
centos7搭建redis主從復制