Redis 配置全攻略(redis.conf 详解 + 生产最佳实践)
目标:让你 10 分钟搞懂 所有核心配置项,并能 一键生成生产级配置文件。
一、配置文件位置
| 安装方式 | 默认路径 |
|---|
| Ubuntu/Debian | /etc/redis/redis.conf |
| CentOS/RHEL | /etc/redis.conf |
| macOS (Homebrew) | /usr/local/etc/redis.conf |
| 源码安装 | redis.conf(在源码根目录) |
| Docker | 需挂载 -v /path/redis.conf:/usr/local/etc/redis.conf |
二、核心配置分类(共 12 大类)
| 分类 | 关键配置项 |
|---|
| 网络 | bind port protected-mode |
| 安全 | requirepass rename-command |
| 内存 | maxmemory maxmemory-policy |
| 持久化 | save appendonly appendfsync |
| 主从 | replicaof replica-read-only |
| 客户端 | timeout maxclients |
| 日志 | loglevel logfile |
| 慢查询 | slowlog-log-slower-than |
| 高级 | tcp-keepalive repl-backlog-size |
三、完整生产级 redis.conf(推荐直接复制)
# ==================== 基础网络 ====================
bind 127.0.0.1 # 生产建议改为内网 IP,如 10.0.0.10
port 6379
protected-mode yes
tcp-backlog 511
timeout 0
tcp-keepalive 300
# ==================== 安全认证 ====================
requirepass your_strong_password_2025 # 必设!至少12位
# 重命名危险命令(防止误操作)
rename-command FLUSHALL "FLUSHALL_PROD"
rename-command FLUSHDB "FLUSHDB_PROD"
rename-command SHUTDOWN "SHUTDOWN_PROD"
# ==================== 内存管理 ====================
maxmemory 2gb # 建议设为物理内存 70%
maxmemory-policy allkeys-lru # 推荐策略:LRU 淘汰
maxmemory-samples 5 # 采样数,越大越精确
# ==================== 持久化(RDB + AOF)================
# RDB 快照(适合大内存冷备)
save 900 1
save 300 10
save 60 10000
dir /var/lib/redis/ # 数据目录
dbfilename dump.rdb
# AOF 日志(推荐开启,更安全)
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec # 每秒同步,性能与安全平衡
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# AOF 自动重写优化
aof-rewrite-incremental-fsync yes
aof-load-truncated yes
# ==================== 主从复制 ====================
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-backlog-size 1gb # 建议设大,防全量复制
repl-backlog-ttl 3600
# ==================== 客户端连接 ====================
maxclients 10000 # 根据 CPU 核数 * 10000
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# ==================== 日志与监控 ====================
loglevel notice # debug, verbose, notice, warning
logfile /var/log/redis/redis.log
syslog-enabled no
# 慢查询(单位:微秒)
slowlog-log-slower-than 10000 # 10ms 以上记录
slowlog-max-len 128
# ==================== 其他优化 ====================
daemonize yes # 后台运行
pidfile /var/run/redis_6379.pid
databases 16
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
replica-lazy-flush yes
# 事件循环优化
hz 10 # 默认10,生产可调高到100
四、配置项深度解析(必知必会)
1. 网络与安全
| 配置 | 说明 | 推荐值 |
|---|
bind 127.0.0.1 | 绑定 IP | 内网 IP |
protected-mode yes | 保护模式(无密码+公网=拒绝连接) | yes |
requirepass xxx | 密码 | 必设! |
rename-command | 重命名命令防误操作 | 生产必用 |
2. 内存管理
maxmemory 2gb
maxmemory-policy allkeys-lru
| 策略 | 说明 |
|---|
allkeys-lru | 推荐:所有 key 参与 LRU |
volatile-lru | 仅有过期时间的 key 参与 |
noeviction | 内存满时返回错误 |
3. 持久化:RDB vs AOF
| 方式 | 优点 | 缺点 |
|---|
| RDB(快照) | 体积小、恢复快 | 可能丢失最后几分钟数据 |
| AOF(日志) | 更安全、可读性强 | 文件大、恢复慢 |
生产建议:双开启,AOF 为主,RDB 备份
4. 主从复制
# 从节点配置
replicaof 192.168.1.10 6379
requirepass slave_password
masterauth master_password
5. 客户端连接
maxclients 10000
timeout 0 # 0=永不超时
实际连接数 ≈ ulimit -n – 1024
五、一键生成配置脚本
# 生成生产级 redis.conf
curl -fsSL https://raw.githubusercontent.com/redis/redis/7.4/redis.conf \
| sed -e 's/# requirepass.*/requirepass your_strong_password/' \
-e 's/bind 127.0.0.1/bind 0.0.0.0/' \
-e 's/# maxmemory <bytes>/maxmemory 2gb/' \
-e 's/# maxmemory-policy noeviction/maxmemory-policy allkeys-lru/' \
-e 's/# appendonly no/appendonly yes/' \
> /etc/redis/redis.conf
六、Docker 生产配置示例
# docker-compose.yml
version: '3.8'
services:
redis:
image: redis:7.4
container_name: redis-prod
restart: unless-stopped
ports:
- "6379:6379"
volumes:
- ./redis.conf:/usr/local/etc/redis/redis.conf
- redis-data:/data
command: redis-server /usr/local/etc/redis/redis.conf
environment:
- TZ=Asia/Shanghai
volumes:
redis-data:
七、验证配置生效
# 重启 Redis
sudo systemctl restart redis
# 查看配置
redis-cli CONFIG GET requirepass
redis-cli CONFIG GET maxmemory
redis-cli INFO memory
redis-cli SLOWLOG GET 5
八、常见配置错误
| 错误 | 后果 | 修正 |
|---|
bind 0.0.0.0 + 无密码 | 公网被入侵 | 加密码 + 防火墙 |
maxmemory 未设 | 内存爆掉 | 设为物理内存 70% |
appendonly no | 宕机数据丢失 | 开启 AOF |
save "" | 禁用 RDB | 至少保留一个 |
九、监控配置(配合 Prometheus)
# 开启统计
latency-tracking-percentiles 50 90 99
完成!你已掌握 Redis 配置!
# 最终检查清单
redis-cli PING # PONG
redis-cli AUTH pwd # OK
redis-cli INFO memory # used_memory < maxmemory
redis-cli INFO persistence # aof_enabled:1
下一步推荐:
- Redis 性能调优
- 主从 + Sentinel 高可用
- Redis Cluster 集群搭建
需要我 生成专属配置(如 64GB 内存、10万 QPS)?发服务器信息给我!