Redis集群数据清理的操作指南
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li><a href="#_label0">一、背景说明</a></li><li><a href="#_label1">二、操作前检查</a></li><ul class="second_class_ul"><li><a href="#_lab2_1_0">2.1 查看集群中是否存在数据</a></li></ul><li><a href="#_label2">三、清理步骤</a></li><ul class="second_class_ul"><li><a href="#_lab2_2_1">3.1 登录任意一台 Redis 节点</a></li><li><a href="#_lab2_2_2">3.2 脚本执行日志示例</a></li><li><a href="#_lab2_2_3">3.3 脚本内容(clear_redis_cluster.sh)</a></li></ul><li><a href="#_label3">四、清理结果确认</a></li><ul class="second_class_ul"><li><a href="#_lab2_3_4">4.1 使用 Redis 客户端工具查看</a></li></ul><li><a href="#_label4">五、单机 Redis 清理(非集群)</a></li><ul class="second_class_ul"></ul><li><a href="#_label5">六、总结与建议</a></li><ul class="second_class_ul"></ul></ul></div><p class="maodian"><a name="_label0"></a></p><h2>一、背景说明</h2><p>生产测试后,Redis 集群中产生大量测试数据。为确保正式上线前环境干净,需在<strong>低峰期</strong>对 Redis 集群进行<strong>全量数据清理</strong>。</p>
<p class="maodian"><a name="_label1"></a></p><h2>二、操作前检查</h2>
<p class="maodian"><a name="_lab2_1_0"></a></p><h3>2.1 查看集群中是否存在数据</h3>
<p>使用 Redis 工具或命令行连接任意节点,执行:</p>
<div class="jb51code"><pre class="brush:sql;">keys *
</pre></div>
<blockquote><p>注意:生产环境建议使用 SCAN 替代 KEYS *,避免阻塞。</p></blockquote>
<p class="maodian"><a name="_label2"></a></p><h2>三、清理步骤</h2>
<p class="maodian"><a name="_lab2_2_1"></a></p><h3>3.1 登录任意一台 Redis 节点</h3>
<p>执行清理脚本:</p>
<div class="jb51code"><pre class="brush:bash;">./clear_redis_cluster.sh 10.1.33.101:8001 redis
</pre></div>
<ul><li>参数1:集群中任意节点地址(IP:PORT)</li><li>参数2:Redis 密码(如无密码可省略)</li></ul>
<p class="maodian"><a name="_lab2_2_2"></a></p><h3>3.2 脚本执行日志示例</h3>
<div class="jb51code"><pre class="brush:bash;">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
</pre></div>
<blockquote><p>说明:</p>
<ul><li>OK 表示该节点清理成功</li><li>READONLY 表示该节点为从节点(slave),无需处理,主节点清空后自动同步</li></ul></blockquote>
<p class="maodian"><a name="_lab2_2_3"></a></p><h3>3.3 脚本内容(clear_redis_cluster.sh)</h3>
<div class="jb51code"><pre class="brush:bash;">#!/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
</pre></div>
<p class="maodian"><a name="_label3"></a></p><h2>四、清理结果确认</h2>
<p class="maodian"><a name="_lab2_3_4"></a></p><h3>4.1 使用 Redis 客户端工具查看</h3>
<p>连接任意节点,执行:</p>
<div class="jb51code"><pre class="brush:bash;">DBSIZE
</pre></div>
<p>返回 <code>0</code> 表示清理成功。</p>
<p class="maodian"><a name="_label4"></a></p><h2>五、单机 Redis 清理(非集群)</h2>
<p>若为非集群模式,直接执行:</p>
<div class="jb51code"><pre class="brush:bash;">FLUSHALL
</pre></div>
<p class="maodian"><a name="_label5"></a></p><h2>六、总结与建议</h2>
<ul><li>✅ 使用脚本可快速清理整个 Redis 集群数据</li><li>✅ 仅对主节点执行 <code>FLUSHALL</code>,从节点自动同步</li><li>✅ 建议在<strong>凌晨低峰期</strong>执行,避免影响业务</li><li>✅ 执行前建议备份 RDB 文件(可选)</li></ul>
頁:
[1]