The redis cluster deletes the specified key in batch
1. Description
Redis cluster sometimes need to delete multiple keys, you must log in to each node, and it is possible that this key is not in this node, so it is more troublesome to delete, the following provides a convenient way to achieve
2. View the master node in the redis cluster
First, you need to confirm which master nodes there are. You can check them with the following command:
#./ redis-cli cluster nodes | grep master
PS: Here I have 3 master nodes, all port 6379
3. Write redis_del.sh script
#!/ bin/bash
redis_comm=/usr/local/redis/bin/redis-cli
redis_ser01=172.18.18.107
redis_ser02=172.18.18.108
redis_ser03=172.18.18.109
$redis_comm -c -h $redis_ser01 keys $1 | xargs -i ./ redis-cli -h $redis_ser01 del {}
$redis_comm -c -h $redis_ser02 keys $1 | xargs -i ./ redis-cli -h $redis_ser02 del {}
$redis_comm -c -h $redis_ser03 keys $1 | xargs -i ./ redis-cli -h $redis_ser03 del {}
Parameter Description:
-c: Start cluster mode and enter redis cluster service
-h: redis host address
xargs -i: The-i option tells xargs that {} can be used instead of the passed parameter
Use redis_del.sh script
For example, we now have a requirement to delete all keys that start with UP in the redis cluster.
First, let's log in to the redis cluster and see:
Then, use the redis_del.sh script to remove:
# ./ redis_del.sh UP*
PS: As shown above, all keys at the beginning of UP have been deleted.
Script Usage: sh redis_del.sh key parameter
For example, you want to delete other key values:
./ redis_del.sh a* #Remove key values starting with a
./ redis_del.sh b* #Remove key values starting with b
Or, delete the keys value ending with:
./ redis_del.sh *ab #Remove key values ending in ab
./ redis_del.sh *123 #Remove key values ending in 123
...... wait
Well, feel useful friends can forward or collection, everyone has questions can be discussed in the following message, there are better suggestions can also be posted in the comment area!!!