操作系統(tǒng)環(huán)境:Ubuntu 20.04
Redis版本:6.2.8
如果已經(jīng)安裝過了,找到自己的安裝路徑。如果沒有安裝過,去官網(wǎng)下載 當(dāng)前最新版本是7.0,我這里用的是6.0,下載 redis-6.2.8.tar.gz,拷貝到自己的虛擬機(jī)或者云服務(wù)器。
tar -zxvf redis-6.2.8.tar.gz 解壓
cd redis-6.2.8
make && make install (注意是root權(quán)限)
在一臺(tái)機(jī)器通過監(jiān)聽不同的port模擬集群(三臺(tái)主服務(wù)器,每臺(tái)主服務(wù)器有兩個(gè)從服務(wù)器,總共9臺(tái)服務(wù)器)
cd redis-6.2.8
mkdir redis_cluster
mkdir 6390 6391 6392 6393 6394 6395 6396 6397 6398
3、以6390為例,在6390目錄下創(chuàng)建文件redis.conf,并添加如下配置注意:需要在步驟二創(chuàng)建的每個(gè)文件夾下創(chuàng)建配置文件,并做相應(yīng)修改。
touch redis.conf
#端口號
port 6390
#默認(rèn)ip為127.0.0.1,需要改為其他節(jié)點(diǎn)機(jī)器可以訪問的ip,否則創(chuàng)建集群時(shí)無法訪問對應(yīng)的端口,無法創(chuàng)建集群
bind 192.168.60.69
#redis后臺(tái)運(yùn)行
daemonize yes
#pidfile文件對應(yīng)6390,6391,6392…
pidfile /var/run/redis_6390.pid
#開啟集群
cluster-enabled yes
#集群的配置,配置文件首次啟動(dòng)自動(dòng)生成6390,6391,6392…
cluster-config-file nodes_6390.conf
#請求超時(shí),默認(rèn)15秒,可自行設(shè)置
cluster-node-timeout 10100
#aof日志開啟,有需要就開啟,它會(huì)每次寫操作都記錄一條日志
appendonly yes
4、分別向分別向6381-6388目錄copy一份配置文件cp -r redis.conf ../6391
cp -r redis.conf ../6392
cp -r redis.conf ../6393
cp -r redis.conf ../6394
cp -r redis.conf ../6395
cp -r redis.conf ../6396
cp -r redis.conf ../6397
cp -r redis.conf ../6398
需要修改的配置:
port 639x (根據(jù)端口號修改)
bind 127.0.0.1 我這里是本機(jī)模擬,寫的本地地址,根據(jù)實(shí)際ip地址填寫
pidfile /var/run/redis_639x.pid(根據(jù)端口號修改)
cluster-config-file nodes_639x.conf(根據(jù)端口號修改)
5. 啟動(dòng)Redis各個(gè)節(jié)點(diǎn)服務(wù)(一定要在src目錄下啟動(dòng),在其他地方無法啟動(dòng))./redis-server …/redis_cluster/6390/redis.conf
./redis-server …/redis_cluster/6391/redis.conf
./redis-server …/redis_cluster/6392/redis.conf
./redis-server …/redis_cluster/6393/redis.conf
./redis-server …/redis_cluster/6394/redis.conf
./redis-server …/redis_cluster/6395/redis.conf
./redis-server …/redis_cluster/6396/redis.conf
./redis-server …/redis_cluster/6397/redis.conf
./redis-server …/redis_cluster/6398/redis.conf
也可以寫個(gè)腳本啟動(dòng),就不用一個(gè)一個(gè)啟動(dòng)了:
vi cluster.sh
#!bin/bash
redis-server 6390/redis.conf
redis-server 6391/redis.conf
redis-server 6392/redis.conf
redis-server 6393/redis.conf
redis-server 6394/redis.conf
redis-server 6395/redis.conf
redis-server 6396/redis.conf
redis-server 6397/redis.conf
redis-server 6398/redis.conf
給腳本賦予執(zhí)行權(quán)限 : chmod +x cluster.sh
運(yùn)行腳本:./cluster.sh
因?yàn)閞edis最終是以數(shù)據(jù)庫的形式的存在,原理和mysql一樣,集群創(chuàng)建成功只能說明安裝redis成功了,但是最終>
!!!注意還需要在redis的src目錄下運(yùn)行
當(dāng)?shù)谝淮芜\(yùn)行的時(shí)候,才需要?jiǎng)?chuàng)建數(shù)據(jù)庫,以后的每一次運(yùn)行都不再需要了,直接啟動(dòng)集群即可
–cluster-replicas 2表示每個(gè)主節(jié)點(diǎn)有2個(gè)從節(jié)點(diǎn)
redis-cli --cluster create 127.0.0.1:6390 127.0.0.1:6391 127.0.0.1:6392 127.0.0.1:6393 127.0.0.1:6394 127.0.0.1:6395 127.0.0.1:6396 127.0.0.1:6397 127.0.0.1:6398 --cluster-replicas 2
注意:這里需要自己手動(dòng)輸入yes,如果超時(shí)了還沒有輸入,則會(huì)自動(dòng)退出,需要再次創(chuàng)建!
看到以下信息說明創(chuàng)建redis集群搭建成功,且16384個(gè)槽位分配成功,:
M: eabfa31d4e7cefa01a64ec5e67ddba219ad74035 127.0.0.1:6390
slots:[0-5460] (5461 slots) master
M: 2ac5623a741d707d4e2319795386d7e13b2ef575 127.0.0.1:6391
slots:[5461-10922] (5462 slots) master
M: 6e4cfc6b5918ed9b59e8e7c8765f1e37a86d3d1b 127.0.0.1:6392
slots:[10923-16383] (5461 slots) master
S: 41fdf2a9bec000cb282bd9bc75a1aa7f33417889 127.0.0.1:6393
replicates 6e4cfc6b5918ed9b59e8e7c8765f1e37a86d3d1b
S: 15403c0a88c4ccf9ad2b67364588819621774085 127.0.0.1:6394
replicates eabfa31d4e7cefa01a64ec5e67ddba219ad74035
S: 38ccc4d1ff1d7a3a31ed5ee805e5c479a6114588 127.0.0.1:6395
replicates 6e4cfc6b5918ed9b59e8e7c8765f1e37a86d3d1b
S: 6f9550397b996b1d53eaeb98d344cef3945ac482 127.0.0.1:6396
replicates 2ac5623a741d707d4e2319795386d7e13b2ef575
S: 78be26f6bdaa88e96e61ba145ba4236f59e9c5bc 127.0.0.1:6397
replicates 2ac5623a741d707d4e2319795386d7e13b2ef575
S: b6429d56397229322f3144b59b3c7e79904fa349 127.0.0.1:6398
replicates eabfa31d4e7cefa01a64ec5e67ddba219ad74035
Can I set the above configuration? (type 'yes' to accept): yes
>>>Nodes configuration updated
>>>Assign a different config epoch to each node
>>>Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
......
>>>Performing Cluster Check (using node 127.0.0.1:6390)
M: eabfa31d4e7cefa01a64ec5e67ddba219ad74035 127.0.0.1:6390
slots:[0-5460] (5461 slots) master
2 additional replica(s)
S: 78be26f6bdaa88e96e61ba145ba4236f59e9c5bc 127.0.0.1:6397
slots: (0 slots) slave
replicates 2ac5623a741d707d4e2319795386d7e13b2ef575
M: 6e4cfc6b5918ed9b59e8e7c8765f1e37a86d3d1b 127.0.0.1:6392
slots:[10923-16383] (5461 slots) master
2 additional replica(s)
S: 6f9550397b996b1d53eaeb98d344cef3945ac482 127.0.0.1:6396
slots: (0 slots) slave
replicates 2ac5623a741d707d4e2319795386d7e13b2ef575
S: 15403c0a88c4ccf9ad2b67364588819621774085 127.0.0.1:6394
slots: (0 slots) slave
replicates eabfa31d4e7cefa01a64ec5e67ddba219ad74035
S: 38ccc4d1ff1d7a3a31ed5ee805e5c479a6114588 127.0.0.1:6395
slots: (0 slots) slave
replicates 6e4cfc6b5918ed9b59e8e7c8765f1e37a86d3d1b
M: 2ac5623a741d707d4e2319795386d7e13b2ef575 127.0.0.1:6391
slots:[5461-10922] (5462 slots) master
2 additional replica(s)
S: b6429d56397229322f3144b59b3c7e79904fa349 127.0.0.1:6398
slots: (0 slots) slave
replicates eabfa31d4e7cefa01a64ec5e67ddba219ad74035
S: 41fdf2a9bec000cb282bd9bc75a1aa7f33417889 127.0.0.1:6393
slots: (0 slots) slave
replicates 6e4cfc6b5918ed9b59e8e7c8765f1e37a86d3d1b
[OK] All nodes agree about slots configuration.
>>>Check for open slots...
>>>Check slots coverage...
[OK] All 16384 slots covered.
可以看到:
ps -ef | grep redis
看到如下信息就說明集群啟動(dòng)成功了。
root 2418510 1 0 11:32 ? 00:00:00 ./redis-server 127.0.0.1:6390 [cluster]
root 2418609 1 0 11:32 ? 00:00:00 ./redis-server 127.0.0.1:6391 [cluster]
root 2418712 1 0 11:32 ? 00:00:00 ./redis-server 127.0.0.1:6392 [cluster]
root 2418815 1 0 11:32 ? 00:00:00 ./redis-server 127.0.0.1:6393 [cluster]
root 2419062 1 0 11:32 ? 00:00:00 ./redis-server 127.0.0.1:6394 [cluster]
root 2419278 1 0 11:33 ? 00:00:00 ./redis-server 127.0.0.1:6395 [cluster]
root 2419440 1 0 11:33 ? 00:00:00 ./redis-server 127.0.0.1:6396 [cluster]
root 2419575 1 0 11:33 ? 00:00:00 ./redis-server 127.0.0.1:6397 [cluster]
root 2419749 1 0 11:33 ? 00:00:00 ./redis-server 127.0.0.1:6398 [cluster]
進(jìn)入redis的服務(wù)器客戶端,也必須要在src目錄下進(jìn)行。其中,-c 表示以集群的方式啟動(dòng)節(jié)點(diǎn),-p 是節(jié)點(diǎn)的端口
./redis-cli -c -p 6390
輸入cluster info 查看集群狀態(tài)
輸入cluster nodes 查看集群各個(gè)節(jié)點(diǎn)主從關(guān)系
quit
10、注意集群是去中心化的,從任意一個(gè)從節(jié)點(diǎn),都可以訪問到主節(jié)點(diǎn)。
集群的讀寫都走主節(jié)點(diǎn)
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧