轉(zhuǎn)載自:https://blog.csdn.net/weixin_42789427/article/details/103183053
一骑丸、安裝redis
1舌仍、安裝依賴包
yum install gcc -y
2、下載源碼包
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar xzf redis-5.0.5.tar.gz
mv redis-5.0.5 /home/zsoft/redis
3通危、安裝redis
cd /home/zsoft/redis
#安裝libc铸豁,不然會出現(xiàn)致命錯誤:jemalloc/jemalloc.h:沒有那個文件或目錄
make MALLOC=libc
make
cd src && make install
4、修改配置文件
vim redis.conf
#修改redis為后臺啟動
將daemonize no改為daemonize yes
5菊碟、啟動redis并驗(yàn)證
/home/zsoft/redis/src/redis-server /home/zsoft/redis/redis.conf
/home/zsoft/redis/src/redis-cli -h 127.0.0.1 -p 6379
輸入命令 ping
如果返回的是pong,則啟動成功
127.0.0.1:7381(主)
127.0.0.1:7382(主)
127.0.0.1:7383(主)
127.0.0.1:7384(備)
127.0.0.1:7385(備)
127.0.0.1:7386(備)
1、安裝依賴包
yum install gcc -y
2逆害、下載源碼包
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar xzf redis-5.0.5.tar.gz
mv redis-5.0.5 /home/zsoft/redis
3藏古、安裝redis
cd /home/zsoft/redis
#安裝libc,不然會出現(xiàn)致命錯誤:jemalloc/jemalloc.h:沒有那個文件或目錄
make MALLOC=libc
make
cd src && make install
4忍燥、創(chuàng)建集群目錄并修改配置文件
cd /home/zsoft/redis/
mkdir conf data logs pid nodes
cd data && mkdir 738{1..6}
#復(fù)制配置文件到指定節(jié)點(diǎn)
cp redis.conf ./conf/redis-7381.conf
cp redis.conf ./conf/redis-7382.conf
cp redis.conf ./conf/redis-7383.conf
cp redis.conf ./conf/redis-7384.conf
cp redis.conf ./conf/redis-7385.conf
cp redis.conf ./conf/redis-7386.conf
#修改所有配置文件
vim ./conf/redis-7381.conf
#綁定的IP為當(dāng)前機(jī)器的IP
bind 172.31.41.3
#開啟redis的服務(wù)保護(hù)模式
protected-mode yes
#端口設(shè)置
port 7381
#redis改為后臺啟動
daemonize yes
#數(shù)據(jù)文件存放路徑設(shè)置
dir /home/zsoft/redis/data/7381/
#pid文件設(shè)置拧晕,這里一定要與當(dāng)前服務(wù)機(jī)的redis端口號對應(yīng)
pidfile /home/zsoft/redis/pid/redis-7381.pid
#設(shè)置日志文件
logfile "/home/zsoft/redis/logs/redis-7381.log"
##開啟cluster集群
#啟動集群模式
cluster-enabled yes
#設(shè)置集群配置文件,這里的7381也要與當(dāng)前服務(wù)機(jī)的redis端口號所對應(yīng)
cluster-config-file /home/zsoft/redis/nodes/nodes-7381.conf
#設(shè)置超時時間
cluster-node-timeout 15000
#開啟AOF日志記錄
appendonly yes
#設(shè)置redis登錄密碼和集群密碼
masterauth ESOPredis@0312!@
requirepass ESOPredis@0312!@
#修改剩余五個配置文件
vim ./conf/redis-7382.conf
:%s/7381/7382
5梅垄、啟動redis
/home/zsoft/redis/src/redis-server /home/zsoft/redis/conf/redis-7381.conf
/home/zsoft/redis/src/redis-server /home/zsoft/redis/conf/redis-7382.conf
/home/zsoft/redis/src/redis-server /home/zsoft/redis/conf/redis-7383.conf
/home/zsoft/redis/src/redis-server /home/zsoft/redis/conf/redis-7384.conf
/home/zsoft/redis/src/redis-server /home/zsoft/redis/conf/redis-7385.conf
/home/zsoft/redis/src/redis-server /home/zsoft/redis/conf/redis-7386.conf
ps -ef | grep redis | grep -v grep
zsoft? ? 12972? ? 1? 0 16:18 ?? ? ? ? 00:00:00 /home/zsoft/redis/src/redis-server *:7381 [cluster]
zsoft? ? 12974? ? 1? 0 16:18 ?? ? ? ? 00:00:00 /home/zsoft/redis/src/redis-server *:7382 [cluster]
zsoft? ? 12981? ? 1? 0 16:18 ?? ? ? ? 00:00:00 /home/zsoft/redis/src/redis-server *:7383 [cluster]
zsoft? ? 12987? ? 1? 0 16:18 ?? ? ? ? 00:00:00 /home/zsoft/redis/src/redis-server *:7384 [cluster]
zsoft? ? 12992? ? 1? 0 16:18 ?? ? ? ? 00:00:00 /home/zsoft/redis/src/redis-server *:7385 [cluster]
zsoft? ? 12994? ? 1? 0 16:18 ?? ? ? ? 00:00:00 /home/zsoft/redis/src/redis-server *:7386 [cluster]
6厂捞、創(chuàng)建集群
/home/zsoft/redis/src/redis-cli --cluster create 172.31.41.3:7381 172.31.41.3:7382 172.31.41.3:7383 172.31.41.3:7384 172.31.41.3:7385 172.31.41.3:7386 --cluster-replicas 1 -a 'ESOPredis@0312!@'
#
–cluster create:
表示創(chuàng)建一個redis集群输玷,需要注意的是前后注意空格。
–cluster-replicas 1:
表示為集群中的每一個主節(jié)點(diǎn)指定一個從節(jié)點(diǎn)靡馁,即一比一的復(fù)制欲鹏。需要注意的是前后注意空格。
-a 'ESOPredis@0312!@':
表示我每個redis節(jié)點(diǎn)都配置了密碼
7臭墨、驗(yàn)證集群狀態(tài)
/home/zsoft/redis/src/redis-cli --cluster check 172.31.41.3:7381 -a 'ESOPredis@0312!@'
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.31.41.3:7381 (22dc0be1...) -> 0 keys | 5461 slots | 1 slaves.
172.31.41.3:7382 (69483f54...) -> 0 keys | 5462 slots | 1 slaves.
172.31.41.3:7383 (753d6812...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.31.41.3:7381)
M: 22dc0be1e7dcf6e507b835842ba6dd940fc0d4c1 172.31.41.3:7381
? slots:[0-5460] (5461 slots) master
? 1 additional replica(s)
M: 69483f541b9fcdd74a4ed9f7bea5a2353b7ad27a 172.31.41.3:7382
? slots:[5461-10922] (5462 slots) master
? 1 additional replica(s)
M: 753d68127f48a4962486df9b2ab866411a695b1c 172.31.41.3:7383
? slots:[10923-16383] (5461 slots) master
? 1 additional replica(s)
S: f9b299fadfc22a43896792e77382e60ee8fd5154 172.31.41.3:7386
? slots: (0 slots) slave
? replicates 22dc0be1e7dcf6e507b835842ba6dd940fc0d4c1
S: f30770f223264bc32027a90be665b3a9bfaea2bd 172.31.41.3:7385
? slots: (0 slots) slave
? replicates 753d68127f48a4962486df9b2ab866411a695b1c
S: b403959de6d61bc3b5f23866b750802df8a864c7 172.31.41.3:7384
? slots: (0 slots) slave
? replicates 69483f541b9fcdd74a4ed9f7bea5a2353b7ad27a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.