CentOS7.9:Redis服务器部署结构化实战教程

发布时间:2026/7/24 17:11:12

CentOS7.9:Redis服务器部署结构化实战教程 一、项目概述1.1 业务场景说明在 CentOS7.9 系统编译安装 Redis完成基础配置、安全设置、持久化配置、开机自启可以部署单机、主从复制、哨兵模式配合 ks‑cfg 脚本实现 PXE 批量自动化部署统一机房 Redis 环境标准。1.2 环境参数项目参数操作系统CentOS Linux release 7.9‑2009Redis 版本redis‑5.0.10安装目录/usr/local/redis配置目录/usr/local/redis/conf数据目录/usr/local/redis/data服务运行用户root端口6379二、安装依赖环境编译 Redis 需要 gcc 编译环境先安装依赖包。shellyum install -y gcc gcc-c make三、编译安装 Redis3.1 下载源码包解压编译shellcd /usr/local/src#下载源码包wget https://download.redis.io/releases/redis-5.0.10.tar.gz#解压压缩包tar -zxf redis-5.0.10.tar.gzcd redis-5.0.10#编译解决内存分配报错make MALLOClibc#编译安装指定安装目录make install PREFIX/usr/local/redis3.2 创建目录结构整理配置文件shell#创建配置目录、数据目录mkdir -p /usr/local/redis/conf /usr/local/redis/data#复制模板配置文件cp redis.conf /usr/local/redis/conf/redis.conf四、修改 redis.conf 核心配置生产必改项shellvi /usr/local/redis/conf/redis.conf修改以下参数ini#开启后台运行daemonize yes#对外开放访问禁止只绑定本地bind 0.0.0.0#进程文件路径pidfile /var/run/redis_6379.pid#日志文件logfile /usr/local/redis/data/redis.log#RDB文件存放目录dir /usr/local/redis/data#设置登录密码requirepass Admin123456#开启AOF持久化appendonly yesappendfsync everysec#开启混合持久化aof-use-rdb-preamble yes五、启动与停止 Redis 服务5.1 启动服务shell/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf5.2 正常关闭服务优先方式刷新持久化数据shellredis-cli -a Admin123456 shutdown5.3 强制关闭生产尽量不用shellkill -9 $(pgrep redis-server)5.4 连接测试shellredis-cli -a Admin123456六、配置 systemd 系统服务设置开机自启6.1 创建 redis.service 文件shellvi /etc/systemd/system/redis.service写入内容ini[Unit]DescriptionRedis ServerAfternetwork.target[Service]TypeforkingExecStart/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.confExecStop/usr/local/redis/bin/redis-cli -a Admin123456 shutdownPrivateTmptrue[Install]WantedBymulti-user.target6.2 加载配置并设置开机自启shell#刷新systemd配置systemctl daemon-reload#设置开机自启并启动服务systemctl enable --now redis#查看运行状态systemctl status redis七、防火墙配置7.1 测试环境直接关闭防火墙shellsystemctl stop firewalldsystemctl disable firewalldsetenforce 0sed -i s/^SELINUXenforcing/SELINUXdisabled/ /etc/selinux/config7.2 生产环境放行端口shellfirewall-cmd --permanent --add-port6379/tcpfirewall-cmd --reload八、多实例部署6380、6381主从环境复制配置文件修改端口、pid 文件、日志目录、数据目录新建 systemd 服务文件 redis‑6380.service启动多个实例搭建主从复制架构。shellcp /usr/local/redis/conf/redis.conf /usr/local/redis/conf/redis‑6380.confsed -i s/6379/6380/g /usr/local/redis/conf/redis‑6380.conf九、ks‑cfg 无人值守批量部署脚本PXE 装机在 % post 段写入脚本新装服务器自动部署 Redis。shell%postyum install -y gcc gcc-c makecd /usr/local/srcwget https://download.redis.io/releases/redis-5.0.10.tar.gztar -zxf redis-5.0.10.tar.gzcd redis-5.0.10make MALLOClibcmake install PREFIX/usr/local/redismkdir -p /usr/local/redis/conf /usr/local/redis/datacp redis.conf /usr/local/redis/conf/redis.confsed -i s/daemonize no/daemonize yes/ /usr/local/redis/conf/redis.confsed -i s/bind 127.0.0.1/bind 0.0.0.0/ /usr/local/redis/conf/redis.confecho requirepass Admin123456 /usr/local/redis/conf/redis.confecho appendonly yes /usr/local/redis/conf/redis.conf#创建systemd服务文件cat /etc/systemd/system/redis.service EOF[Unit]DescriptionRedis ServerAfternetwork.target[Service]TypeforkingExecStart/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.confExecStop/usr/local/redis/bin/redis-cli -a Admin123456 shutdownPrivateTmptrue[Install]WantedBymulti-user.targetEOFsystemctl daemon-reloadsystemctl enable --now redis%end十、高频故障总结故障 1make 编译时报 malloc 报错原因缺少 libc 环境解决编译命令使用make MALLOClibc。故障 2外网无法连接 Redis原因bind 绑定 [127.0.0.1](127.0.0.1)防火墙端口未放行解决bind 改为 [0.0.0.0](0.0.0.0)防火墙放行 6379 端口。故障 3systemd 启动失败原因service 文件路径写错密码不一致解决核对 ExecStart 和 ExecStop 路径以及登录密码。故障 4重启服务器 Redis 无法自动启动原因没有执行 daemon‑reload 刷新配置解决执行systemctl daemon-reload再设置 enable 开机项。

相关新闻