1 . 單機(jī)模式
本文基于 redis3版本進(jìn)行講解.
redis 下載地址 : https://redis.io/ . 進(jìn)行下載需要的版本.
或者通過命令進(jìn)行下載
$ wget http://download.redis.io/releases/redis-3.2.9.tar.gz
$ tar xzf redis-3.2.9.tar.gz
$ cd redis-3.2.9
$ make
$ src/redis-server # 啟動 reids 服務(wù).
$ src/redis-cli # 啟動 reids 客戶端.
redis> set foo bar
OK
redis> get foo
"bar"
以上就是 reids 單機(jī)模式啟動,更多配置或者命令請查看官網(wǎng)
1 . 主從模式
1.1 環(huán)境
本文以一臺機(jī)器進(jìn)行部署講解,多臺機(jī)器類似.
將下載的 redis 進(jìn)行復(fù)制三份.
$ cp -R redis redis01
$ cp -R redis redis02
$ cp -R redis redis03
如圖. 復(fù)制三分 redis.
在一臺機(jī)器上啟動3個 reids, 一個做 master, 兩個做 slave.
Master 端口: 6380
Slave1 端口: 6381
Slave2 端口: 6382
1.2 Sentinel配置
Master
redis.conf
port 6380
sentinel.conf
port 26380
sentinel monitor mymaster 192.168.1.103 6380 2
Slave1
redis.conf
port 6381
slaveof 192.168.1.103 6380
sentinel.conf
port 26381
sentinel monitor mymaster 192.168.1.103 6380 2
Slave2
redis.conf
port 6382
slaveof 192.168.1.103 6380
sentinel.conf
port 26382
sentinel monitor mymaster 192.168.1.103 6380 2
主從配置就是修改兩個文件 : redis.conf,sentinel.conf .
1.3 遠(yuǎn)程訪問
雖然這樣已經(jīng)配置了主從配置,但是不能通過 192.168.1.103
訪問的.只能通過127.0.0.1
訪問.這里如果需要進(jìn)行遠(yuǎn)程訪問的話,需要添加一個參數(shù)
Master 機(jī)器中sentinel.conf 中添加 "protected-mode no" 這樣就能通過 IP 進(jìn)行訪問了.
1.4 啟動
Master 啟動
Slave01 啟動
Slave02 啟動
啟動節(jié)點時,已經(jīng)看到日志是連接成功的. 下面在看 master 中的日志
Master 日志
最后啟動sentinel .
sentinel 服務(wù)啟動
由圖可以看出 redis 主從配置是成功的. 下面通過 info 命令驗證一下
info 命令驗證
可以看到當(dāng)前是 master ,master 下面有兩個從屬 reids...
到這里 redis 主從配置就完成了...是不是很簡單呢,趕緊動手配置一下吧...