下面按CentOS 7/8常见场景,给你一份Redis 安装与配置实战指南,从安装到常用配置、安全、开机自启都有。
# CentOS 7
yum install -y epel-release
yum install -y redis
# CentOS 8
dnf install -y redis
yum install -y gcc make
wget https://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 install
/etc/redis.confredis-7.2.4/redis.conf编辑配置文件:
vi /etc/redis.conf
bind 127.0.0.1
# 或允许内网访问
bind 127.0.0.1 192.168.1.100
⚠️ 不要直接 bind 0.0.0.0 且不设密码
requirepass yourpassword
登录方式:
redis-cli -a yourpassword
daemonize yes
port 6379
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
maxmemory 1gb
maxmemory-policy allkeys-lru
systemctl start redis
systemctl enable redis
systemctl status redis
redis-server /path/redis.conf
或写 systemd 服务(推荐)
firewall-cmd --zone=public --add-port=6379/tcp --permanent
firewall-cmd --reload
redis-cli
127.0.0.1:6379> ping
PONG
✅ 设置密码
✅ 禁止公网暴露
✅ 使用非默认端口
✅ 限制 bind IP
✅ 定期备份 RDB/AOF
你可以直接说:“我要生产环境配置” 或 “我要主从”。