RHEL 9.8 安装 Redis 8.8.1
-
- [RHEL 9.8 安装 Redis 8.8.1](#RHEL 9.8 安装 Redis 8.8.1)
-
- [1. 安装编译依赖](#1. 安装编译依赖)
- [2. 下载 Redis 8.8.1](#2. 下载 Redis 8.8.1)
- [3. 编译安装 Redis](#3. 编译安装 Redis)
- [4. 创建 Redis 运行用户](#4. 创建 Redis 运行用户)
- [5. 创建 Redis 目录](#5. 创建 Redis 目录)
- [6. 备份 Redis 配置](#6. 备份 Redis 配置)
- [7. 修改 Redis 配置](#7. 修改 Redis 配置)
- [8. 创建 systemd 服务](#8. 创建 systemd 服务)
- [9. 重新加载 systemd](#9. 重新加载 systemd)
- [10. 启动 Redis](#10. 启动 Redis)
- [11. 设置开机启动](#11. 设置开机启动)
- [12. 检查 Redis 端口](#12. 检查 Redis 端口)
- [13. 测试 Redis](#13. 测试 Redis)
- 最终目录结构
- 常用运维命令
- 如果以后需要远程访问
RHEL 9.8 安装 Redis 8.8.1
适用环境:
text
OS : RHEL 9.8
Redis : 8.8.1
安装方式 : 源码编译
程序目录 : /usr/local/bin
配置目录 : /etc/redis
数据目录 : /data/redis
日志目录 : /var/log/redis
服务管理 : systemd
1. 安装编译依赖
bash
dnf install -y gcc gcc-c++ make tar wget
确认编译环境:
bash
gcc --version
make --version
2. 下载 Redis 8.8.1
进入软件目录:
bash
cd /soft
下载:
bash
wget https://download.redis.io/releases/redis-8.8.1.tar.gz
解压:
bash
tar -zxvf redis-8.8.1.tar.gz
cd redis-8.8.1
3. 编译安装 Redis
开始编译:
bash
make -j$(nproc)
编译完成后先确认:
bash
src/redis-server --version
正常应看到:
text
Redis server v=8.8.1
安装到系统:
bash
make install
默认安装到:
text
/usr/local/bin
检查:
bash
redis-server --version
redis-cli --version
4. 创建 Redis 运行用户
Redis 不建议直接使用 root 运行。
bash
useradd -r -s /sbin/nologin redis
如果提示用户已经存在,可以忽略。
5. 创建 Redis 目录
bash
mkdir -p /etc/redis
mkdir -p /data/redis
mkdir -p /var/log/redis
复制默认配置:
bash
cp /soft/redis-8.8.1/redis.conf /etc/redis/redis.conf
设置权限:
bash
chown -R redis:redis /data/redis
chown -R redis:redis /var/log/redis
chown redis:redis /etc/redis/redis.conf
6. 备份 Redis 配置
bash
cp /etc/redis/redis.conf /etc/redis/redis.conf.bak
7. 修改 Redis 配置
编辑:
bash
vi /etc/redis/redis.conf
重点确认或修改以下参数:
ini
bind 127.0.0.1
protected-mode yes
port 6379
daemonize no
supervised no
dir /data/redis
logfile /var/log/redis/redis.log
appendonly yes
其中最重要的是:
ini
daemonize no
supervised no
因为后面 Redis 交给 systemd 托管,不让 Redis 自己后台运行,也不使用 systemd notify 模式。
8. 创建 systemd 服务
创建:
bash
vi /etc/systemd/system/redis.service
写入:
ini
[Unit]
Description=Redis 8.8.1
After=network.target
[Service]
Type=simple
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=on-failure
RestartSec=5
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
这里建议使用:
ini
Type=simple
不要使用:
ini
Type=notify
对应 redis.conf 也保持:
ini
supervised no
9. 重新加载 systemd
bash
systemctl daemon-reload
如果之前 Redis 启动失败过,可以执行:
bash
systemctl reset-failed redis
10. 启动 Redis
bash
systemctl start redis
查看状态:
bash
systemctl status redis -l --no-pager
正常应看到:
text
Active: active (running)
11. 设置开机启动
bash
systemctl enable redis
或者启动并设置开机启动一步完成:
bash
systemctl enable --now redis
检查:
bash
systemctl is-enabled redis
正常:
text
enabled
12. 检查 Redis 端口
bash
ss -lntp | grep 6379
默认只监听本机:
text
127.0.0.1:6379
这是比较安全的配置。
13. 测试 Redis
直接执行:
bash
redis-cli ping
正常返回:
text
PONG
进入 Redis:
bash
redis-cli
测试:
text
127.0.0.1:6379> set name redis881
OK
127.0.0.1:6379> get name
"redis881"
退出:
text
127.0.0.1:6379> exit
检查 Redis 版本:
bash
redis-cli INFO server | grep redis_version
正常:
text
redis_version:8.8.1
最终目录结构
text
/soft/
└── redis-8.8.1/
└── Redis源码
/usr/local/bin/
├── redis-server
├── redis-cli
├── redis-benchmark
└── ...
/etc/redis/
├── redis.conf
└── redis.conf.bak
/data/redis/
└── Redis数据文件
/var/log/redis/
└── redis.log
/etc/systemd/system/
└── redis.service
常用运维命令
以后主要记住这些即可:
bash
systemctl start redis
systemctl stop redis
systemctl restart redis
systemctl status redis
查看日志:
bash
journalctl -u redis
实时查看:
bash
journalctl -u redis -f
查看 Redis 自身日志:
bash
tail -f /var/log/redis/redis.log
检查端口:
bash
ss -lntp | grep 6379
检查进程:
bash
ps -ef | grep redis
测试:
bash
redis-cli ping
查看版本:
bash
redis-server --version
如果以后需要远程访问
当前配置:
ini
bind 127.0.0.1
只能本机访问。
如果 Spring Boot 或其他业务程序也部署在这台服务器上,建议保持不变。
如果业务服务器和 Redis 是不同机器:
ini
bind 0.0.0.0
protected-mode no
但这种方式风险比较大。
或者绑定 Redis 服务器内网 IP,例如:
ini
bind 127.0.0.1 192.168.10.20
protected-mode yes
然后再配置 ACL 或密码,并只允许指定内网服务器访问 6379。