安装 Redis 的步骤因操作系统而异,以下是常见平台的安装方法:
Linux (Ubuntu/Debian)
-
更新软件包列表:
sudo apt update
-
安装 Redis:
sudo apt install redis-server
-
启动 Redis 服务:
sudo systemctl start redis-server
-
设置开机自启:
sudo systemctl enable redis-server
-
检查运行状态:
sudo systemctl status redis-server
Linux (CentOS/RHEL)
-
添加 EPEL 仓库(如未安装):
sudo yum install epel-release
-
安装 Redis:
sudo yum install redis
-
启动并启用 Redis:
sudo systemctl start redis sudo systemctl enable redis
macOS (通过 Homebrew)
-
安装 Redis:
brew install redis
-
启动 Redis:
brew services start redis # 后台运行
或手动运行:
redis-server
Windows
Windows 官方不支持原生 Redis,但可以通过以下方式:
-
Windows Subsystem for Linux (WSL)
在 WSL 中按上述 Linux 步骤安装。 -
微软维护的 Redis 移植版
下载地址:https://github.com/microsoftarchive/redis/releases
(注意:非官方最新版,仅用于开发测试)。
验证安装
连接到 Redis 并测试:
redis-cli ping
如果返回 PONG
,说明 Redis 正常运行。
基本配置
-
配置文件路径(Linux):
/etc/redis/redis.conf
-
允许远程访问(需谨慎):修改
bind 127.0.0.1
为0.0.0.0
并设置密码requirepass yourpassword
。
卸载 Redis
-
Ubuntu/Debian:
sudo apt remove --purge redis-server
-
macOS:
brew uninstall redis