{"id":201157,"date":"2025-05-10T09:31:35","date_gmt":"2025-05-10T01:31:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201157\/"},"modified":"2025-05-10T09:31:35","modified_gmt":"2025-05-10T01:31:35","slug":"%e8%af%a6%e8%a7%a3%e5%a6%82%e4%bd%95%e6%b8%85%e7%90%86redis%e9%9b%86%e7%be%a4%e7%9a%84%e6%89%80%e6%9c%89%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201157\/","title":{"rendered":"\u8be6\u89e3\u5982\u4f55\u6e05\u7406redis\u96c6\u7fa4\u7684\u6240\u6709\u6570\u636e"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u8be6\u89e3\u5982\u4f55\u6e05\u7406redis\u96c6\u7fa4\u7684\u6240\u6709\u6570\u636e<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u8be6\u89e3\u5982\u4f55\u6e05\u7406redis\u96c6\u7fa4\u7684\u6240\u6709\u6570\u636e\u300b\uff0c\u804a\u804aredis\u6e05\u7406\u3001\u96c6\u7fa4\u6570\u636e\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<h2>1. \u80cc\u666f\uff1a\u751f\u4ea7\u6d4b\u8bd5\u540eredis\u4e2d\u4ea7\u751f\u5927\u91cf\u6570\u636e<\/h2>\n<p>\u751f\u4ea7\u524d\u9700\u8981\u6e05\u7406reids\u96c6\u7fa4\u4e2d\u7684\u6570\u636e\u3002\u3001<br \/> \u4f60\u770b\u6709\u5f88\u591akey\u5462\uff1a<\/p>\n<p>\u4f7f\u7528\u5de5\u5177<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167239800663aec4b68393d.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4f7f\u7528\u547d\u4ee4\uff0c\u67e5\u770b\u662f\u5426\u6709\u6570\u636e\uff1a<\/p>\n<pre>\nkeys * <\/pre>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167239800763aec4b747666.jpg\" class=\"aligncenter\"><\/p>\n<h2>2. \u6e05\u7406\u6b65\u9aa4<\/h2>\n<h3>2.1 \u4efb\u610f\u767b\u5f55\u4e00\u53f0redis\u673a\u5668<\/h3>\n<p>\u6267\u884c\u4e0b\u9762\u811a\u672c\uff1a<\/p>\n<pre>\nclear_redis_cluster.sh 10.1.33.101:8001 redis<\/pre>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167239800763aec4b7edfe3.jpg\" class=\"aligncenter\"><\/p>\n<p>\u6267\u884c\u65e5\u5fd7\u5982\u4e0b\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167239801063aec4ba13c11.jpg\" class=\"aligncenter\"><\/p>\n<pre>\nClearing 10.1.33.112:8028 ...\nBackground append only file rewriting started\nREADONLY You can't write against a read only replica. \nClearing 10.1.33.110:8026 ...\nBackground append only file rewriting started\nREADONLY You can't write against a read only replica. \nClearing 10.1.33.111:8027 ...\nBackground append only file rewriting started\nREADONLY You can't write against a read only replica. \nClearing 10.1.33.107:8007 ...\nBackground append only file rewriting started\nOK\nClearing 10.1.33.108:8024 ...\nBackground append only file rewriting started\nREADONLY You can't write against a read only replica. \nClearing 10.1.33.104:8020 ...\nBackground append only file rewriting started\nREADONLY You can't write against a read only replica. \nClearing 10.1.33.114:8014 ...\nBackground append only file rewriting started\nOK\nClearing 10.1.33.109:8025 ...\nBackground append only file rewriting started\nREADONLY You can't write against a read only replica. \nClearing 10.1.33.105:8005 ...\nBackground append only file rewriting started\nOK\nClearing 10.1.33.108:8008 ...\nBackground append only file rewriting started\nOK<\/pre>\n<h3>2.2 clear_redis_cluster.sh\u5185\u5bb9<\/h3>\n<pre>\n#!\/bin\/bash\n# Writed by yijian on 2018\/8\/20\n# Batch to clear all nodes using FLUSHALL command\n# \u7528\u6765\u6e05\u7a7a\u4e00\u4e2aredis\u96c6\u7fa4\u4e2d\u7684\u6240\u6709\u6570\u636e\uff0c\u8981\u6c42 FLUSHALL \u547d\u4ee4\u53ef\u7528\uff0c\n# \u5982\u679c\u5728 redis.conf \u4e2d\u4f7f\u7528 rename \u6539\u540d\u4e86 FLUSHALL\uff0c\u5219\u4e0d\u80fd\u6267\u884c\u672c\u811a\u672c\u3002\n# \u53ef\u5e26\u4e24\u4e2a\u53c2\u6570\uff1a\n# 1\uff09\u53c2\u65701 \u96c6\u7fa4\u4e2d\u7684\u4efb\u4e00\u53ef\u7528\u8282\u70b9\uff08\u5fc5\u987b\uff09\n# 2\uff09\u8fde\u63a5redis\u7684\u5bc6\u7801\uff08\u8bbe\u7f6e\u4e86\u5bc6\u7801\u624d\u9700\u8981\uff09\nREDIS_CLI=${REDIS_CLI:-redis-cli}\nREDIS_IP=${REDIS_IP:-127.0.0.1}\nREDIS_PORT=${REDIS_PORT:-6379}\n\n# \u663e\u793a\u7528\u6cd5\u51fd\u6570\nfunction usage()\n{\n  echo \"Usage: clear_redis_cluster.sh a_redis_node_of_cluster redis_password\"\n  echo \"Example1: clear_redis_cluster.sh '127.0.0.1:6379'\"\n  echo \"Example2: clear_redis_cluster.sh '127.0.0.1:6379' '123456'\"\n}\n\n# \u68c0\u67e5\u53c2\u6570\u4e2a\u6570\nif test $# -lt 1 -o $# -gt 2; then\n  usage\n  exit 1\nfi\n\n# \u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u96c6\u7fa4\u4e2d\u7684\u8282\u70b9\uff0c\nREDIS_NODE=\"$1\"\n# \u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u5bc6\u7801\nREDIS_PASSWORD=\"\"\nif test $# -ge 2; then\n  REDIS_PASSWORD=\"$2\"\nfi\n\n# \u53d6\u5f97IP\u548c\u7aef\u53e3\neval $(echo \"$REDIS_NODE\" | awk -F[\\:] '{ printf(\"REDIS_IP=%s\\nREDIS_PORT=%s\\n\",$1,$2) }')\nif test -z \"$REDIS_IP\" -o -z \"$REDIS_PORT\"; then\n  echo \"Parameter error: \\`$REDIS_NODE\\`.\"\n  usage\n  exit 1\nfi\n\n# \u786e\u4fddredis-cli\u53ef\u7528\necho \"Checking \\`redis-cli\\` ...\"\nwhich \"$REDIS_CLI\" &gt; \/dev\/null 2&gt;&amp;1\nif test $? -ne 0; then\n  echo \"Command \\`redis-cli\\` is not exists or not executable.\"\n  echo \"You can set environment variable \\`REDIS_CLI\\` to point to the redis-cli.\"\n  echo \"Example: export REDIS_CLI=\/usr\/local\/bin\/redis-cli\"\n  exit 1\nfi\n\nif test -z \"$REDIS_PASSWORD\"; then\n  redis_nodes=`redis-cli -h $REDIS_IP -p $REDIS_PORT cluster nodes | awk -F[\\ \\:\\@] '!\/ERR\/{ printf(\"%s:%s\\n\",$2,$3); }'`\nelse\n  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); }'`\nfi\nif test -z \"$redis_nodes\"; then\n  # Standlone\uff08\u975e\u96c6\u7fa4\uff09\n  if test -z \"$REDIS_PASSWORD\"; then\n    $REDIS_CLI -h $REDIS_IP -p $REDIS_PORT FLUSHALL ASYNC\n    $REDIS_CLI -h $REDIS_IP -p $REDIS_PORT BGREWRITEAOF\n  else\n    $REDIS_CLI --no-auth-warning -a \"$REDIS_PASSWORD\" -h $REDIS_IP -p $REDIS_PORT FLUSHALL ASYNC\n    $REDIS_CLI --no-auth-warning -a \"$REDIS_PASSWORD\" -h $REDIS_IP -p $REDIS_PORT BGREWRITEAOF\n  fi\nelse\n  # Cluster\uff08\u96c6\u7fa4\uff09\n  for redis_node in $redis_nodes;\n  do\n    if test ! -z \"$redis_node\"; then\n      eval $(echo \"$redis_node\" | awk -F[\\:] '{ printf(\"redis_node_ip=%s\\nredis_node_port=%s\\n\",$1,$2) }')\n\n      if test ! -z \"$redis_node_ip\" -a ! -z \"$redis_node_port\"; then\n        # clear\n        echo -e \"Clearing \\033[1;33m${redis_node_ip}:${redis_node_port}\\033[m ...\"\n        if test -z \"$REDIS_PASSWORD\"; then\n          result=`$REDIS_CLI -h $redis_node_ip -p $redis_node_port FLUSHALL ASYNC`\n          $REDIS_CLI -h $redis_node_ip -p $redis_node_port BGREWRITEAOF\n        else\n          result=`$REDIS_CLI --no-auth-warning -a \"$REDIS_PASSWORD\" -h $redis_node_ip -p $redis_node_port FLUSHALL ASYNC`\n          $REDIS_CLI --no-auth-warning -a \"$REDIS_PASSWORD\" -h $redis_node_ip -p $redis_node_port BGREWRITEAOF\n        fi\n\n        if test ! -z \"$result\"; then\n          # SUCCESS\n          if test \"$result\" = \"OK\"; then\n            echo -e \"\\033[0;32;32m$result\\033[m\"\n          else\n            echo -e \"\\033[0;32;31m$result\\033[m\"\n          fi\n        fi\n      fi\n    fi\n  done\nfi\n<\/pre>\n<p>\u8fd9\u4f4d\u4ec1\u5144\u7684\u811a\u672c\u5199\u7684\u7279\u522b\u597d\u3002\u76f4\u63a5\u6267\u884c\u5373\u53ef\u3002<\/p>\n<h3>2.3 \u786e\u8ba4\u5220\u9664\u6210\u529f<\/h3>\n<p>\u4f7f\u7528redis\u5de5\u5177\u67e5\u770b\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167239801063aec4baad5da.jpg\" class=\"aligncenter\"><\/p>\n<h2>3.\u6e05\u7406\u5355\u673aredis<\/h2>\n<pre>\nflushall <\/pre>\n<h2>4.\u603b\u7ed3<\/h2>\n<p>\u4f7f\u7528\u811a\u672c\u5220\u9664redis\u96c6\u7fa4\u4e2d\u7684\u6570\u636e\u3002<br \/> \u8bb0\u5f97\u5730\u5740\u54e6\uff1a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8be6\u89e3\u5982\u4f55\u6e05\u7406redis\u96c6\u7fa4\u7684\u6240\u6709&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201157","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201157"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201157\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}