一、背景说明
生产测试后,redis 集群中产生大量测试数据。为确保正式上线前环境干净,需在低峰期对 redis 集群进行全量数据清理。
二、操作前检查
2.1 查看集群中是否存在数据
使用 redis 工具或命令行连接任意节点,执行:
keys *
注意:生产环境建议使用 scan 替代 keys *,避免阻塞。
三、清理步骤
3.1 登录任意一台 redis 节点
执行清理脚本:
./clear_redis_cluster.sh 10.1.33.101:8001 redis
- 参数1:集群中任意节点地址(ip:port)
- 参数2:redis 密码(如无密码可省略)
3.2 脚本执行日志示例
clearing 10.1.33.112:8028 ... background append only file rewriting started readonly you can't write against a read only replica. clearing 10.1.33.107:8007 ... background append only file rewriting started ok
说明:
- ok 表示该节点清理成功
- readonly 表示该节点为从节点(slave),无需处理,主节点清空后自动同步
3.3 脚本内容(clear_redis_cluster.sh)
#!/bin/bash
# writed by yijian on 2018/8/20
# batch to clear all nodes using flushall command
# 用来清空一个redis集群中的所有数据,要求 flushall 命令可用,
# 如果在 redis.conf 中使用 rename 改名了 flushall,则不能执行本脚本。
# 可带两个参数:
# 1)参数1 集群中的任一可用节点(必须)
# 2)连接redis的密码(设置了密码才需要)
redis_cli=${redis_cli:-redis-cli}
redis_ip=${redis_ip:-127.0.0.1}
redis_port=${redis_port:-6379}
# 显示用法函数
function usage()
{
echo "usage: clear_redis_cluster.sh a_redis_node_of_cluster redis_password"
echo "example1: clear_redis_cluster.sh '127.0.0.1:6379'"
echo "example2: clear_redis_cluster.sh '127.0.0.1:6379' '123456'"
}
# 检查参数个数
if test $# -lt 1 -o $# -gt 2; then
usage
exit 1
fi
# 第一个参数为集群中的节点,
redis_node="$1"
# 第二个参数为密码
redis_password=""
if test $# -ge 2; then
redis_password="$2"
fi
# 取得ip和端口
eval $(echo "$redis_node" | awk -f[\:] '{ printf("redis_ip=%s\nredis_port=%s\n",$1,$2) }')
if test -z "$redis_ip" -o -z "$redis_port"; then
echo "parameter error: \`$redis_node\`."
usage
exit 1
fi
# 确保redis-cli可用
echo "checking \`redis-cli\` ..."
which "$redis_cli" > /dev/null 2>&1
if test $? -ne 0; then
echo "command \`redis-cli\` is not exists or not executable."
echo "you can set environment variable \`redis_cli\` to point to the redis-cli."
echo "example: export redis_cli=/usr/local/bin/redis-cli"
exit 1
fi
if test -z "$redis_password"; then
redis_nodes=`redis-cli -h $redis_ip -p $redis_port cluster nodes | awk -f[\ \:\@] '!/err/{ printf("%s:%s\n",$2,$3); }'`
else
redis_nodes=`redis-cli --no-auth-warning -a "$redis_password" -h $redis_ip -p $redis_port cluster nodes | awk -f[\ \:\@] '!/err/{ printf("%s:%s\n",$2,$3); }'`
fi
if test -z "$redis_nodes"; then
# standlone(非集群)
if test -z "$redis_password"; then
$redis_cli -h $redis_ip -p $redis_port flushall async
$redis_cli -h $redis_ip -p $redis_port bgrewriteaof
else
$redis_cli --no-auth-warning -a "$redis_password" -h $redis_ip -p $redis_port flushall async
$redis_cli --no-auth-warning -a "$redis_password" -h $redis_ip -p $redis_port bgrewriteaof
fi
else
# cluster(集群)
for redis_node in $redis_nodes;
do
if test ! -z "$redis_node"; then
eval $(echo "$redis_node" | awk -f[\:] '{ printf("redis_node_ip=%s\nredis_node_port=%s\n",$1,$2) }')
if test ! -z "$redis_node_ip" -a ! -z "$redis_node_port"; then
# clear
echo -e "clearing \033[1;33m${redis_node_ip}:${redis_node_port}\033[m ..."
if test -z "$redis_password"; then
result=`$redis_cli -h $redis_node_ip -p $redis_node_port flushall async`
$redis_cli -h $redis_node_ip -p $redis_node_port bgrewriteaof
else
result=`$redis_cli --no-auth-warning -a "$redis_password" -h $redis_node_ip -p $redis_node_port flushall async`
$redis_cli --no-auth-warning -a "$redis_password" -h $redis_node_ip -p $redis_node_port bgrewriteaof
fi
if test ! -z "$result"; then
# success
if test "$result" = "ok"; then
echo -e "\033[0;32;32m$result\033[m"
else
echo -e "\033[0;32;31m$result\033[m"
fi
fi
fi
fi
done
fi
四、清理结果确认
4.1 使用 redis 客户端工具查看
连接任意节点,执行:
dbsize
返回 0 表示清理成功。
五、单机 redis 清理(非集群)
若为非集群模式,直接执行:
flushall
六、总结与建议
- ✅ 使用脚本可快速清理整个 redis 集群数据
- ✅ 仅对主节点执行
flushall,从节点自动同步 - ✅ 建议在凌晨低峰期执行,避免影响业务
- ✅ 执行前建议备份 rdb 文件(可选)
到此这篇关于redis集群数据清理的操作指南的文章就介绍到这了,更多相关redis集群数据清理内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论