Redis 安装教程(多平台全覆盖)
目标:5 分钟内,让你在 Linux / macOS / Windows / Docker 上成功运行 Redis。
一、查看最新版本(2025 年 11 月)
| 平台 | 最新稳定版 |
|---|---|
| 官方 | Redis 7.4.x(推荐) |
| Docker | redis:7.4 |
官网下载:https://redis.io/download/
二、安装方式对比
| 方式 | 推荐场景 | 优点 |
|---|---|---|
| 源码编译 | 生产服务器、定制优化 | 最高性能、可定制 |
| 包管理器 | 开发测试 | 简单快速 |
| Docker | 跨平台、隔离、CI/CD | 最推荐! |
三、详细安装步骤
1. Linux(Ubuntu / Debian)
# 更新软件包
sudo apt update
# 安装 Redis(包含 redis-server 和 redis-cli)
sudo apt install redis-server -y
# 启动服务
sudo systemctl start redis
# 开机自启
sudo systemctl enable redis
# 检查状态
sudo systemctl status redis
验证:
redis-cli ping
# 输出:PONG
2. Linux(CentOS / RHEL / Rocky)
# 启用 EPEL(可选)
sudo dnf install epel-release -y
# 安装 Redis
sudo dnf install redis -y
# 启动
sudo systemctl start redis
# 开机自启
sudo systemctl enable redis
# 检查
redis-cli ping
3. macOS(推荐 Homebrew)
# 安装 Homebrew(若未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装 Redis
brew install redis
# 启动服务(后台运行)
brew services start redis
# 或前台运行(调试用)
redis-server
验证:
redis-cli ping
4. Windows(官方已停止支持)
推荐方案:WSL2(Windows Subsystem for Linux)
# PowerShell 开启 WSL
wsl --install -d Ubuntu
# 进入 Ubuntu
wsl
# 按 Ubuntu 方式安装
sudo apt update && sudo apt install redis-server -y
redis-cli ping
5. Docker(跨平台神器,强烈推荐!)
# 拉取官方最新镜像
docker pull redis:7.4
# 运行容器(后台)
docker run -d \
--name my-redis \
-p 6379:6379 \
redis:7.4
# 查看日志
docker logs my-redis
带密码 + 数据持久化:
docker run -d \
--name redis-secure \
-p 6379:6379 \
-v redis-data:/data \
-e REDIS_PASSWORD=mysecretpassword \
redis:7.4 \
redis-server --appendonly yes --requirepass mysecretpassword
连接:
redis-cli -a mysecretpassword
> PING
PONG
6. 源码编译安装(生产推荐)
# 下载最新版
wget https://download.redis.io/releases/redis-7.4.0.tar.gz
tar xzf redis-7.4.0.tar.gz
cd redis-7.4.0
# 编译(推荐使用 jemalloc 提升性能)
make MALLOC=jemalloc
# 安装到 /usr/local/bin
sudo make install
# 创建配置文件目录
sudo mkdir /etc/redis
sudo cp redis.conf /etc/redis/redis.conf
# 启动
redis-server /etc/redis/redis.conf
四、配置文件(/etc/redis/redis.conf 关键项)
# 绑定地址(生产建议指定内网 IP)
bind 127.0.0.1
# 端口
port 6379
# 后台运行
daemonize yes
# 数据目录
dir /var/lib/redis/
# 密码(推荐生产开启)
requirepass your_strong_password
# 持久化
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
# 内存淘汰策略
maxmemory 2gb
maxmemory-policy allkeys-lru
五、防火墙与安全
# 开放端口(仅内网)
sudo ufw allow from 192.168.1.0/24 to any port 6379
# 或云服务器安全组放行
警告:永远不要将 Redis 暴露在公网无密码!
六、验证安装成功
# 进入客户端
redis-cli
# 测试命令
PING
SET install:ok "yes"
GET install:ok
KEYS *
输出应为:
PONG
OK
"yes"
1) "install:ok"
七、常见问题排查
| 问题 | 解决方案 |
|---|---|
Could not connect to Redis | 检查 bind、port、防火墙 |
PING 返回 NOAUTH | 需要密码:redis-cli -a password |
| 启动失败 | 查看日志:journalctl -u redis 或 docker logs |
| 内存爆了 | 设置 maxmemory + maxmemory-policy |
八、安装后推荐操作
# 1. 设置密码
CONFIG SET requirepass "yourpassword"
AUTH yourpassword
# 2. 开启 AOF 持久化
CONFIG SET appendonly yes
# 3. 测试性能
redis-benchmark -n 100000 -q
九、图形化管理工具(可选)
| 工具 | 下载 |
|---|---|
| RedisInsight(官方) | https://redis.com/redis-enterprise/redis-insight/ |
| Another Redis Desktop Manager | https://github.com/qishibo/AnotherRedisDesktopManager |
完成!你的 Redis 已就绪
redis-cli
> INFO server
# 看到 redis_version:7.4.x 即成功
下一步推荐:
需要我帮你 一键生成生产级 redis.conf 或 Docker Compose 集群?告诉我!