Redis 缓存运维指南
目录
- Redis 安装与配置
- 核心配置详解
- 数据持久化
- 主从复制
- 哨兵模式
- 集群模式
- 性能优化
- 监控与诊断
- 安全加固
1. Redis 安装与配置
1.1 源码安装
1 2 3 4 5 6 7 8 9 10 11 12
| wget https://download.redis.io/releases/redis-7.0.5.tar.gz tar xzf redis-7.0.5.tar.gz cd redis-7.0.5
make make install
redis-server --version redis-cli --version
|
1.2 系统配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| cat >> /etc/sysctl.conf << EOF vm.overcommit_memory = 1 net.core.somaxconn = 65535 vm.swappiness = 0 EOF sysctl -p
echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag
useradd -r -s /sbin/nologin redis mkdir -p /var/lib/redis /var/log/redis chown -R redis:redis /var/lib/redis /var/log/redis
|
1.3 配置文件
1 2 3 4 5 6 7 8 9
| cp redis.conf /etc/redis/redis.conf
sed -i 's/daemonize no/daemonize yes/' /etc/redis/redis.conf sed -i 's/# bind 127.0.0.1/bind 0.0.0.0/' /etc/redis/redis.conf sed -i 's/protected-mode yes/protected-mode no/' /etc/redis/redis.conf sed -i 's|dir ./|dir /var/lib/redis|' /etc/redis/redis.conf sed -i 's|logfile ""|logfile /var/log/redis/redis.log|' /etc/redis/redis.conf
|
1.4 Systemd 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [Unit] Description=Redis In-Memory Data Store After=network.target
[Service] User=redis Group=redis ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown Restart=always
[Install] WantedBy=multi-user.target
|
1 2 3 4 5
| systemctl daemon-reload systemctl start redis systemctl enable redis systemctl status redis
|
2. 核心配置详解
2.1 网络配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # 绑定地址 bind 127.0.0.1 ::1
# 端口 port 6379
# 超时时间(0 表示永不超时) timeout 0
# TCP keepalive tcp-keepalive 300
# 保护模式 protected-mode yes
|
2.2 内存配置
1 2 3 4 5 6 7 8 9 10 11 12
| # 最大内存 maxmemory 2gb
# 内存淘汰策略 # volatile-lru: 对设置了过期时间的 key 使用 LRU 算法 # allkeys-lru: 对所有 key 使用 LRU 算法 # volatile-ttl: 对设置了过期时间的 key 使用 TTL 算法 # noeviction: 不淘汰任何 key(默认) maxmemory-policy allkeys-lru
# LRU 采样数量 maxmemory-samples 5
|
2.3 连接配置
1 2 3 4 5 6 7 8 9 10 11
| # 最大客户端连接数 maxclients 10000
# 密码认证 requirepass your_password
# 重命名危险命令 rename-command FLUSHALL "" rename-command FLUSHDB "" rename-command CONFIG "" rename-command DEBUG ""
|
3. 数据持久化
3.1 RDB 快照
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| # RDB 配置 save 900 1 # 900 秒内至少 1 个 key 变化 save 300 10 # 300 秒内至少 10 个 key 变化 save 60 10000 # 60 秒内至少 10000 个 key 变化
# RDB 文件名 dbfilename dump.rdb
# RDB 压缩 rdbcompression yes
# RDB 校验和 rdbchecksum yes
# 持久化目录 dir /var/lib/redis
|
1 2 3 4 5 6
| redis-cli BGSAVE redis-cli SAVE
redis-cli INFO persistence
|
3.2 AOF 日志
1 2 3 4 5 6 7 8 9 10 11 12 13
| # AOF 配置 appendonly yes appendfilename "appendonly.aof"
# AOF 同步策略 # always: 每次写入都同步(最安全,性能最差) # everysec: 每秒同步一次(推荐) # no: 由操作系统决定何时同步(性能最好,安全性最差) appendfsync everysec
# AOF 重写 auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
|
1 2 3 4 5
| redis-cli BGREWRITEAOF
redis-cli INFO persistence
|
3.3 混合持久化(Redis 4.0+)
1 2
| # 启用 RDB-AOF 混合持久化 aof-use-rdb-preamble yes
|
4. 主从复制
4.1 配置从节点
1 2 3 4 5 6 7 8 9 10 11 12
| # 从节点配置 replicaof 192.168.1.100 6379 masterauth your_master_password
# 从节点只读 replica-read-only yes
# 复制超时 repl-timeout 60
# 复制 ping 间隔 repl-ping-replica-period 10
|
4.2 动态配置
1 2 3 4 5
| redis-cli SLAVEOF 192.168.1.100 6379
redis-cli SLAVEOF NO ONE
|
4.3 监控复制
1 2 3 4 5 6 7 8 9 10
| redis-cli INFO replication
role
role
|
5. 哨兵模式
5.1 哨兵配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # sentinel.conf port 26379 daemonize yes pidfile /var/run/redis/redis-sentinel.pid logfile /var/log/redis/sentinel.log
# 监控主节点 sentinel monitor mymaster 192.168.1.100 6379 2
# 认证 sentinel auth-pass mymaster your_password
# 故障判定时间(毫秒) sentinel down-after-milliseconds mymaster 5000
# 并行同步数 sentinel parallel-syncs mymaster 1
# 故障转移超时(毫秒) sentinel failover-timeout mymaster 10000
# 通知脚本 sentinel notification-script mymaster /var/redis/notify.sh
|
5.2 启动哨兵
1 2 3 4 5
| redis-sentinel /etc/redis/sentinel.conf
systemctl start redis-sentinel
|
5.3 哨兵命令
1 2 3 4 5 6 7 8 9 10 11
| redis-cli -p 26379 SENTINEL masters
redis-cli -p 26379 SENTINEL slaves mymaster
redis-cli -p 26379 SENTINEL get-master-addr-by-name mymaster
redis-cli -p 26379 SENTINEL failover mymaster
|
6. 集群模式
6.1 创建集群
1 2 3 4 5 6 7 8
| redis-cli --cluster create \ 192.168.1.100:6379 192.168.1.101:6379 192.168.1.102:6379 \ 192.168.1.103:6379 192.168.1.104:6379 192.168.1.105:6379 \ --cluster-replicas 1
redis-cli --cluster create 192.168.1.100:6379 --cluster-empty
|
6.2 节点配置
1 2 3 4 5 6 7 8 9 10 11 12
| # 启用集群模式 cluster-enabled yes
# 集群配置文件 cluster-config-file nodes.conf
# 节点超时时间(毫秒) cluster-node-timeout 5000
# 集群密码 masterauth your_password requirepass your_password
|
6.3 集群管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| redis-cli -c -p 6379 CLUSTER INFO redis-cli -c -p 6379 CLUSTER NODES
redis-cli --cluster check 192.168.1.100:6379
redis-cli --cluster add-node 192.168.1.106:6379 192.168.1.100:6379
redis-cli --cluster del-node 192.168.1.100:6379 <node-id>
redis-cli --cluster reshard 192.168.1.100:6379
redis-cli --cluster failover 192.168.1.100:6379
|
7. 性能优化
7.1 内存优化
1 2 3 4 5 6 7 8 9
| # 使用更高效的数据结构 # 例如:hash-max-ziplist-entries, hash-max-ziplist-value
hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64
|
7.2 网络优化
1 2 3 4 5 6 7 8
| # TCP backlog tcp-backlog 65535
# 禁用 THP echo never > /sys/kernel/mm/transparent_hugepage/enabled
# 增加文件描述符限制 ulimit -n 65535
|
7.3 批量操作
1 2 3 4 5 6 7 8 9
| redis-cli --pipe < commands.txt
redis-cli MSET key1 value1 key2 value2 key3 value3 redis-cli MGET key1 key2 key3
redis-cli EVAL "return redis.call('SET', KEYS[1], ARGV[1])" 1 key1 value1
|
8. 监控与诊断
8.1 INFO 命令
1 2 3 4 5 6 7 8 9
| redis-cli INFO
redis-cli INFO memory redis-cli INFO persistence redis-cli INFO replication redis-cli INFO stats redis-cli INFO keyspace
|
8.2 慢查询日志
1 2 3 4 5
| # 慢查询阈值(微秒) slowlog-log-slower-than 10000
# 慢查询记录数 slowlog-max-len 128
|
1 2 3 4
| redis-cli SLOWLOG GET 10 redis-cli SLOWLOG LEN redis-cli SLOWLOG RESET
|
8.3 实时监控
1 2 3 4 5 6 7
| redis-cli --stat redis-cli --bigkeys redis-cli --hotkeys
redis-cli --intrinsic-latency 100
|
8.4 监控工具
1 2 3 4 5 6 7 8
| redis-monitor
redis_exporter --redis.addr=localhost:6379
|
9. 安全加固
9.1 访问控制
1 2 3 4 5 6 7 8 9 10 11 12
| # 设置密码 requirepass StrongP@ssw0rd123
# 绑定内网 IP bind 192.168.1.100
# 禁用危险命令 rename-command FLUSHALL "" rename-command FLUSHDB "" rename-command CONFIG "" rename-command DEBUG "" rename-command SHUTDOWN SHUTDOWN_SECRET
|
9.2 网络安全
1 2 3 4 5 6 7
| firewall-cmd --add-port=6379/tcp --permanent firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port port="6379" protocol="tcp" accept' --permanent firewall-cmd --reload
iptables -A INPUT -p tcp --dport 6379 -s 192.168.1.0/24 -j ACCEPT
|
9.3 SSL/TLS(Redis 6.0+)
1 2 3 4 5 6 7 8 9
| # 启用 TLS tls-port 6379 port 0
tls-cert-file /etc/redis/tls/redis.crt tls-key-file /etc/redis/tls/redis.key tls-ca-cert-file /etc/redis/tls/ca.crt
tls-auth-clients yes
|
文档版本: 1.0
最后更新: 2026-02-27
适用版本: Redis 6.0+, 7.0+