redis集群重新启动案例-步骤详细-有启动脚本
redis集群重新启动案例
(启动脚本,在文末~~~)
- 切换root账户
- cd 到集群所在文件夹myapps,找到集群节点配置文件redis.conf里面指示的存放数据的dir,这里是同目录下的redis
- 删除数据文件aof、rdb
- 删除集群节点配置文件nodes*.conf
- 关闭防火墙
- 启动集群服务器脚本startall.sh
- 启动创建集群脚本creat-cluster.sh
- 选择任意节点,如:7001,启动redis客户端,并且指定集群,-c
- set几个key value,发现ok,并且分配到集群其他节点,对应的插槽
- cluster info ,查看集群信息,ok,正常
- 用redis可视化工具,如redis desktop manage,查看redis节点存储值情况
- java连接redis,用Jedis包,版本要对应上,报错时多是版本不匹配导致
linux启动redis集群情况:
[water@centos-water001 ~]$ su
Password:
[root@centos-water001 water]# cd /
[root@centos-water001 /]# cd /home/admin/myapps
[root@centos-water001 myapps]# ll
total 8
drwxr-xr-x. 3 root root 4096 Jan 6 01:27 redis
drwxr-xr-x. 3 root root 69 Jan 4 21:48 redis1
drwxr-xr-x. 3 root root 69 Jan 4 21:53 redis2
drwxr-xr-x. 8 root root 4096 Jan 5 22:06 redis-cluster
[root@centos-water001 myapps]# cd redis
[root@centos-water001 redis]# ll
total 100
-rw-r--r--. 1 root root 276 Jan 5 23:25 appendonly.aof
drwxr-xr-x. 2 root root 4096 Jan 3 19:47 bin
-rw-r--r--. 1 root root 197 Jan 6 01:27 dump.rdb
-rw-r--r--. 1 root root 811 Jan 5 23:20 nodes-7001.conf
-rw-r--r--. 1 root root 811 Jan 5 23:20 nodes-7002.conf
-rw-r--r--. 1 root root 811 Jan 5 23:20 nodes-7003.conf
-rw-r--r--. 1 root root 811 Jan 5 23:20 nodes-7004.conf
-rw-r--r--. 1 root root 811 Jan 5 23:20 nodes-7005.conf
-rw-r--r--. 1 root root 811 Jan 5 23:20 nodes-7006.conf
-rw-r--r--. 1 root root 61892 Jan 4 21:10 redis.conf
[root@centos-water001 redis]# rm -rf node*.*
[root@centos-water001 redis]# ll
total 76
-rw-r--r--. 1 root root 276 Jan 5 23:25 appendonly.aof
drwxr-xr-x. 2 root root 4096 Jan 3 19:47 bin
-rw-r--r--. 1 root root 197 Jan 6 01:27 dump.rdb
-rw-r--r--. 1 root root 61892 Jan 4 21:10 redis.conf
[root@centos-water001 redis]# vim appendonly.aof
[root@centos-water001 redis]# vim dump.rdb
[root@centos-water001 redis]# rm -rf appendonly.aof
[root@centos-water001 redis]# rm -rf dump.rdb
[root@centos-water001 redis]# ll
total 68
drwxr-xr-x. 2 root root 4096 Jan 3 19:47 bin
-rw-r--r--. 1 root root 61892 Jan 4 21:10 redis.conf
[root@centos-water001 redis]# cd ..
[root@centos-water001 myapps]# ll
total 4
drwxr-xr-x. 3 root root 33 Jan 8 09:41 redis
drwxr-xr-x. 3 root root 69 Jan 4 21:48 redis1
drwxr-xr-x. 3 root root 69 Jan 4 21:53 redis2
drwxr-xr-x. 8 root root 4096 Jan 5 22:06 redis-cluster
[root@centos-water001 myapps]# cd redis-cluster/
[root@centos-water001 redis-cluster]# l
bash: l: command not found...
[root@centos-water001 redis-cluster]# ll
total 8
drwxr-xr-x. 3 root root 33 Jan 5 01:00 7001
drwxr-xr-x. 3 root root 33 Jan 5 22:14 7002
drwxr-xr-x. 3 root root 33 Jan 5 01:03 7003
drwxr-xr-x. 3 root root 33 Jan 5 01:03 7004
drwxr-xr-x. 3 root root 33 Jan 5 01:04 7005
drwxr-xr-x. 3 root root 33 Jan 5 01:05 7006
-rwxr--r--. 1 root root 182 Jan 5 22:06 create-cluster.sh
-rwxr--r--. 1 root root 276 Jan 5 01:14 startall.sh
[root@centos-water001 redis-cluster]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-01-08 09:38:21 CST; 3min 31s ago
Docs: man:firewalld(1)
Main PID: 828 (firewalld)
Tasks: 2
CGroup: /system.slice/firewalld.service
└─828 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
Jan 08 09:38:21 centos-water001 systemd[1]: Starting firewalld - dynamic firewall daemon...
Jan 08 09:38:21 centos-water001 systemd[1]: Started firewalld - dynamic firewall daemon.
Jan 08 09:38:22 centos-water001 firewalld[828]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future r...ing it now.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos-water001 redis-cluster]# systemctl stop firewalld
[root@centos-water001 redis-cluster]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sat 2022-01-08 09:42:11 CST; 9s ago
Docs: man:firewalld(1)
Process: 828 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 828 (code=exited, status=0/SUCCESS)
Jan 08 09:38:21 centos-water001 systemd[1]: Starting firewalld - dynamic firewall daemon...
Jan 08 09:38:21 centos-water001 systemd[1]: Started firewalld - dynamic firewall daemon.
Jan 08 09:38:22 centos-water001 firewalld[828]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future r...ing it now.
Jan 08 09:42:09 centos-water001 systemd[1]: Stopping firewalld - dynamic firewall daemon...
Jan 08 09:42:11 centos-water001 systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos-water001 redis-cluster]# ./startall.sh
3424:C 08 Jan 2022 09:42:59.525 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3424:C 08 Jan 2022 09:42:59.525 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3424, just started
3424:C 08 Jan 2022 09:42:59.525 # Configuration loaded
3426:C 08 Jan 2022 09:42:59.552 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3426:C 08 Jan 2022 09:42:59.552 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3426, just started
3426:C 08 Jan 2022 09:42:59.552 # Configuration loaded
3434:C 08 Jan 2022 09:42:59.574 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3434:C 08 Jan 2022 09:42:59.574 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3434, just started
3434:C 08 Jan 2022 09:42:59.574 # Configuration loaded
3436:C 08 Jan 2022 09:42:59.615 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3436:C 08 Jan 2022 09:42:59.615 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3436, just started
3436:C 08 Jan 2022 09:42:59.615 # Configuration loaded
3441:C 08 Jan 2022 09:42:59.646 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3441:C 08 Jan 2022 09:42:59.646 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3441, just started
3441:C 08 Jan 2022 09:42:59.646 # Configuration loaded
3446:C 08 Jan 2022 09:42:59.678 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3446:C 08 Jan 2022 09:42:59.679 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3446, just started
3446:C 08 Jan 2022 09:42:59.679 # Configuration loaded
[root@centos-water001 redis-cluster]# ./create-cluster.sh
>>> 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.18.128:7005 to 192.168.18.128:7001
Adding replica 192.168.18.128:7006 to 192.168.18.128:7002
Adding replica 192.168.18.128:7004 to 192.168.18.128:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 5ab5d4b27872fa5d1c54d5bcb13ac5c5b6ba7379 192.168.18.128:7001
slots:[0-5460] (5461 slots) master
M: 3799d3df1a5169c776b0f04e9e442a97070487af 192.168.18.128:7002
slots:[5461-10922] (5462 slots) master
M: c8cfc1f94ccb0845a420e2652518c0e29b01c302 192.168.18.128:7003
slots:[10923-16383] (5461 slots) master
S: ebf501fa54446fa43fd72e56278cf60a572feac5 192.168.18.128:7004
replicates 3799d3df1a5169c776b0f04e9e442a97070487af
S: 860793c6ef9f6124466cbc2cd64311c38413b10b 192.168.18.128:7005
replicates c8cfc1f94ccb0845a420e2652518c0e29b01c302
S: 48e8548907909875035c66ecf1063c6f49ff8861 192.168.18.128:7006
replicates 5ab5d4b27872fa5d1c54d5bcb13ac5c5b6ba7379
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.18.128:7001)
M: 5ab5d4b27872fa5d1c54d5bcb13ac5c5b6ba7379 192.168.18.128:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: c8cfc1f94ccb0845a420e2652518c0e29b01c302 192.168.18.128:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: ebf501fa54446fa43fd72e56278cf60a572feac5 192.168.18.128:7004
slots: (0 slots) slave
replicates 3799d3df1a5169c776b0f04e9e442a97070487af
M: 3799d3df1a5169c776b0f04e9e442a97070487af 192.168.18.128:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 860793c6ef9f6124466cbc2cd64311c38413b10b 192.168.18.128:7005
slots: (0 slots) slave
replicates c8cfc1f94ccb0845a420e2652518c0e29b01c302
S: 48e8548907909875035c66ecf1063c6f49ff8861 192.168.18.128:7006
slots: (0 slots) slave
replicates 5ab5d4b27872fa5d1c54d5bcb13ac5c5b6ba7379
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@centos-water001 redis-cluster]# ps -aux | grep redis
root 3425 0.4 0.2 156556 8080 ? Ssl 09:42 0:00 ./bin/redis-server 192.168.18.128:7001 [cluster]
root 3430 0.3 0.2 162700 10136 ? Ssl 09:42 0:00 ./bin/redis-server 192.168.18.128:7002 [cluster]
root 3435 0.3 0.2 162700 10120 ? Ssl 09:42 0:00 ./bin/redis-server 192.168.18.128:7003 [cluster]
root 3440 0.3 0.3 166796 14144 ? Ssl 09:42 0:00 ./bin/redis-server 192.168.18.128:7004 [cluster]
root 3445 0.4 0.3 160652 12112 ? Ssl 09:42 0:00 ./bin/redis-server 192.168.18.128:7005 [cluster]
root 3450 0.2 0.3 160652 12096 ? Ssl 09:42 0:00 ./bin/redis-server 192.168.18.128:7006 [cluster]
root 3472 0.0 0.0 112812 972 pts/0 S+ 09:43 0:00 grep --color=auto redis
[root@centos-water001 redis-cluster]# ll
total 8
drwxr-xr-x. 3 root root 33 Jan 5 01:00 7001
drwxr-xr-x. 3 root root 33 Jan 5 22:14 7002
drwxr-xr-x. 3 root root 33 Jan 5 01:03 7003
drwxr-xr-x. 3 root root 33 Jan 5 01:03 7004
drwxr-xr-x. 3 root root 33 Jan 5 01:04 7005
drwxr-xr-x. 3 root root 33 Jan 5 01:05 7006
-rwxr--r--. 1 root root 182 Jan 5 22:06 create-cluster.sh
-rwxr--r--. 1 root root 276 Jan 5 01:14 startall.sh
[root@centos-water001 redis-cluster]# cd 7001
[root@centos-water001 7001]# ll
total 68
drwxr-xr-x. 2 root root 4096 Jan 4 22:01 bin
-rw-r--r--. 1 root root 61891 Jan 5 01:00 redis.conf
[root@centos-water001 7001]# ./bin/redis-cli -h 192.168.18.128 -p 7001 -c
192.168.18.128:7001> ping
PONG
192.168.18.128:7001> keys *
(empty list or set)
192.168.18.128:7001> set username waterking
-> Redirected to slot [14315] located at 192.168.18.128:7003
OK
192.168.18.128:7003> set password 123
-> Redirected to slot [9540] located at 192.168.18.128:7002
OK
192.168.18.128:7002> set school sanshui
OK
192.168.18.128:7002> keys *
1) "password"
2) "school"
192.168.18.128:7002> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:141
cluster_stats_messages_pong_sent:151
cluster_stats_messages_meet_sent:4
cluster_stats_messages_sent:296
cluster_stats_messages_ping_received:150
cluster_stats_messages_pong_received:145
cluster_stats_messages_meet_received:1
cluster_stats_messages_received:296
192.168.18.128:7002> get school
"sanshui"
192.168.18.128:7002>
redis可视化工具情况:

java连接redis集群情况:
redis集群用redis5.0.5 ,jar包用jedis 2.9,否则报错,
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
package com.water.controller;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.io.IOException;
import java.util.HashSet;
/**
* @author wsj
* @version 1.0
* @description: redis 集群
* @date 2022/1/5 1:27
*/
public class Demo3 {
public static void main(String[] args) throws IOException {
HashSet<HostAndPort> set = new HashSet<>();
//配置IP和端口
set.add(new HostAndPort("192.168.18.128",7001));
set.add(new HostAndPort("192.168.18.128",7002));
set.add(new HostAndPort("192.168.18.128",7003));
set.add(new HostAndPort("192.168.18.128",7004));
set.add(new HostAndPort("192.168.18.128",7005));
set.add(new HostAndPort("192.168.18.128",7006));
//命令测试
JedisCluster jedisCluster = new JedisCluster(set);
jedisCluster.set("test1","hahhhhhh");
System.out.println(jedisCluster.get("test1"));
//linux上set了 这里可以拿
System.out.println(jedisCluster.get("username"));
jedisCluster.close();
}
}
运行结果


startall.sh启动集群服务器脚本
注意:脚本创建完成,需要赋予执行的权限,命令:chmod u+x startall.sh
cd 7001
./bin/redis-server ./redis.conf
cd ..
cd 7002
./bin/redis-server ./redis.conf
cd ..
cd 7003
./bin/redis-server ./redis.conf
cd ..
cd 7004
./bin/redis-server ./redis.conf
cd ..
cd 7005
./bin/redis-server ./redis.conf
cd ..
cd 7006
./bin/redis-server ./redis.conf
cd ..
create-cluster.sh创建redis集群脚本
注意这里因为是redis 5版本 可以直接指定一个节点 启动客户端 创建集群,指令不要错了
cd 7001/bin
./redis-cli --cluster create 192.168.18.128:7001 192.168.18.128:7002 192.168.18.128:7003 192.168.18.128:7004 192.168.18.128:7005 192.168.18.128:7006 --cluster-replicas 1
希望本文能帮到你~