Windows本地搭建Redis集群(集群模式)

手打不易,如果转摘,请注明出处!

注明原文: https://blog.csdn.net/q258523454/article/details/144477957


前言

Redis版本:redis 5.0.14.1

Windows版本:Windows10

本文只讲集群模式

1. 安装Redis

1.1 下载

https://github.com/tporadowski/redis/releases

1.2 解压

解压到任意目录,例如:E:\Program Files (x86)\redis5.0.14.1

2. 集群配置准备

2.1 创建集群目录

创建一个目录,名字随意,例如: cluster

由于Redis集群模式,一般需要6个节点。假设我们要创建的集群节点的端口分别为:

7001、7002、7003、7004、7005、7006.

我们先创建7001节点,如图所示先在cluster目录下,创建一个名为7001的目录.

我们把下载的redis再解压(或者拷贝)一次,本文这里下载的版本是:Redis-x64-5.0.14.1.zip,重新再这里解压一次。注意:cluster目录的上一层,已经是解压了的,只是这里在做一次同样的操作。

得到如下目录:

2.2 修改节点配置

打开目录:E:\Program Files (x86)\redis5.0.14.1\cluster\7001 下的配置文件:redis.windows.conf

修改或新增以下内容:

注意:

① requirepass 和 masterauth 是密码,可以不设置,看自己需要

② requirepass 和 masterauth 需要同时设置,否则下次启动会报错:MASTER aborted replication with an error: NOAUTH Authentication required,原因可以参考:Link

复制代码
port 7001
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 15000

requirepass 123456
masterauth 123456

2.3 新增start.bat

在目录:E:\Program Files (x86)\redis5.0.14.1\cluster\7001新增bat文件,方便启动

start.bat 内容如下:

复制代码
title redis-7001
redis-server.exe redis.windows.conf

此时,第一个集群节点7001,配置完成。

我们可以双击 start.bat看是否正常启动.

2.4 创建其他5个节点

我们直接拷贝7001目录,拷贝命名为7002、7003、7004、7005、7006

分别修改对应的配置文件 redis.windows.conf

注意: 下面的配置,700X,在不同目录下改成对应的7002、7003、7004、7005、7006

复制代码
port 700X
cluster-enabled yes
cluster-config-file nodes-700X.conf
cluster-node-timeout 15000

requirepass 123456
masterauth 123456

分别修改对应的启动bat文件 start.bat

注意: 下面的配置,700X,在不同目录下改成对应的7002、7003、7004、7005、7006

复制代码
title redis-700X
redis-server.exe redis.windows.conf

至此,6个集群节点的配置,全部准备完毕!

3. 启动集群

3.1 启动6个节点

首先,我们启动这6个节点。

方式1:对每个节点手动启动 (不推荐)

复制代码
redis-server.exe redis.windows.conf

方式2:执行每个目录下的 start.bat(不推荐)

方式3:写一个批量的bat,一次性启动

例如,bat文件名为:run_cluster_7001_7006.bat

命令如下:

复制代码
@echo off
setlocal

rem 获取脚本所在的目录作为父目录
set "parent_directory=%~dp0"

rem 遍历父目录下的每个子目录
for /d %%D in ("%parent_directory%*") do (
  rem 检查子目录是否包含 start.bat 文件
  if exist "%%~D\start.bat" (
    rem 进入子目录并运行 start.bat
    pushd "%%~D"
    start start.bat
    popd
  )
)

endlocal

保存后,双击即可执行。

3.2 启动集群模式

节点都启动后,节点界面会显示:Ready to accept connections

意思是需要我们启动集群模式。

启动命令是:

注意:如果没有密码,去掉**"-a 123456**"

复制代码
redis-cli.exe -a 123456 --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

假如我们用下面这个 redis-cli.exe 启动

打开CMD命令,输入 "E:",切换磁盘。注意冒号是英文格式。

复制代码
E:

cd 到指定目录:

复制代码
E:\Program Files (x86)\redis5.0.14.1

执行命令,注意redis-cli.exe前面没有 ./

复制代码
redis-cli.exe -a 123456 --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

CMD界面执行如图所示:

bash 复制代码
E:\Program Files (x86)\redis5.0.14.1>redis-cli.exe -a 123456 --cluster create --cluster-replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 25e19e328a37fd52b671f00c0a9d87111d514487 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
M: 5f93a0d2fb64fb182fe22b8d8a75c4af6007eec5 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
M: d6d27d2be5a257f84b393104b9eb31014be95d0d 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
S: aeca57ee8f3c1dba4f04223ebbe44701853db324 127.0.0.1:7004
   replicates 5f93a0d2fb64fb182fe22b8d8a75c4af6007eec5
S: 0964ae11a4bf4e924bd52f35e0e9e9961d471236 127.0.0.1:7005
   replicates d6d27d2be5a257f84b393104b9eb31014be95d0d
S: b9755b7d45c2d794743666ccb3a70f34979857de 127.0.0.1:7006
   replicates 25e19e328a37fd52b671f00c0a9d87111d514487
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join

>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 25e19e328a37fd52b671f00c0a9d87111d514487 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0964ae11a4bf4e924bd52f35e0e9e9961d471236 127.0.0.1:7005
   slots: (0 slots) slave
   replicates d6d27d2be5a257f84b393104b9eb31014be95d0d
M: 5f93a0d2fb64fb182fe22b8d8a75c4af6007eec5 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: d6d27d2be5a257f84b393104b9eb31014be95d0d 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: b9755b7d45c2d794743666ccb3a70f34979857de 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 25e19e328a37fd52b671f00c0a9d87111d514487
S: aeca57ee8f3c1dba4f04223ebbe44701853db324 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 5f93a0d2fb64fb182fe22b8d8a75c4af6007eec5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

E:\Program Files (x86)\redis5.0.14.1>

各个节点的界面如图所示:

4. 连接测试

这里用客户端:ARDM------Another Redis Desktop Manager

下载链接:

https://github.com/qishibo/AnotherRedisDesktopManager/releases

如图所示,编辑信息,点击确认。

点击进入

显示正常

5. 其他

5.1 快速停止集群模式

我们新建一个批量bat,如图所示:

stop.bat内容如下:

复制代码
@echo off
set START_PORT=7001
set END_PORT=7006

echo Closing port  %START_PORT%-%END_PORT%...
echo.

for /l %%i in (%START_PORT%, 1, %END_PORT%) do (
    call :closeProgram %%i
)

echo.
echo Closing all
echo.

taskkill /f /fi "imagename eq cmd.exe"

echo.
echo All closed.
goto :eof

:closeProgram
set PORT=%1

echo Searching for programs using port %PORT%...

for /f "tokens=5" %%a in ('netstat -ano ^| findstr /i ":%PORT%"') do (
    taskkill /f /pid %%a
    echo Program with port %PORT% has been closed.
)

echo.
echo End of program list for port %PORT%.
goto :eof
相关推荐
遥不可及~~斌2 分钟前
Spring Boot 项目日志系统全攻略:Logback、Log4j2、Log4j与SLF4J整合指南
spring boot·log4j·logback
·云扬·6 分钟前
【BUG】阿里云服务器数据库远程连接报错
服务器·阿里云·bug
你们补药再卷啦8 分钟前
不用额外下载jar包,idea快速查看使用的组件源码
java·ide·intellij-idea
MXsoft61811 分钟前
云原生运维在 2025 年的发展蓝图
运维·服务器·数据库
爱的叹息22 分钟前
Spring Boot 自定义配置类(包含字符串、数字、布尔、小数、集合、映射、嵌套对象)实现步骤及示例
java·linux·spring boot
嵌入式-老费31 分钟前
Linux上位机开发实践(一个硬件算法加速的示例)
linux·运维·服务器
前进的程序员1 小时前
Linux C 与 C 语言的区别及开发差异
linux·运维·c语言
@西瓜@1 小时前
JAVAEE(多线程-线程池)
java·开发语言
葡萄城技术团队1 小时前
如何通过前端表格控件实现自动化报表?1
运维·前端·自动化
CZIDC1 小时前
华为昇腾服务器上查看固件、驱动和CANN版本的常用方法
linux·运维·服务器