1. 背景:生產(chǎn)測試后redis中產(chǎn)生大量數(shù)據(jù)
生產(chǎn)前需要清理reids集群中的數(shù)據(jù)。、
你看有很多key呢:
使用工具
使用命令,查看是否有數(shù)據(jù):
1
|
keys * |
2. 清理步驟
2.1 任意登錄一臺redis機(jī)器
執(zhí)行下面腳本:
1
|
clear_redis_cluster.sh 10.1.33.101:8001 redis |
執(zhí)行日志如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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)容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#!/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é)點(diǎn)(必須) # 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é)點(diǎn), 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.清理單機(jī)redis
1
|
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)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/fsjwin/article/details/111300274