From bfc862fec8b24bf65b9bb075f13ed19d19d88ac1 Mon Sep 17 00:00:00 2001 From: yovinchen Date: Fri, 19 Jul 2024 14:11:27 +0800 Subject: [PATCH] no message --- Redis 集群命令.md | 374 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 374 insertions(+) create mode 100644 Redis 集群命令.md diff --git a/Redis 集群命令.md b/Redis 集群命令.md new file mode 100644 index 0000000..1d2fd79 --- /dev/null +++ b/Redis 集群命令.md @@ -0,0 +1,374 @@ +# Redis 集群命令 + +## 节点管理命令 + +1. ### 获取集群信息 + +```shell +cluster info +``` + +2. ### 列出集群中所有节点的信息 + +```shell +cluster nodes +``` + +3. ### 将一个新节点加入集群中 + +```shell +cluster meet +``` + +4. ### 从集群中移除一个节点 + +```shell +cluster forget +``` + +5. ### 将一个节点设置为另一个节点的从节点 + +```shell +cluster replicate +``` + +6. ### 在主从之间切换角色 + +```shell +cluster failover +``` + +7. ### 重置集群节点的状态 + +```shell +cluster reset hard +``` + +## 插槽管理命令 + +1. ### 向集群添加插槽 + +```shell +cluster addslots 1 2 3 +``` + +2. ### 从集群中删除插槽。 + +```shell +cluster delslots 1 2 3 +``` + +3. ### 设置插槽的状态。 + +```shell +cluster setslot 1 node node-id +cluster setslot 1 migrating node-id +cluster setslot 1 importing node-id +``` + +4. + + ### 查看插槽信息 + +``` +cluster slots +``` + +## 其他命令 + +1. ### 计算给定键的插槽。 + +```shell +cluster keyslot mykey +``` + +2. ### 获取对某个节点的失败报告数。 + +```shell +cluster count-failure-reports +``` + +3. ### 计算某个插槽中的键数量。 + +```shell +cluster countkeysinslot 1 +``` + +4. ### 获取某个插槽中的键。 + +```shell +cluster getkeysinslot 1 10 +``` + +5. ### 强制集群保存配置。 + +```shell +cluster saveconfig +``` + +6. ### 列出某个主节点的从节点。 + +```shell +cluster slaves master-node-id +``` + +## 集群命令 + +### 1.创建集群 + +```shell +redis-cli --cluster create : ... : --cluster-replicas +``` + +**参数**: + +- `host1:port1 ... hostN:portN`: 指定集群中的各个节点。 +- `--cluster-replicas `: 设置每个主节点的副本数。 + +**举例**: + +假设有三个Redis实例,分别运行在端口7000、7001和7002,创建一个包含三个节点的Redis集群,每个主节点有一个副本。 + +```shell +redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 --cluster-replicas 1 +``` + +### 2.检查集群 + +```shell +redis-cli --cluster check : --cluster-search-multiple-owners +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 +- `--cluster-search-multiple-owners`: 搜索有多个拥有者的槽(可选)。 + +**举例**: + +检查集群的健康状态,使用节点127.0.0.1:7000作为入口。 + +```shell +redis-cli --cluster check 127.0.0.1:7000 +``` + +### 3.获取集群信息 + +```shell +redis-cli --cluster info : +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 + +**举例**: + +获取集群的信息,使用节点127.0.0.1:7000作为入口。 + +```shell +redis-cli --cluster info 127.0.0.1:7000 +``` + +### 4.修复集群 + +```shell +redis-cli --cluster fix : --cluster-search-multiple-owners --cluster-fix-with-unreachable-masters +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 +- `--cluster-search-multiple-owners`: 搜索有多个拥有者的槽(可选)。 +- `--cluster-fix-with-unreachable-masters`: 处理无法访问的主节点(可选)。 + +**举例**: + +修复集群中可能存在的问题,使用节点127.0.0.1:7000作为入口。例如重新分配槽等。 + +```shell +redis-cli --cluster fix 127.0.0.1:7000 +``` + +### 5.重新分片 + +```shell +redis-cli --cluster reshard : --cluster-from --cluster-to --cluster-slots --cluster-yes --cluster-timeout --cluster-pipeline --cluster-replace +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 +- `--cluster-from `: 指定源节点。 +- `--cluster-to `: 指定目标节点。 +- `--cluster-slots `: 指定需要迁移的槽数。 +- `--cluster-yes`: 自动确认操作。 +- `--cluster-timeout `: 设置操作超时时间。 +- `--cluster-pipeline `: 设置管道数量。 +- `--cluster-replace`: 替换现有的槽分配。 + +**举例**: + +将槽从节点127.0.0.1:7000移动到节点127.0.0.1:7001。 + +```shell +redis-cli --cluster reshard 127.0.0.1:7000 --cluster-from 127.0.0.1:7000 --cluster-to 127.0.0.1:7001 --cluster-slots 5 --cluster-yes +``` + +### 6.重新平衡 + +```shell +redis-cli --cluster rebalance : --cluster-weight --cluster-use-empty-masters --cluster-timeout --cluster-simulate --cluster-pipeline --cluster-threshold --cluster-replace +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 +- `--cluster-weight `: 指定节点权重。 +- `--cluster-use-empty-masters`: 使用空的主节点。 +- `--cluster-timeout `: 设置操作超时时间。 +- `--cluster-simulate`: 模拟操作。 +- `--cluster-pipeline `: 设置管道数量。 +- `--cluster-threshold `: 设置平衡阈值。 +- `--cluster-replace`: 替换现有的槽分配。 + +**举例**: + +重新平衡集群中节点的数据分布,使用节点127.0.0.1:7000作为入口。 + +```shell +redis-cli --cluster rebalance 127.0.0.1:7000 +``` + +### 7.添加节点 + +```shell +redis-cli --cluster add-node : : --cluster-slave --cluster-master-id +``` + +**参数**: + +- `new_host:new_port`: 新节点的地址。 +- `existing_host:existing_port`: 集群中任意一个节点的地址。 +- `--cluster-slave`: 将新节点添加为从节点。 +- `--cluster-master-id `: 指定主节点ID(在从节点模式下使用)。 + +**举例**: + +将新节点127.0.0.1:7003添加到现有集群中,并使其成为从节点。 + +```shell +redis-cli --cluster add-node 127.0.0.1:7003 127.0.0.1:7000 --cluster-slave +``` + +### 8.删除节点 + +```shell +redis-cli --cluster del-node : +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 +- `node_id`: 要删除的节点ID。 + +**举例**: + +从集群中删除节点127.0.0.1:7003。从集群中删除指定节点,其中``是要删除的节点ID,可以通过`redis-cli --cluster info 127.0.0.1:7000`命令获取。 + +```shell +redis-cli --cluster del-node 127.0.0.1:7000 +``` + +### 9.发送命令 + +```shell +redis-cli --cluster call : command arg arg .. arg --cluster-only-masters --cluster-only-replicas +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 +- `command arg arg .. arg`: 要发送的命令及其参数。 +- `--cluster-only-masters`: 仅向主节点发送命令。 +- `--cluster-only-replicas`: 仅向从节点发送命令。 + +**举例**: + +向集群中所有主节点发送`FLUSHALL`命令,清除所有数据。 + +```shell +redis-cli --cluster call 127.0.0.1:7000 FLUSHALL --cluster-only-masters +``` + +### 10.设置超时时间 + +```shell +redis-cli --cluster set-timeout : +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 +- `milliseconds`: 超时时间(毫秒)。 + +**举例**: + +设置节点127.0.0.1:7000的超时时间为2000毫秒。 + +```shell +redis-cli --cluster set-timeout 127.0.0.1:7000 2000 +``` + +### 11.导入数据 + +```shell +redis-cli --cluster import : --cluster-from --cluster-from-user --cluster-from-pass --cluster-from-askpass --cluster-copy --cluster-replace +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 +- `--cluster-from `: 源Redis实例地址。 +- `--cluster-from-user `: 源Redis实例用户名。 +- `--cluster-from-pass `: 源Redis实例密码。 +- `--cluster-from-askpass`: 从输入中获取密码。 +- `--cluster-copy`: 复制数据而不是移动数据。 +- `--cluster-replace`: 替换现有的数据。 + +**举例**: + +将数据从非集群Redis实例127.0.0.1:6379导入到集群中。 + +```shell +redis-cli --cluster import 127.0.0.1:7000 --cluster-from 127.0.0.1:6379 --cluster-copy +``` + +### 12.备份 + +```shell +redis-cli --cluster backup : +``` + +**参数**: + +- `host:port`: 集群中任意一个节点的地址。 +- `backup_directory`: 备份文件保存的目录。 + +**举例**: + +备份节点127.0.0.1:7000的数据到目录/backup. + +```shell +redis-cli --cluster backup 127.0.0.1:7000 /backup +``` + +### 13. 显示帮助信息 + +```shell +redis-cli --cluster help +``` + +**解释**: + +- 显示所有集群管理命令的帮助信息。