无为清净楼资源网 Design By www.qnjia.com

1. 背景:生产测试后redis中产生大量数据

生产前需要清理reids集群中的数据。、
你看有很多key呢:

使用工具

详解如何清理redis集群的所有数据

使用命令,查看是否有数据:

keys * 

详解如何清理redis集群的所有数据

2. 清理步骤

2.1 任意登录一台redis机器

执行下面脚本:

clear_redis_cluster.sh 10.1.33.101:8001 redis

详解如何清理redis集群的所有数据

执行日志如下:

详解如何清理redis集群的所有数据

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.110:8026 ...
Background append only file rewriting started
READONLY You can't write against a read only replica. 
Clearing 10.1.33.111:8027 ...
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
Clearing 10.1.33.108:8024 ...
Background append only file rewriting started
READONLY You can't write against a read only replica. 
Clearing 10.1.33.104:8020 ...
Background append only file rewriting started
READONLY You can't write against a read only replica. 
Clearing 10.1.33.114:8014 ...
Background append only file rewriting started
OK
Clearing 10.1.33.109:8025 ...
Background append only file rewriting started
READONLY You can't write against a read only replica. 
Clearing 10.1.33.105:8005 ...
Background append only file rewriting started
OK
Clearing 10.1.33.108:8008 ...
Background append only file rewriting started
OK

2.2 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 $"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

这位仁兄的脚本写的特别好。直接执行即可。

2.3 确认删除成功

使用redis工具查看:

详解如何清理redis集群的所有数据

3.清理单机redis

flushall 

4.总结

使用脚本删除redis集群中的数据。
记得地址哦:
https://github.com/eyjian/redis-tools/blob/master/clear_redis_cluster.sh

标签:
清理redis集群数据,redis清理集群

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。