从redis集群查key比较麻烦,要到每个master上面都查一遍,删除的时候也比较麻烦,xargs的方式并不适合集群的批量删除key,所以写了如下脚本,方便批量查询和删除key:
#!/bin/bash
# 查询 sh redis.sh keys "*_courseList_*"
# 删除 sh redis.sh del "*_courseList_*"
cluster="192.168.50.199:7000,192.168.50.199:7001,192.168.50.199:7002,192.168.50.199:7003,192.168.50.199:7004,192.168.50.199:7005"
PW=""
REDIS_RUN(){
COMMAND=$1
KEY="$2"
for node in $(echo $cluster|sed 's/,/ /g');
do
ip=$(echo $node|awk -F\: '{print $1}')
port=$(echo $node|awk -F\: '{print $2}')
echo $ip $port $COMMAND $KEY
redis-cli -h $ip -p $port -a "$PW" -c $COMMAND "$KEY" 2>/dev/null
done
}
if [[ x$1 == xkeys ]];then
REDIS_RUN "keys" "$2"
fi
if [[ x$1 == xdel ]];then
REDIS_RUN "keys" "$2" |egrep -v " keys |^$"|sort|uniq|while read line;do REDIS_RUN "del" "$line";done
fi
评论区