1.查看redis集群信息
[root@database01 ~]# redis-cli -c -h 192.168.0.164 -p 6379
192.168.0.164:6379> auth c2b9d063b86153fd9436fb07d8b3801d9ea40629964c422a22aac5a9eb9af3a1
OK
192.168.0.164:6379> cluster nodes
205e88f0a5984698c09328d1a1b16fd273eb79f2 192.168.0.39:6380@16380 slave 5a308d09f6372ff8a361c0e074c9f57d783dc9c6 0 1676961626000 5 connected
abb2ab96ce5f65d0bc4362aa775274c8090be2af 192.168.0.164:6380@16380 master - 0 1676961627450 7 connected 10923-16383
a745e59c190f629d04905550f341d67dea47758f 192.168.0.39:6379@16379 master - 0 1676961626000 2 connected 5461-10922
5a308d09f6372ff8a361c0e074c9f57d783dc9c6 192.168.0.164:6379@16379 myself,master - 0 1676961627000 1 connected 0-5460
1bdcabe88cface91c23b91c9e92db33e6dde02d1 192.168.0.68:6380@16380 slave a745e59c190f629d04905550f341d67dea47758f 0 1676961626448 6 connected
7c369ee5e691a94e141e0ba44ee86f47d6cbd469 192.168.0.68:6379@16379 slave abb2ab96ce5f65d0bc4362aa775274c8090be2af 0 1676961626000 7 connected
2.关闭redis集群中的三个从节点
可以同时开两个窗口,一个窗口执行命令操作,另一个创建查看集群节点数等
# 命令格式:redis-cli --cluster del-node 节点ip:port 节点ID -a '集群访问密码'
# redis-cli --cluster del-node 192.168.0.39:6380 205e88f0a5984698c09328d1a1b16fd273eb79f2 -a 'c2b9d063b86153fd9436fb07d8b3801d9ea40629964c422a22aac5a9eb9af3a1'
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 205e88f0a5984698c09328d1a1b16fd273eb79f2 from cluster 192.168.0.39:6380
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
redis-cli --cluster del-node 192.168.0.68:6379 7c369ee5e691a94e141e0ba44ee86f47d6cbd469 -a 'c2b9d063b86153fd9436fb07d8b3801d9ea40629964c422a22aac5a9eb9af3a1'
redis-cli --cluster del-node 192.168.0.68:6380 1bdcabe88cface91c23b91c9e92db33e6dde02d1 -a 'c2b9d063b86153fd9436fb07d8b3801d9ea40629964c422a22aac5a9eb9af3a1'
# 从节点退出集群的同时,相应的进程也会退出
3.迁移另外两个主节点slots槽数据到另一个主节点上
可以同时开两个窗口,一个窗口执行命令操作,另一个创建查看集群节点数等
最后只保留192.168.0.164:6379这一个主节点
# 迁移192.168.0.39:6379主节点数据
redis-cli --cluster reshard 192.168.0.39:6379 -a 'c2b9d063b86153fd9436fb07d8b3801d9ea40629964c422a22aac5a9eb9af3a1'
# 会出现一下问答对话项
#需要移动数据槽的数量
How many slots do you want to move (from 1 to 16384)? 5162 ==》这个数字是要填写192.168.0.39:6379 这个主节点的槽位数,上面显示的有
#需要接受数据槽的主节点ID,这里我们填写192.168.0.164:6379这一个主节点ID
What is the receiving node ID? 5a308d09f6372ff8a361c0e074c9f57d783dc9c6
#填写数据槽来源节点
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: a745e59c190f629d04905550f341d67dea47758f =》 这个填写主节点 192.168.0.39:6379的节点ID,也就是要移除的主节点
Source node #2: done =》输入这个开始执行迁移
# 删除这个主节点
redis-cli --cluster del-node 192.168.0.39:6379 a745e59c190f629d04905550f341d67dea47758f -a 'c2b9d063b86153fd9436fb07d8b3801d9ea40629964c422a22aac5a9eb9af3a1'
redis-cli --cluster reshard 192.168.0.164:6380 -a 'c2b9d063b86153fd9436fb07d8b3801d9ea40629964c422a22aac5a9eb9af3a1'
# 删除这个主节点
redis-cli --cluster del-node 192.168.0.164:6380 abb2ab96ce5f65d0bc4362aa775274c8090be2af -a 'c2b9d063b86153fd9436fb07d8b3801d9ea40629964c422a22aac5a9eb9af3a1'
4.登录到最后保留的主节点上,执行save命令
[root@database01 ~]# redis-cli -c -h 192.168.0.164 -p 6379
192.168.0.164:6379> auth c2b9d063b86153fd9436fb07d8b3801d9ea40629964c422a22aac5a9eb9af3a1
OK
192.168.0.164:6379> save
192.168.0.164:6379> CONFIG GET dir => 获取dump.rdp文件保存路径
5.启动新节点的redis,找到dump.rdp文件保存路径后,停止redis,删除该目录下默认生成的dump.rdp文件,把上一步获取到的dump.rdp文件放在这个目录下,注意dump.rdp文件文件的权限,最后再启动redis
登录redis后执行命令:keys *
,查看key的个数是不是跟原先集群中的keys个数一样