一、Windows 系统安装 Redis
方法1:直接安装(推荐新手)
-
下载 Redis for Windows
访问微软维护的 Redis 版本:https://github.com/microsoftarchive/redis/releases
下载Redis-x64-3.2.100.msi
(或最新版本)安装包。 -
安装 Redis
- 双击下载的
.msi
文件 - 点击下一步,勾选 “Add Redis installation folder to PATH”(将 Redis 添加到环境变量)
- 完成安装
- 双击下载的
-
启动 Redis 服务
- 按
Win + R
输入cmd
打开命令行 - 输入以下命令启动 Redis 服务(保持窗口不要关闭):
redis-server
- 看到
[OK] Redis is running
表示启动成功
- 按
方法2:通过 WSL 安装(适合 Win10/11 高级用户)
-
启用 WSL(Windows Subsystem for Linux)
- 管理员身份打开 PowerShell,输入:
wsl --install
- 重启电脑后,按提示安装 Ubuntu 系统
- 管理员身份打开 PowerShell,输入:
-
在 WSL 中安装 Redis
sudo apt update sudo apt install redis-server sudo service redis-server start
二、macOS 系统安装 Redis
方法:使用 Homebrew(推荐)
-
安装 Homebrew(如果已安装跳过)
打开终端,粘贴以下命令:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
安装 Redis
brew install redis
-
启动 Redis 服务
# 手动启动(关闭终端会停止服务) redis-server# 设置开机自启 brew services start redis
三、Linux 系统安装(Ubuntu/Debian)
方法1:通过 apt 安装
sudo apt update
sudo apt install redis-server# 启动 Redis
sudo systemctl start redis-server# 设置开机自启
sudo systemctl enable redis-server
方法2:手动编译安装(获取最新版)
# 安装编译依赖
sudo apt install build-essential# 下载最新版 Redis
wget https://download.redis.io/redis-stable.tar.gz
tar -xzvf redis-stable.tar.gz
cd redis-stable# 编译安装
make
sudo make install# 启动 Redis
redis-server
四、验证安装是否成功
所有系统通用的验证方法:
- 打开命令行窗口
- 输入以下命令连接 Redis:
redis-cli ping
- 如果返回
PONG
表示安装成功!
(示意图:命令行返回 PONG)
五、安装后注意事项
-
默认配置
- 无需密码即可访问(仅限本地连接
127.0.0.1:6379
) - 数据保存在内存中,重启后数据会丢失
- 无需密码即可访问(仅限本地连接
-
基础安全设置(可选)
编辑配置文件(Linux/macOS 路径/etc/redis/redis.conf
,Windows 在安装目录):# 设置密码(取消注释并修改) requirepass your_password# 允许远程访问(慎用) bind 0.0.0.0
-
关闭 Redis 服务
# Linux/macOS sudo systemctl stop redis-server# 通用方法(强制关闭) redis-cli shutdown
常见问题解决
-
Windows 安装后无法启动
- 确保安装了 Microsoft Visual C++ 2010 Redistributable Package
- 检查 6379 端口是否被占用
-
macOS/Linux 提示权限不足
在命令前加sudo
,如:sudo redis-server
通过以上步骤,您已经成功安装了 Redis!接下来可以结合 Spring Boot 进行开发(参考上一篇教程)。如有其他问题欢迎留言讨论!