当前位置: 首页 > 工具软件 > credis for C > 使用案例 >

误用redis cluster forget删除节点,如何把节点加回来?【详细步骤】

冯飞鹏
2023-12-01

问题

如果你在使用cluster forget操作的时候,误删了某个节点。该如何恢复呢?

本例中演示的集群有3个master节点

$ redis-cli -c -h 192.168.1.196 -p 6379 -a xxxxxx cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
bfc47617d26387e800c1ff1b714d25175caaaa60 192.168.1.196:6579@16579 master - 0 1640674351105 6 connected 6554-12014
74c8b8101bc810e82d98926c9d0f60b8f1b5d163 192.168.1.196:6479@16479 master - 0 1640674349099 8 connected 3277-6553 12560-13106 14746-16383
d1851a905c4daf870dc9ca7c28c3ebe29585af07 192.168.1.196:6379@16379 myself,master - 0 1640674350000 7 connected 0-3276 12015-12559 13107-14745

先用cluster forget操作删掉一个:

$ redis-cli -c -h 192.168.1.196 -p 6379 -a xxxxx cluster forget bfc47617d26387e800c1ff1b714d25175caaaa60
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK

现在只剩下2个节点:

$ redis-cli -c -h 192.168.1.196 -p 6379 -a xxxxx cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
74c8b8101bc810e82d98926c9d0f60b8f1b5d163 192.168.1.196:6479@16479 master - 0 1640674488571 8 connected 3277-6553 12560-13106 14746-16383
d1851a905c4daf870dc9ca7c28c3ebe29585af07 192.168.1.196:6379@16379 myself,master - 0 1640674486000 7 connected 0-3276 12015-12559 13107-14745

整个集群都不好了:

$ redis-cli --cluster check 192.168.1.196:6379 -a xxxxxx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.196:6379 (d1851a90...) -> 41303 keys | 5461 slots | 0 slaves.
192.168.1.196:6479 (74c8b810...) -> 41365 keys | 5462 slots | 0 slaves.
[OK] 82668 keys in 2 masters.
5.05 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.196:6379)
M: d1851a905c4daf870dc9ca7c28c3ebe29585af07 192.168.1.196:6379
   slots:[0-3276],[12015-12559],[13107-14745] (5461 slots) master
M: 74c8b8101bc810e82d98926c9d0f60b8f1b5d163 192.168.1.196:6479
   slots:[3277-6553],[12560-13106],[14746-16383] (5462 slots) master
[ERR] Nodes don't agree about configuration!
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.

解决

可以使用cluster meet 命令

$ redis-cli -c -h 192.168.1.196 -p 6379 -a xxxxxx cluster meet 192.168.1.196 6579
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK

命令参数说明:

参数说明
-c集群模式登录
-h 192.168.1.196任意节点ip地址
-p 6379登录节点的端口
-a xxxxxx密码
cluster meetcluster meet 命令
192.168.1.196 6579要增加的节点的ip地址和端口

检查

$ redis-cli --cluster check 192.168.1.196:6379 -a xxxxxx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.196:6379 (d1851a90...) -> 41303 keys | 5461 slots | 0 slaves.
192.168.1.196:6579 (bfc47617...) -> 41090 keys | 5461 slots | 0 slaves.
192.168.1.196:6479 (74c8b810...) -> 41365 keys | 5462 slots | 0 slaves.
[OK] 123758 keys in 3 masters.
7.55 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.196:6379)
M: d1851a905c4daf870dc9ca7c28c3ebe29585af07 192.168.1.196:6379
   slots:[0-3276],[12015-12559],[13107-14745] (5461 slots) master
M: bfc47617d26387e800c1ff1b714d25175caaaa60 192.168.1.196:6579
   slots:[6554-12014] (5461 slots) master
M: 74c8b8101bc810e82d98926c9d0f60b8f1b5d163 192.168.1.196:6479
   slots:[3277-6553],[12560-13106],[14746-16383] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

被删掉的节点又回来了。

 类似资料: