Redis 配置

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

下一步推荐

  1. Redis 性能调优
  2. 主从 + Sentinel 高可用
  3. Redis Cluster 集群搭建

需要我 生成专属配置(如 64GB 内存、10万 QPS)?发服务器信息给我!

文章已创建 2481

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部