Redis 是一个高性能的内存数据库,安装过程简单。以下是针对不同平台的安装指南。
1. Linux(以 Ubuntu 为例)
- 更新包索引:
sudo apt update
- 安装 Redis:
sudo apt install redis-server
- 启动 Redis 服务:
sudo systemctl start redis
- (可选)设置开机启动:
sudo systemctl enable redis
- 验证安装:
redis-cli ping
输出 PONG
表示成功。
2. Windows
Redis 官方不支持 Windows,但可以通过以下方式安装:
- 使用 WSL(推荐):
- 在 Windows 10/11 上启用 WSL2(Windows Subsystem for Linux)。
- 安装 Ubuntu(通过 Microsoft Store)。
- 在 WSL 中按照 Linux 安装步骤操作。
- Docker:
- 安装 Docker Desktop.
- 运行 Redis 容器:
bash docker run -d -p 6379:6379 redis
- 验证:
bash docker exec -it <container_id> redis-cli ping
- 非官方 Windows 版本:
下载 Microsoft 维护的 Redis 移植版(不推荐,版本较旧):Redis GitHub.
3. MacOS
- 安装 Homebrew(如果未安装):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- 安装 Redis:
brew install redis
- 启动 Redis:
brew services start redis
- 验证安装:
redis-cli ping
输出 PONG
表示成功。
4. 源码安装(适用于所有平台)
如果需要特定版本或自定义编译:
- 下载源码:
wget https://download.redis.io/releases/redis-7.2.4.tar.gz
- 解压并编译:
tar -xzvf redis-7.2.4.tar.gz
cd redis-7.2.4
make
sudo make install
- 启动 Redis:
redis-server
- 验证:
redis-cli ping
最新版本下载:Redis 官方下载
5. 注意事项
- 防火墙:确保 Redis 默认端口
6379
未被阻止。 - 依赖:Linux 安装可能需要
gcc
和make
(sudo apt install build-essential
)。 - 配置:安装后可修改
/etc/redis/redis.conf
(Linux)或自定义配置文件,详见后续配置教程。
6. 验证安装
无论哪种方式,运行以下命令确认 Redis 正常工作:
redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set testkey "Hello Redis"
OK
127.0.0.1:6379> get testkey
"Hello Redis"
7. 参考资料
- 官方安装指南:Redis Installation
- Docker 镜像:Redis Docker Hub
- 中文教程:Redis 中文网
发表回复