Redis單機(jī)安裝以及集群搭建

<p>? ? ?? ?筆者在工作中也有接觸到非關(guān)系型數(shù)據(jù)庫(kù)Redis,主要用于緩存。但是也僅限于使用方面,底層沒(méi)具體了解過(guò),剛好有朋友正在研究這個(gè),我也就再深入研究一波。</p><p>
</p><p>
</p><p>今天主要來(lái)看一下Redis的安裝以及集群搭建(我也是第一次搭建)。
</p><p>
</p><p>環(huán)境:CentOS ?7.1,redis-5.0.7</p><p>
</p><p><strong>一、單機(jī)安裝</strong>
</p><p>1、將Redis安裝包放置服務(wù)器并解壓</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-8bc319687c10e405.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>2、進(jìn)入redis安裝目錄(筆者的是/usr/soft/redis-5.0.7)執(zhí)行<strong>make && make install</strong>命令,安裝完成后出現(xiàn)下圖的場(chǎng)景,則說(shuō)明安裝成功</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-1d57ab31473e7bb3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>3、在redis安裝目錄創(chuàng)建兩個(gè)文件夾etc(用于存放redis.conf),bin(存放相應(yīng)的命令腳本)</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-6d5705380ab90265.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>將redis.conf移到etc目錄中,將src下的mkreleasehdr.sh、redis-benchmark、redis-check-aof、redis-check-rdb、redis-cli、redis-server移到bin目錄中</p><pre>mv?redis.conf?etc/

進(jìn)入src目錄下

mv?mkreleasehdr.sh?redis-benchmark?redis-check-aof?redis-check-rdb?redis-cli?redis-server?../bin/</pre><p>
</p><p>3、配置redis.conf(常用配置項(xiàng))</p><pre>#注釋掉這一行,否則其它機(jī)器不可以訪問(wèn)當(dāng)前redis,也可以改成其他ip

bind?127.0.0.1

將protected-mode后面的yes改成no,也是為了讓其他ip訪問(wèn)當(dāng)前

redis?protected-mode?no

將daemonize后面的no改成yes,這個(gè)是為了讓redis后臺(tái)運(yùn)行。?

daemonize?yes

日志文件所在目錄logfile?/usr/soft/redis-5.0.7/logs/redis.log

可配置密碼,也可不配,例如123456是我設(shè)置的密碼?

如果配置了密碼,在用redis-cli連接的時(shí)候需要用【auth?‘密碼’】命令才用使用

requirepass?123456</pre><p>4、將redis安裝目錄加入環(huán)境變量</p><p>vim ~/.bashrc(也可以是/etc/profile)
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-2af3cbf2805cb360.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>保存之后執(zhí)行source?~/.bashrc(如果是/etc/profile文件,則是source /etc/profile)</p><p>5、啟動(dòng)redis</p><pre>#/usr/soft/redis-5.0.7/etc/redis.conf是配置文件
redis-server?/usr/soft/redis-5.0.7/etc/redis.conf</pre><p>
</p><p>6、客戶端連接
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-038abc03efc6a11c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>(推薦一個(gè)可視化連接工具RedisDesktopManager)
</p><p>用redis-cli創(chuàng)建一個(gè)key、value鍵值對(duì)</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-e79ec3440eceebc6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-e4383c28ca4fa088.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-6578e4adfff1e413.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>
</p><p>7、關(guān)閉redis</p><pre>#也可以直接通過(guò)kill?-9殺掉進(jìn)程
redis-cli?shutdown</pre><p>
</p><p>
</p><h1><strong>二、集群安裝</strong></h1><p>Redis集群是去中心化的,并沒(méi)有主從節(jié)點(diǎn)之分。集群中要求節(jié)點(diǎn)數(shù)量必須是,所以至少要有三個(gè)節(jié)點(diǎn),并且每個(gè)節(jié)點(diǎn)至少有一備份節(jié)點(diǎn),所以至少需要6個(gè)redis服務(wù)實(shí)例。</p><p>我這邊有三個(gè)服務(wù)器,每個(gè)服務(wù)器上啟兩個(gè)redis,一個(gè)active節(jié)點(diǎn),一個(gè)standby節(jié)點(diǎn)
</p><p>
</p><pre>#三臺(tái)服務(wù)器,每臺(tái)服務(wù)器redis端口號(hào)為7001,7002
192.169.197.100
192.169.197.110
192.169.197.120</pre><p>
</p><p>1、在redis目錄下創(chuàng)建redis_cluster目錄,在redis_cluster目錄下創(chuàng)建7001,7002兩個(gè)節(jié)點(diǎn)目錄,將redis.conf(基于單機(jī)安裝,配置項(xiàng)跟之前的一樣)復(fù)制到7001,7002兩個(gè)目錄中。
</p><p>
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-320769e4b5a31038.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-43082605da84635d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>2、分別修改7001,7002目錄下的redis.conf配置文件</p><pre>#7001目錄下的redis.conf
port?7001?#端口
pidfile?/var/run/redis_7001.pid?#進(jìn)程文件
cluster-enabled?yes?#開(kāi)啟集群功能
cluster-config-file?nodes-7001.conf?#集群配置文件,集群?jiǎn)?dòng)后生成
appendonly?yes??#開(kāi)啟日志

7002目錄下的redis.conf

port?7002?#端口
pidfile?/var/run/redis_7002.pid?#進(jìn)程文件
cluster-enabled?yes?#開(kāi)啟集群功能
cluster-config-file?nodes-7002.conf?#集群配置文件,集群?jiǎn)?dòng)后生成
appendonly?yes??#開(kāi)啟日志</pre><p>
</p><p>3、將redis_cluster用scp命令分發(fā)到另外兩臺(tái)服務(wù)器
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-8c04c370aec6c93a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>4、依次啟動(dòng)6個(gè)redis實(shí)例,(下面以腳本統(tǒng)一啟動(dòng))</p><pre>#!/bin/bash
echo?'開(kāi)始啟動(dòng)redis-cluster'

redisnodes=('cnode-1'?'cnode-2'?'cnode-3')
ports=('7001'?'7002')
for?ip?in?{redisnodes[@]}; do&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;port&nbsp;in&nbsp;{ports[@]};
???????do??????
???????????echo?"start?redis?ip:port"
???????????ssh?ip&nbsp;redis-server&nbsp;/usr/soft/redis-5.0.7/redis_cluster/port/redis.conf???
???????done
done
echo?'啟動(dòng)redis-cluster結(jié)束'</pre><p>
</p><p>腳本執(zhí)行結(jié)果:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-599c388ad1044908.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>5、創(chuàng)建集群命令,一共六個(gè)節(jié)點(diǎn)</p><pre>#如果沒(méi)設(shè)置密碼,需要把【-a?123456】去掉
redis-cli?-a?123456?--cluster?create?192.168.197.100:7001?192.168.197.110:7001?192.168.197.120:7001?192.168.197.100:7002?192.168.197.110:7002?192.168.197.120:7002?--cluster-replicas?1</pre><p>
</p><p>執(zhí)行之后會(huì)發(fā)現(xiàn)slots的分布
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-44b038980cbe46d0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>6、驗(yàn)證集群</p><pre>#[-c]參數(shù)是為了啟動(dòng)集群模式,缺少此參數(shù)會(huì)在設(shè)值的時(shí)候直接報(bào)錯(cuò)
redis-cli?-c?-h?192.168.197.100?-p?7001?-a?123456</pre><p>
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-6466de01dd2461ef.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-d381cf90e1f9c091.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>7、關(guān)閉集群
</p><p>可以一個(gè)一個(gè)關(guān)閉,但這邊使用腳本統(tǒng)一關(guān)閉</p><pre>#!/bin/bash

所有服務(wù)器節(jié)點(diǎn)的hostname

allnodes=('cnode-1'?'cnode-2'?'cnode-3')

端口號(hào)開(kāi)始

PORT=7001

結(jié)束端口號(hào)

ENDPORT=7003

密碼,如果redis沒(méi)配置密碼可省略

PASSWROD=123456
while?[?((PORT&nbsp;&lt;&nbsp;ENDPORT))&nbsp;!=&nbsp;&quot;0&quot;&nbsp;];&nbsp; do&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;ip&nbsp;in&nbsp;{allnodes[@]};????
????do????????
????????#判斷某個(gè)端口是否已被占用,如果是,則執(zhí)行關(guān)閉命令????????
????????count=ssh&nbsp;$ip&nbsp;lsof&nbsp;-i:$PORT&nbsp;|&nbsp;wc&nbsp;-l????????
????????if?[?count&nbsp;-gt&nbsp;0&nbsp;];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&quot;Stopping&nbsp;Redis&nbsp;ip:PORT&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssh&nbsp;ip?redis-cli?-p?PORT&nbsp;-a&nbsp;PASSWROD?shutdown?2>/dev/null????????
????????else????????????
????????????echo?"no?redis?ip:PORT"????????
????????fi????
????done????
PORT=$((PORT+1))doneexit?0</pre><p>
</p><p>腳本執(zhí)行結(jié)果:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/5080600-679b61aeff3cfbf6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>
</p><p>
</p><p>上述就是Redis單機(jī)版安裝和集群部署的全部步驟。</p><p>
</p><p>=======================================================</p><p>我是Liusy,一個(gè)喜歡健身的程序員。</p><p>歡迎關(guān)注【Liusy01】,一起交流Java技術(shù)及健身,獲取更多干貨。</p><p>
</p><p>
</p><p>?
</p>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容