redis5 集群安装
1 服务器分配
服务器三台:192.168.0.36、192.168.0.37、192.168.0.38
2 上传安装包至第一台服务器/u01目录,并安装
cd /u01
tar xzf redis-5.0.6.tar.gz
cd redis-5.0.6
make
mkdir -p conf/6379
mkdir -p conf/6380
下载地址:https://download.redis.io/releases/
2 配置
2.1 配置6379节点
vim conf/6379/redis.conf
输入文件内容
#bind 127.0.0.1 注释掉
#需要不同服务器的节点连通,这个就要设置为 no
protected-mode no
#设置后台运行redis
daemonize yes
#no,redis启动时会从rdb文件恢复数据,yes不会。
appendonly yes
port 6379
pidfile /u01/redis-5.0.6/run/redis_6379.pid
appendfilename "appendonly_6379.aof"
logfile "/u01/redis-5.0.6/log/redis_6379.log"
#不要密码就注释
requirepass password
#集群配置
cluster-enabled yes
cluster-node-timeout 5000
cluster-config-file nodes-6379.conf
#持久化配置
#dbfilename:持久化数据存储在本地的文件
dbfilename dump_6379.rdb
#dir:持久化数据存储在本地的路径,如果是./在/redis/redis-3.0.6/src下启动的redis-cli,则数据会存储在当前src目录下
dir /u01/redis-5.0.6/dbfile
#save时间,以下分别表示更改了1个key时间隔900s进行持久化存储;更改了10个key300s进行存储;更改10000个key60s进行存储。
save 900 1
save 300 10
save 60 10000
2.2 配置6380节点
vim conf/6380/redis.conf
输入文件内容
#bind 127.0.0.1 注释掉
#需要不同服务器的节点连通,这个就要设置为 no
protected-mode no
#设置后台运行redis
daemonize yes
appendonly yes
port 6380
pidfile /u01/redis-5.0.6/run/redis_6380.pid
appendfilename "appendonly_6380.aof"
logfile "/u01/redis-5.0.6/log/redis_6380.log"
#不要密码就注释
requirepass password
#集群配置
cluster-enabled yes
cluster-node-timeout 5000
cluster-config-file nodes-6380.conf
#持久化配置
#dbfilename:持久化数据存储在本地的文件
dbfilename dump_6380.rdb
#dir:持久化数据存储在本地的路径,如果是./在/redis/redis-3.0.6/src下启动的redis-cli,则数据会存储在当前src目录下
dir /u01/redis-5.0.6/dbfile
#save时间,以下分别表示更改了1个key时间隔900s进行持久化存储;更改了10个key300s进行存储;更改10000个key60s进行存储。
save 900 1
save 300 10
save 60 10000
配置参考https://blog.csdn.net/ljl890705/article/details/51039015/
2.3 创建log、run目录
mkdir -p /u01/redis-5.0.6/log
mkdir -p /u01/redis-5.0.6/run
mkdir -p /u01/redis-5.0.6/dbfile
3 将按照设置好的redis复制到另外两台服务器
cd /u01
scp -r redis-5.0.6 root@192.168.0.37:/u01
scp -r redis-5.0.6 root@192.168.0.38:/u01
4 在三台服务器上分别启动redis
/u01/redis-5.0.6/src/redis-server /u01/redis-5.0.6/conf/6379/redis.conf
/u01/redis-5.0.6/src/redis-server /u01/redis-5.0.6/conf/6380/redis.conf
5 执行创建集群
如果没有设置密码 删除 -a password
/u01/redis-5.0.6/src/redis-cli --cluster create --cluster-replicas 1 -a password 192.168.0.36:6379 192.168.0.37:6379 192.168.0.38:6379 192.168.0.36:6380 192.168.0.37:6380 192.168.0.38:6380
执行结果:
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.0.32:6380 to 192.168.0.31:6379
Adding replica 192.168.0.33:6380 to 192.168.0.32:6379
Adding replica 192.168.0.31:6380 to 192.168.0.33:6379
M: 80ab043dc8b1a0577d3d5154ca5e4228f42ca866 192.168.0.31:6379
slots:[0-5460] (5461 slots) master
M: abaffa7628928e144af639876c9b0e698e9f181f 192.168.0.32:6379
slots:[5461-10922] (5462 slots) master
M: 834817dad8fd39767d76e4ee62c57cc73fb1e6bf 192.168.0.33:6379
slots:[10923-16383] (5461 slots) master
S: 758e1af23ab06a11b375f8b9347e51b926268c80 192.168.0.31:6380
replicates 834817dad8fd39767d76e4ee62c57cc73fb1e6bf
S: d506ae0bcf170fa352e08893d7fa98d8de95bb84 192.168.0.32:6380
replicates 80ab043dc8b1a0577d3d5154ca5e4228f42ca866
S: bebff53a2abf087517fd733133ed224876b433b3 192.168.0.33:6380
replicates abaffa7628928e144af639876c9b0e698e9f181f
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 192.168.0.31:6379)
M: 80ab043dc8b1a0577d3d5154ca5e4228f42ca866 192.168.0.31:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 834817dad8fd39767d76e4ee62c57cc73fb1e6bf 192.168.0.33:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: d506ae0bcf170fa352e08893d7fa98d8de95bb84 192.168.0.32:6380
slots: (0 slots) slave
replicates 80ab043dc8b1a0577d3d5154ca5e4228f42ca866
S: 758e1af23ab06a11b375f8b9347e51b926268c80 192.168.0.31:6380
slots: (0 slots) slave
replicates 834817dad8fd39767d76e4ee62c57cc73fb1e6bf
S: bebff53a2abf087517fd733133ed224876b433b3 192.168.0.33:6380
slots: (0 slots) slave
replicates abaffa7628928e144af639876c9b0e698e9f181f
M: abaffa7628928e144af639876c9b0e698e9f181f 192.168.0.32:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
6 测试
/u01/redis-5.0.6/src/redis-cli -c -p 6379 -a password
127.0.0.1:6380> set pppppppppppppppppppppppppppppppppppppp 1
-> Redirected to slot [10527] located at 192.168.0.37:6379
OK
192.168.0.37:6379> get pppppppppppppppppppppppppppppppppppppp
"1"
192.168.0.37:6379> set ppppppppppppppppppppppppppppppppppppppaaaaaaaaaaaaa 1
-> Redirected to slot [1258] located at 192.168.0.38:6379
OK
192.168.0.38:6379> set ppppppppppppppppppppppppppppppppppppppaaaaaaaaaaaaaaaaaaaaaaaaaaa 1
OK
192.168.0.38:6379> get ppppppppppppppppppppppppppppppppppppppaaaaaaaaaaaaaaaaaaaaaaaaaaa
"1"
192.168.0.38:6379> set xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxaaaaaaaaaaaaaaaaaaaaqqqqqqqqqqqqqqqqq w
-> Redirected to slot [14939] located at 192.168.0.36:6379
OK
192.168.0.36:6379> get xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxaaaaaaaaaaaaaaaaaaaaqqqqqqqqqqqqqqqqq
"w"
192.168.0.36:6379>
7 关闭
src/redis-cli -p 6380 shutdown
或
src/redis-cli -p 6380 -a password
shutdown