电脑安装 Redis 的方法

126 阅读2分钟

安装 Redis 的方法取决于你使用的操作系统。以下是针对 macOS、Linux(以 Ubuntu/Debian 为例)和 Windows 的详细安装步骤。


✅ 一、macOS 上安装 Redis

方法 1:使用 Homebrew(推荐)

# 1. 安装 Homebrew(如果还没安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 2. 使用 Homebrew 安装 Redis
brew install redis

# 3. 启动 Redis 服务
brew services start redis
# 或手动运行:
redis-server /opt/homebrew/etc/redis.conf

📌 注意:Apple Silicon 芯片(M1/M2)默认路径是 /opt/homebrew/etc/redis.conf
Intel 芯片是 /usr/local/etc/redis.conf

验证是否成功:

# 打开 Redis 客户端
redis-cli

# 输入 ping,应返回 PONG
127.0.0.1:6379> ping
PONG

✅ 二、Linux(Ubuntu/Debian)上安装 Redis

# 1. 更新包列表
sudo apt update

# 2. 安装 Redis
sudo apt install redis-server

# 3. 启动 Redis 并设置开机自启
sudo systemctl start redis-server
sudo systemctl enable redis-server

# 4. 检查状态
sudo systemctl status redis-server

验证:

redis-cli ping
# 返回 PONG 表示成功

🔒 生产环境建议配置密码、绑定 IP、关闭危险命令等,修改 /etc/redis/redis.conf


✅ 三、Windows 上安装 Redis

Redis 官方不支持 Windows,但微软维护了一个移植版本。

方法:使用 Microsoft 提供的 Redis for Windows

  1. 下载地址: 👉 github.com/microsoftar…

  2. 下载 Redis-x64-3.2.100.msi(推荐稳定版)

  3. 双击安装,选择:

    • Add to firewall
    • Run Redis as a service
  4. 安装完成后会自动启动服务。

验证:

打开命令提示符:

redis-cli ping
# 返回 PONG 表示成功

⚠️ 注意:这个项目已归档,仅适合开发测试,不推荐用于生产环境


✅ 四、通过 Docker 安装 Redis(跨平台通用)

# 拉取镜像并启动容器
docker run -d --name redis \
  -p 6379:6379 \
  redis:latest

# 添加密码启动(推荐)
docker run -d --name redis \
  -p 6379:6379 \
  redis:latest \
  --requirepass "yourpassword"

连接:

docker exec -it redis redis-cli
# 如果设置了密码
127.0.0.1:6379> auth yourpassword

💡 优点:干净、隔离、易于管理,适合开发和生产。


✅ 五、常用命令总结

命令说明
redis-server启动 Redis 服务
redis-cli打开 Redis 客户端
redis-cli ping测试连接
redis-cli shutdown关闭 Redis
CONFIG GET requirepass查看密码
CONFIG SET requirepass "123456"设置密码

✅ 六、配置建议(开发环境)

编辑配置文件(如 /etc/redis/redis.confC:\Program Files\Redis\redis.windows.conf):

bind 127.0.0.1        # 只允许本地连接(安全)
port 6379
daemonize yes         # 后台运行(Linux)
requirepass yourpass  # 设置密码

修改后重启 Redis 生效。


✅ 七、Java 项目中如何连接 Redis?

确保你的 application.yml 中配置了 Redis:

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: yourpass  # 如果设置了
    database: 0

✅ 总结

系统推荐方式
macOSbrew install redis
Linuxapt install redis-server
Windows下载 MSI 安装包(仅开发)
所有系统Docker 安装最简单可靠