经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Redis » 查看文章
详解如何清理redis集群的所有数据
来源:jb51  时间:2021/2/18 18:21:55  对本文有异议

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

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

使用工具

在这里插入图片描述

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

  1. keys *

在这里插入图片描述

2. 清理步骤

2.1 任意登录一台redis机器

执行下面脚本:

  1. clear_redis_cluster.sh 10.1.33.101:8001 redis

在这里插入图片描述

执行日志如下:

在这里插入图片描述

  1. Clearing 10.1.33.112:8028 ...
  2. Background append only file rewriting started
  3. READONLY You can't write against a read only replica.
  4. Clearing 10.1.33.110:8026 ...
  5. Background append only file rewriting started
  6. READONLY You can't write against a read only replica.
  7. Clearing 10.1.33.111:8027 ...
  8. Background append only file rewriting started
  9. READONLY You can't write against a read only replica.
  10. Clearing 10.1.33.107:8007 ...
  11. Background append only file rewriting started
  12. OK
  13. Clearing 10.1.33.108:8024 ...
  14. Background append only file rewriting started
  15. READONLY You can't write against a read only replica.
  16. Clearing 10.1.33.104:8020 ...
  17. Background append only file rewriting started
  18. READONLY You can't write against a read only replica.
  19. Clearing 10.1.33.114:8014 ...
  20. Background append only file rewriting started
  21. OK
  22. Clearing 10.1.33.109:8025 ...
  23. Background append only file rewriting started
  24. READONLY You can't write against a read only replica.
  25. Clearing 10.1.33.105:8005 ...
  26. Background append only file rewriting started
  27. OK
  28. Clearing 10.1.33.108:8008 ...
  29. Background append only file rewriting started
  30. OK

2.2 clear_redis_cluster.sh内容

  1. #!/bin/bash
  2. # Writed by yijian on 2018/8/20
  3. # Batch to clear all nodes using FLUSHALL command
  4. # 用来清空一个redis集群中的所有数据,要求 FLUSHALL 命令可用,
  5. # 如果在 redis.conf 中使用 rename 改名了 FLUSHALL,则不能执行本脚本。
  6. # 可带两个参数:
  7. # 1)参数1 集群中的任一可用节点(必须)
  8. # 2)连接redis的密码(设置了密码才需要)
  9. REDIS_CLI=${REDIS_CLI:-redis-cli}
  10. REDIS_IP=${REDIS_IP:-127.0.0.1}
  11. REDIS_PORT=${REDIS_PORT:-6379}
  12.  
  13. # 显示用法函数
  14. function usage()
  15. {
  16. echo "Usage: clear_redis_cluster.sh a_redis_node_of_cluster redis_password"
  17. echo "Example1: clear_redis_cluster.sh '127.0.0.1:6379'"
  18. echo "Example2: clear_redis_cluster.sh '127.0.0.1:6379' '123456'"
  19. }
  20.  
  21. # 检查参数个数
  22. if test $# -lt 1 -o $# -gt 2; then
  23. usage
  24. exit 1
  25. fi
  26.  
  27. # 第一个参数为集群中的节点,
  28. REDIS_NODE="$1"
  29. # 第二个参数为密码
  30. REDIS_PASSWORD=""
  31. if test $# -ge 2; then
  32. REDIS_PASSWORD="$2"
  33. fi
  34.  
  35. # 取得IP和端口
  36. eval $(echo "$REDIS_NODE" | awk -F[\:] '{ printf("REDIS_IP=%s\nREDIS_PORT=%s\n",$1,$2) }')
  37. if test -z "$REDIS_IP" -o -z "$REDIS_PORT"; then
  38. echo "Parameter error: \`$REDIS_NODE\`."
  39. usage
  40. exit 1
  41. fi
  42.  
  43. # 确保redis-cli可用
  44. echo "Checking \`redis-cli\` ..."
  45. which "$REDIS_CLI" > /dev/null 2>&1
  46. if test $? -ne 0; then
  47. echo "Command \`redis-cli\` is not exists or not executable."
  48. echo "You can set environment variable \`REDIS_CLI\` to point to the redis-cli."
  49. echo "Example: export REDIS_CLI=/usr/local/bin/redis-cli"
  50. exit 1
  51. fi
  52.  
  53. if test -z "$REDIS_PASSWORD"; then
  54. redis_nodes=`redis-cli -h $REDIS_IP -p $REDIS_PORT cluster nodes | awk -F[\ \:\@] '!/ERR/{ printf("%s:%s\n",$2,$3); }'`
  55. else
  56. 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); }'`
  57. fi
  58. if test -z "$redis_nodes"; then
  59. # Standlone(非集群)
  60. if test -z "$REDIS_PASSWORD"; then
  61. $REDIS_CLI -h $REDIS_IP -p $REDIS_PORT FLUSHALL ASYNC
  62. $REDIS_CLI -h $REDIS_IP -p $REDIS_PORT BGREWRITEAOF
  63. else
  64. $REDIS_CLI --no-auth-warning -a "$REDIS_PASSWORD" -h $REDIS_IP -p $REDIS_PORT FLUSHALL ASYNC
  65. $REDIS_CLI --no-auth-warning -a "$REDIS_PASSWORD" -h $REDIS_IP -p $REDIS_PORT BGREWRITEAOF
  66. fi
  67. else
  68. # Cluster(集群)
  69. for redis_node in $redis_nodes;
  70. do
  71. if test ! -z "$redis_node"; then
  72. eval $(echo "$redis_node" | awk -F[\:] '{ printf("redis_node_ip=%s\nredis_node_port=%s\n",$1,$2) }')
  73.  
  74. if test ! -z "$redis_node_ip" -a ! -z "$redis_node_port"; then
  75. # clear
  76. echo -e "Clearing \033[1;33m${redis_node_ip}:${redis_node_port}\033[m ..."
  77. if test -z "$REDIS_PASSWORD"; then
  78. result=`$REDIS_CLI -h $redis_node_ip -p $redis_node_port FLUSHALL ASYNC`
  79. $REDIS_CLI -h $redis_node_ip -p $redis_node_port BGREWRITEAOF
  80. else
  81. result=`$REDIS_CLI --no-auth-warning -a "$REDIS_PASSWORD" -h $redis_node_ip -p $redis_node_port FLUSHALL ASYNC`
  82. $REDIS_CLI --no-auth-warning -a "$REDIS_PASSWORD" -h $redis_node_ip -p $redis_node_port BGREWRITEAOF
  83. fi
  84.  
  85. if test ! -z "$result"; then
  86. # SUCCESS
  87. if test "$result" = "OK"; then
  88. echo -e "\033[0;32;32m$result\033[m"
  89. else
  90. echo -e "\033[0;32;31m$result\033[m"
  91. fi
  92. fi
  93. fi
  94. fi
  95. done
  96. fi

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

2.3 确认删除成功

使用redis工具查看:

在这里插入图片描述

3.清理单机redis

  1. flushall

4.总结

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

到此这篇关于详解如何清理redis集群的所有数据的文章就介绍到这了,更多相关清理redis集群数据内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号