Windows 系统搭建Redis原生集群详细图文指南
🧱 Windows 系统搭建 Redis 原生集群详细图文指南
适用于:Redis 单机多实例集群部署(伪分布式集群)
📁 一、目录结构规划
我们将使用以下路径来组织 Redis 集群:
C:\software\redis # Redis 安装目录
C:\RedisCluster # 集群主目录
C:\RedisCluster\6380 # 实例1配置目录(端口6380)
C:\RedisCluster\6381 # 实例2配置目录(端口6381)
...
C:\RedisCluster\6385 # 实例6配置目录(端口6385)
🔧 二、环境准备
1. 下载并解压 Redis
前往官方归档地址下载 Redis for Windows:
🔗 https://github.com/microsoftarchive/redis/releases
推荐下载版本:
Redis-x64-3.2.100.zip
(稳定性最好)Redis-x64-5.0.14.1.zip
(功能更全)
将下载的压缩包解压至:
C:\software\redis
确保包含以下关键文件:
redis-server.exe
redis-cli.exe
🧰 三、创建六个 Redis 实例(端口从 6380 到 6385)
打开 PowerShell(任意身份),依次执行以下步骤:
创建主目录
mkdir C:\RedisCluster
实例 1: 端口 6380
mkdir C:\RedisCluster\6380
$conf = @"
port 6380
bind 127.0.0.1
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123456
masterauth 123456
"@
Set-Content -Path "C:\RedisCluster\6380\redis-6380.conf" -Value $conf
实例 2: 端口 6381
mkdir C:\RedisCluster\6381
$conf = @"
port 6381
bind 127.0.0.1
cluster-enabled yes
cluster-config-file nodes-6381.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123456
masterauth 123456
"@
Set-Content -Path "C:\RedisCluster\6381\redis-6381.conf" -Value $conf
实例 3: 端口 6382
mkdir C:\RedisCluster\6382
$conf = @"
port 6382
bind 127.0.0.1
cluster-enabled yes
cluster-config-file nodes-6382.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123456
masterauth 123456
"@
Set-Content -Path "C:\RedisCluster\6382\redis-6382.conf" -Value $conf
实例 4: 端口 6383
mkdir C:\RedisCluster\6383
$conf = @"
port 6383
bind 127.0.0.1
cluster-enabled yes
cluster-config-file nodes-6383.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123456
masterauth 123456
"@
Set-Content -Path "C:\RedisCluster\6383\redis-6383.conf" -Value $conf
实例 5: 端口 6384
mkdir C:\RedisCluster\6384
$conf = @"
port 6384
bind 127.0.0.1
cluster-enabled yes
cluster-config-file nodes-6384.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123456
masterauth 123456
"@
Set-Content -Path "C:\RedisCluster\6384\redis-6384.conf" -Value $conf
实例 6: 端口 6385
mkdir C:\RedisCluster\6385
$conf = @"
port 6385
bind 127.0.0.1
cluster-enabled yes
cluster-config-file nodes-6385.conf
cluster-node-timeout 5000
appendonly yes
requirepass 123456
masterauth 123456
"@
Set-Content -Path "C:\RedisCluster\6385\redis-6385.conf" -Value $conf
▶️ 四、启动所有 Redis 实例
在 PowerShell 中依次运行以下命令:
启动实例 6380
cd "C:\software\redis"
Start-Process powershell.exe "-NoExit", "& '$PWD\redis-server.exe' 'C:\RedisCluster\6380\redis-6380.conf'"
启动实例 6381
cd "C:\software\redis"
Start-Process powershell.exe "-NoExit", "& '$PWD\redis-server.exe' 'C:\RedisCluster\6381\redis-6381.conf'"
启动实例 6382
cd "C:\software\redis"
Start-Process powershell.exe "-NoExit", "& '$PWD\redis-server.exe' 'C:\RedisCluster\6382\redis-6382.conf'"
启动实例 6383
cd "C:\software\redis"
Start-Process powershell.exe "-NoExit", "& '$PWD\redis-server.exe' 'C:\RedisCluster\6383\redis-6383.conf'"
启动实例 6384
cd "C:\software\redis"
Start-Process powershell.exe "-NoExit", "& '$PWD\redis-server.exe' 'C:\RedisCluster\6384\redis-6384.conf'"
启动实例 6385
cd "C:\software\redis"
Start-Process powershell.exe "-NoExit", "& '$PWD\redis-server.exe' 'C:\RedisCluster\6385\redis-6385.conf'"
🌐 五、创建 Redis 集群
等待上面所有实例成功运行后,在一个新的 PowerShell 窗口中执行下面命令来创建集群:
cd "C:\software\redis"
.\redis-cli.exe --cluster create 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 --cluster-replicas 1 -a 123456 --cluster-yes
🔍 六、验证 Redis 集群状态
继续在同一窗口中执行:
$commands = @"
cluster info
cluster nodes
exit
"@echo $commands | .\redis-cli.exe -c -p 6380 -a 123456
输出示例:
cluster_state:ok
cluster_slots_assigned:16384
cluster_known_nodes:6
...
表示集群已经正确生成!
📜 七、附录:完整 .ps1
脚本一键部署(带路径检测)
你可以将此脚本保存为 setup-redis-cluster.ps1
并运行:
# 设置变量
$redisPath = "C:\software\redis"
$clusterDir = "C:\RedisCluster"
$instances = @(6380, 6381, 6382, 6383, 6384, 6385)
$password = "123456"# 检查 Redis 是否安装正确
if (-Not (Test-Path "$redisPath")) {Write-Host "❌ 错误:未找到 Redis 安装目录,请检查路径是否为:$redisPath" -ForegroundColor Redexit
}if (-Not (Test-Path "$redisPath\redis-server.exe") -or -Not (Test-Path "$redisPath\redis-cli.exe")) {Write-Host "❌ 错误:未找到 redis-server.exe 或 redis-cli.exe,请确认是否已正确解压 Redis 到该目录。" -ForegroundColor Redexit
}# 创建主目录
New-Item -ItemType Directory -Force -Path $clusterDir | Out-Null# 创建每个实例的目录和配置文件
foreach ($port in $instances) {$dir = "$clusterDir\$port"New-Item -ItemType Directory -Force -Path $dir | Out-Null$confFile = "$dir\redis-$port.conf"$content = @"
port $port
bind 127.0.0.1
cluster-enabled yes
cluster-config-file nodes-$port.conf
cluster-node-timeout 5000
appendonly yes
requirepass $password
masterauth $password
"@Set-Content -Path $confFile -Value $contentWrite-Host "✅ 已创建配置文件:$confFile"
}# 启动实例
foreach ($port in $instances) {$confFile = "$clusterDir\$port\redis-$port.conf"Start-Process powershell.exe "-NoExit", "& '$redisPath\redis-server.exe' '$confFile'"Write-Host "🔌 已启动 Redis 实例,端口:$port"
}# 等待启动完成
Write-Host "`n⏳ 正在等待 Redis 实例启动完成(10秒)..." -ForegroundColor Yellow
Start-Sleep -Seconds 10# 创建集群
Write-Host "`n🧱 正在创建 Redis 集群..." -ForegroundColor Green
$nodes = $instances | ForEach-Object { "127.0.0.1:$_" }
& "$redisPath\redis-cli.exe" --cluster create @nodes --cluster-replicas 1 -a $password --cluster-yes# 验证集群
Write-Host "`n🔍 正在验证 Redis 集群状态..." -ForegroundColor Green
$commands = @"
cluster info
cluster nodes
exit
"@echo $commands | & "$redisPath\redis-cli.exe" -c -p 6380 -a $password# 结束提示
Write-Host "`n🎉 Redis 集群搭建完成!" -ForegroundColor Green
Write-Host "你可以通过 redis-cli 连接测试,例如:" -ForegroundColor Cyan
Write-Host " cd C:\software\redis" -ForegroundColor Cyan
Write-Host " redis-cli.exe -c -p 6380 -a 123456" -ForegroundColor Cyan
✅ 总结
步骤 | 内容 |
---|---|
1 | Redis 安装路径为:C:\software\redis |
2 | 六个配置文件分别创建,不省略任何重复步骤 |
3 | 每个实例单独启动一个窗口 |
4 | 使用 redis-cli 创建集群 |
5 | 验证 cluster info 和 cluster nodes |
6 | 提供完整 .ps1 脚本,支持一键运行 |