欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解如何清理redis集群的所有數(shù)據(jù)

 更新時間:2021年02月18日 16:05:10   作者:前路無畏  
這篇文章主要介紹了詳解如何清理redis集群的所有數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1. 背景:生產(chǎn)測試后redis中產(chǎn)生大量數(shù)據(jù)

生產(chǎn)前需要清理reids集群中的數(shù)據(jù)。、
你看有很多key呢:

使用工具

在這里插入圖片描述

使用命令,查看是否有數(shù)據(jù):

keys * 

在這里插入圖片描述

2. 清理步驟

2.1 任意登錄一臺redis機器

執(zhí)行下面腳本:

clear_redis_cluster.sh 10.1.33.101:8001 redis

在這里插入圖片描述

執(zhí)行日志如下:

在這里插入圖片描述

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內(nèi)容

#!/bin/bash
# Writed by yijian on 2018/8/20
# Batch to clear all nodes using FLUSHALL command
# 用來清空一個redis集群中的所有數(shù)據(jù),要求 FLUSHALL 命令可用,
# 如果在 redis.conf 中使用 rename 改名了 FLUSHALL,則不能執(zhí)行本腳本。
# 可帶兩個參數(shù):
# 1)參數(shù)1 集群中的任一可用節(jié)點(必須)
# 2)連接redis的密碼(設(shè)置了密碼才需要)
REDIS_CLI=${REDIS_CLI:-redis-cli}
REDIS_IP=${REDIS_IP:-127.0.0.1}
REDIS_PORT=${REDIS_PORT:-6379}

# 顯示用法函數(shù)
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'"
}

# 檢查參數(shù)個數(shù)
if test $# -lt 1 -o $# -gt 2; then
  usage
  exit 1
fi

# 第一個參數(shù)為集群中的節(jié)點,
REDIS_NODE="$1"
# 第二個參數(shù)為密碼
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

這位仁兄的腳本寫的特別好。直接執(zhí)行即可。

2.3 確認(rèn)刪除成功

使用redis工具查看:

在這里插入圖片描述

3.清理單機redis

flushall 

4.總結(jié)

使用腳本刪除redis集群中的數(shù)據(jù)。
記得地址哦:
https://github.com/eyjian/redis-tools/blob/master/clear_redis_cluster.sh

到此這篇關(guān)于詳解如何清理redis集群的所有數(shù)據(jù)的文章就介紹到這了,更多相關(guān)清理redis集群數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Redis中key的操作命令

    Redis中key的操作命令

    本文主要介紹了Redis中key的操作命令,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • Redis?SortedSet數(shù)據(jù)類型及其常用命令總結(jié)

    Redis?SortedSet數(shù)據(jù)類型及其常用命令總結(jié)

    Redis的SortedSet是一個可排序的set集合,與Java中的TreeSet有些類似,但底層數(shù)據(jù)結(jié)構(gòu)卻差別很大,這篇文章主要介紹了Redis?SortedSet數(shù)據(jù)類型及其常用命令詳解,需要的朋友可以參考下
    2024-06-06
  • Redis中一些最常見的面試問題總結(jié)

    Redis中一些最常見的面試問題總結(jié)

    Redis在互聯(lián)網(wǎng)技術(shù)存儲方面使用如此廣泛,幾乎所有的后端技術(shù)面試官都要在Redis的使用和原理方面對小伙伴們進(jìn)行各種刁難。下面這篇文章主要給大家總結(jié)介紹了關(guān)于Redis中一些最常見的面試問題,需要的朋友可以參考下
    2018-09-09
  • Redis搶單預(yù)熱的實現(xiàn)示例

    Redis搶單預(yù)熱的實現(xiàn)示例

    本文主要介紹了Redis搶單預(yù)熱的實現(xiàn)示例,以應(yīng)對搶單活動帶來的高并發(fā)訪問壓力,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • Redis如何使用lua腳本實例教程

    Redis如何使用lua腳本實例教程

    這篇文章主要給大家介紹了關(guān)于Redis如何使用lua腳本的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • redis清空所有數(shù)據(jù)的三種方法

    redis清空所有數(shù)據(jù)的三種方法

    本文主要介紹了redis清空所有數(shù)據(jù)的三種方法,主要包括FLUSHALL,FLUSHDB,SCREPT FLUSH這三個指令,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • Redis內(nèi)存空間占用及避免數(shù)據(jù)丟失的方法

    Redis內(nèi)存空間占用及避免數(shù)據(jù)丟失的方法

    在現(xiàn)代的互聯(lián)網(wǎng)應(yīng)用中,Redis作為一種高性能的內(nèi)存數(shù)據(jù)庫,被廣泛應(yīng)用于緩存、會話管理和消息隊列等場景,然而,Redis的內(nèi)存資源是有限的,過多的內(nèi)存占用可能會導(dǎo)致數(shù)據(jù)丟失所以本文將給大家介紹一下Redis內(nèi)存空間占用及避免數(shù)據(jù)丟失的方法
    2023-08-08
  • Redis命令處理過程源碼解析

    Redis命令處理過程源碼解析

    這篇文章主要介紹了Redis命令處理過程源碼解析,本文是基于社區(qū)版redis4.0.8,通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • Redis中的數(shù)據(jù)結(jié)構(gòu)跳表詳解

    Redis中的數(shù)據(jù)結(jié)構(gòu)跳表詳解

    跳表是一種基于并聯(lián)的鏈表結(jié)構(gòu),用于在有序元素序列中快速查找元素的數(shù)據(jù)結(jié)構(gòu),本文給大家介紹Redis中的數(shù)據(jù)結(jié)構(gòu)跳表,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • Redis實戰(zhàn)記錄之限制操作頻率

    Redis實戰(zhàn)記錄之限制操作頻率

    這篇文章主要給大家介紹了關(guān)于Redis實戰(zhàn)記錄之限制操作頻率的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06

最新評論