Linux 安装 Redis
在 Linux 系统上安装 Redis 服务可以通过包管理器或源码编译两种方式,以下是详细步骤:
包管理器安装
适用于 Ubuntu/Debian 或 CentOS/RHEL 系统,简单快捷:
Ubuntu/Debian 系统:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| sudo apt update
sudo apt install redis-server
sudo systemctl start redis-server
sudo systemctl enable redis-server
sudo systemctl status redis-server
|
CentOS/RHEL 系统:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
sudo yum install epel-release
sudo yum install redis
sudo systemctl start redis
sudo systemctl enable redis
sudo systemctl status redis
|
源码编译安装
适用于其他 Linux 发行版或系统,需要先安装依赖库,然后编译安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| sudo apt update && sudo apt install build-essential tcl
wget http://download.redis.io/releases/redis-7.2.4.tar.gz
tar xzf redis-7.2.4.tar.gz
cd redis-7.2.4
make
make test
sudo make install
sudo mkdir -p /etc/redis /var/lib/redis
sudo cp redis.conf /etc/redis/
|
在配置文件中修改以下关键设置:
1 2 3 4 5 6 7 8 9 10 11
| # 监听地址,允许远程连接(如果需要) bind 0.0.0.0
# 守护进程模式 daemonize yes
# 数据存储目录 dir /var/lib/redis
# 日志文件 logfile /var/log/redis/redis-server.log
|
创建系统服务文件:
1
| sudo nano /etc/systemd/system/redis.service
|
将以下内容粘贴进去并保存:
1 2 3 4 5 6 7 8 9 10 11 12 13
| [Unit] Description=Redis In-Memory Data Store After=network.target
[Service] User=redis Group=redis ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown Restart=always
[Install] WantedBy=multi-user.target
|
完成安装:
1 2 3 4 5 6 7
| sudo adduser --system --group --no-create-home redis sudo chown -R redis:redis /var/lib/redis
sudo systemctl start redis sudo systemctl enable redis
|
验证安装
连接 Redis 服务器
redis-cli
测试命令
ping # 应返回 PONG
退出
exit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 常用命令:
```bash # 启动 Redis sudo systemctl start redis
# 停止 Redis sudo systemctl stop redis
# 重启 Redis sudo systemctl restart redis
# 查看 Redis 状态 sudo systemctl status redis
|