前置说明
Redis 安装目录:/usr/local/redis
配置文件路径:/usr/local/redis/etc/redis.conf
数据 / 日志 / RDB/AOF 目录:/data/redis
Redis 访问密码:xxxxxx(自行替换)
最大内存限制:2GB(根据服务器配置修改)
步骤 1:创建编译工作目录
mkdir -p /opt/redis-sourcecd /opt/redis-source
步骤 2:安装编译必须依赖包
Redis 源码编译需要 C 语言编译器、编译工具、内存分配库、下载工具
sudo dnf install -y gcc gcc-c++ make cmake jemalloc-devel systemd-devel wget unzip
这一步可能会报错:错误:没有任何匹配: jemalloc-devel
Rocky Linux 9 默认 BaseOS + AppStream 源里包名不是 jemalloc-devel,正确包名:jemalloc(开发文件内置在这个包里)
解决方案 1(推荐,直接替换执行)
sudo dnf install -y gcc gcc-c++ make cmake systemd-devel wget unzip jemalloc
删掉了不存在的 jemalloc-devel,只装 jemalloc 即可正常编译 Redis。
解决方案 2:如果上面依旧找不到,先安装 epel 源再装
# 安装epel仓库sudo dnf install -y epel-releasesudo dnf makecache# 再安装编译依赖sudo dnf install -y gcc gcc-c++ make cmake systemd-devel wget unzip jemalloc
步骤 3:下载 Redis 7.2.5 源码并解压
# 下载官方源码包wget https://download.redis.io/releases/redis-7.2.5.tar.gz# 解压文件tar -zxvf redis-7.2.5.tar.gz# 进入源码根目录cd redis-7.2.5
步骤 4:编译 + 安装二进制程序
jemalloc 为高性能内存分配器,生产环境推荐使用
# 编译源码make MALLOC=jemalloc -j$(nproc)# 编译产物安装到指定目录make PREFIX=/usr/local/redis install
执行完成后,/usr/local/redis/bin 下会生成:redis-server、redis-cli、redis-benchmark、redis-check-rdb 等执行文件
步骤 5:创建 Redis 专属文件夹结构
# 存放配置文件mkdir -p /usr/local/redis/etc# 存放持久化文件、运行日志mkdir -p /data/redis# 把源码默认配置文件复制到自定义配置目录cp redis.conf /usr/local/redis/etc/
步骤 6:逐条修改 redis.conf 核心配置(生产安全 + 持久化)
不用手动打开文件修改,直接执行下面一整条 sed 命令批量替换
# 1. 监听所有网卡,支持局域网/远程连接sed -i 's/^bind 127.0.0.1 -::1/bind 0.0.0.0/' /usr/local/redis/etc/redis.conf# 2. 关闭保护模式(配合bind开放地址使用)sed -i 's/^protected-mode yes/protected-mode no/' /usr/local/redis/etc/redis.conf# 3. 设置登录密码sed -i "s/# requirepass foobared/requirepass xxxxxx/" /usr/local/redis/etc/redis.conf# 4. 修改数据文件存储路径sed -i "s|^dir ./|dir /data/redis|" /usr/local/redis/etc/redis.conf# 5. 开启AOF持久化,防止断电丢失AI会话数据sed -i 's/^appendonly no/appendonly yes/' /usr/local/redis/etc/redis.confsed -i 's/# appendfsync everysec/appendfsync everysec/' /usr/local/redis/etc/redis.conf# 6. 开启后台守护进程运行sed -i 's/^daemonize no/daemonize yes/' /usr/local/redis/etc/redis.conf# 7. 修改pid进程文件路径sed -i "s|^pidfile /var/run/redis_6379.pid|pidfile /var/run/redis.pid|" /usr/local/redis/etc/redis.conf# 8. 指定日志输出文件sed -i "s|^# logfile ""|logfile /data/redis/redis.log|" /usr/local/redis/etc/redis.conf # 9. 限制最大内存 + 内存淘汰策略(只淘汰带过期时间的数据)echo "maxmemory 2gb" >> /usr/local/redis/etc/redis.conf echo "maxmemory-policy volatile-lru" >> /usr/local/redis/etc/redis.conf
步骤 7:编写 systemd 服务文件,用 systemctl 管理 Redis 启停
新建 redis 系统服务配置文件
sudo vim /etc/systemd/system/redis.service
粘贴下方全部内容:
[Unit]Description=Redis 7.2.5 Database ServiceAfter=network.target[Service]Type=forkingExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.confExecStop=/usr/local/redis/bin/redis-cli -a xxxxxx shutdownExecReload=/usr/local/redis/bin/redis-cli -a xxxxxx shutdown && /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.confPIDFile=/var/run/redis.pidRestart=alwaysRestartSec=3User=rootGroup=root[Install] WantedBy=multi-user.target
保存退出:Esc→:wq→回车
步骤 8:配置全局环境变量,任意目录直接使用 redis-cli 命令
sudo tee -a /etc/profile <<'EOF'export PATH=$PATH:/usr/local/redis/binEOF# 重载配置立即生效source /etc/profile# 验证全局命令是否生效redis-cli --version
步骤 9:防火墙放行 6379 端口(项目远程连接 Redis 必备)
# 永久放行6379端口sudo firewall-cmd --add-port=6379/tcp --permanent # 重载防火墙规则sudo firewall-cmd --reload# 查看放行端口列表sudo firewall-cmd --list-ports
步骤 10:加载 systemd 配置、设置开机自启、启动 Redis
# 重新加载systemd系统服务配置sudo systemctl daemon-reload# 设置开机自启sudo systemctl enable redis# 启动Redis服务sudo systemctl start redis# 查看运行状态,出现 active (running) 即为启动成功sudo systemctl status redis
步骤 11:本地连接测试 Redis 可用性
# 密码登录redis客户端redis-cli -a xxxxxx --no-auth-warning# 查看redis版本、运行信息127.0.0.1:6379> info server# 读写测试(验证持久化正常)127.0.0.1:6379> set ai_memory_test 测试AI会话存储127.0.0.1:6379> get ai_memory_test# 退出客户端 127.0.0.1:6379> exit
常用 Redis 运维命令
# 启停服务systemctl start redissystemctl stop redissystemctl restart redis# 实时查看运行日志tail -f /data/redis/redis.log# 清理systemd失败状态(启动报错时使用) systemctl reset-failed redis
卸载系统 dnf 自带的旧版 Redis(如之前装过 redis6)
systemctl stop redisdnf remove -y redisrm -rf /var/lib/redis /etc/redis