Windows 安装 Redis 的几种方式与测试流程总结

Redis 在 Windows 上的安装和运行与 Linux 有所不同,以下是几种常见的安装方式及完整的测试流程总结。

图片[1]_Windows 安装 Redis 的几种方式与测试流程总结_知途无界

一、Windows 安装 Redis 的几种方式

1. 使用 Microsoft Archive 提供的旧版 Redis (非官方维护)

步骤:

  1. 访问 MicrosoftArchive/redis 下载页面
  2. 下载最新稳定版(如 Redis-x64-3.2.100.msi
  3. 双击安装包完成安装

特点:

  • 微软维护的非官方版本
  • 已停止更新(最新版本基于 Redis 3.2)
  • 适合简单测试环境

2. 使用 WSL2 (Windows Subsystem for Linux) 安装原生 Redis

步骤:

  1. 启用 WSL2:
   wsl --install
  1. 安装 Linux 发行版(如 Ubuntu)
  2. 在 WSL 终端中安装 Redis:
   sudo apt update
   sudo apt install redis-server
  1. 启动 Redis 服务:
   sudo service redis-server start

特点:

  • 推荐方式,获得最新稳定版 Redis
  • 性能接近原生 Linux 环境
  • 需要 Windows 10/11 2004+ 版本

3. 使用 Docker 容器运行 Redis

步骤:

  1. 安装 Docker Desktop for Windows
  2. 运行 Redis 容器:
   docker run --name redis-server -d -p 6379:6379 redis
  1. 测试连接:
   docker exec -it redis-server redis-cli ping
   # 应返回 "PONG"

特点:

  • 隔离环境,不影响主机系统
  • 版本管理方便(可指定任意版本)
  • 适合开发环境

4. 使用 Memurai (Windows 原生 Redis 替代品)

步骤:

  1. 下载并安装 Memurai
  2. 安装完成后自动运行服务
  3. 使用默认端口 6379 连接

特点:

  • Windows 原生优化
  • 免费版功能有限
  • 适合 Windows 专用环境

二、Redis 测试流程

1. 基本连接测试

使用 redis-cli 测试:

# 在 WSL 或 Docker 中
redis-cli ping
# 应返回 "PONG"

# 指定主机和端口(如 Docker 情况)
redis-cli -h localhost -p 6379 ping

使用 Python 测试:

import redis

r = redis.Redis(host='localhost', port=6379, db=0)
print(r.ping())  # 应返回 True

2. 数据操作测试

# 设置键值
redis-cli set name "Redis on Windows"

# 获取键值
redis-cli get name  # 应返回 "Redis on Windows"

# 列表操作
redis-cli rpush fruits "apple" "banana" "orange"
redis-cli lrange fruits 0 -1  # 应返回列表

3. 持久化测试

检查配置文件:

# 在 redis.conf 中确认
appendonly yes
save 900 1

手动触发持久化:

redis-cli BGSAVE
# 检查 dump.rdb 文件是否生成

4. 性能测试

# 使用 redis-benchmark 测试
redis-benchmark -n 10000 -c 50

测试结果解读:

  • 每秒操作数(ops/sec)
  • 延迟统计
  • 不同命令的性能表现

5. 集群模式测试(Redis 3.0+)

在 WSL/Docker 中创建集群:

redis-cli --cluster create 127.0.0.1:7000 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 --cluster-replicas 1

测试集群功能:

redis-cli -c -p 7000  # 使用集群模式连接
127.0.0.1:7000> set key value

三、常见问题解决

  1. 连接被拒绝:
  • 检查 Redis 服务是否运行
  • 检查防火墙设置
  • 确认配置文件中 bind 127.0.0.1protected-mode no 设置
  1. Windows 上的性能问题:
  • 使用 WSL2 或 Docker 替代原生安装
  • 考虑 Memurai 等优化版本
  1. 持久化失败:
  • 检查磁盘空间
  • 确认配置文件中的持久化设置
  • 查看 Redis 日志文件

四、推荐方案

场景推荐方式
开发环境WSL2 + Redis 或 Docker
生产环境Linux 服务器
Windows 专用Memurai
快速测试MicrosoftArchive 版本

对于大多数现代开发场景,WSL2 + Redis 是最佳选择,它提供了接近生产环境的体验,同时保持了 Windows 的便利性。

© 版权声明
THE END
喜欢就点个赞,支持一下吧!
点赞17 分享
评论 抢沙发
头像
欢迎您留下评论!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容