Redis AOF 文件修复
Redis 启动时报错:
[003380] 16 Mar 09:21:42.596 * Server initialized[003380] 16 Mar 09:21:42.596 * Reading RDB base file on AOF loading...[003380] 16 Mar 09:21:42.596 * Loading RDB produced by version 8.2.2[003380] 16 Mar 09:21:42.596 * RDB age 53298 seconds[003380] 16 Mar 09:21:42.596 * RDB memory usage when created 0.00 Mb[003380] 16 Mar 09:21:42.596 * RDB is base AOF[003380] 16 Mar 09:21:42.596 * Done loading RDB, keys loaded: 11, keys expired: 0.[003380] 16 Mar 09:21:42.596 * DB loaded from base file appendonly.aof.495.base.rdb: 0.004 seconds[003380] 16 Mar 09:21:42.596 # Bad file format reading the append only file appendonly.aof.495.incr.aof: make a backup of your AOF file, then use ./redis-check-aof --fix <filename.manifest>AOF(Append Only File)文件格式损坏或异常,导致 Redis 无法正常启动。
步骤 1:备份 AOF 文件
Section titled “步骤 1:备份 AOF 文件”在修复前,先备份 AOF 文件,防止数据丢失:
# Windowscopy appendonly.aof appendonly.aof.backup
# Linux/Maccp appendonly.aof appendonly.aof.backup步骤 2:使用 redis-check-aof 工具修复
Section titled “步骤 2:使用 redis-check-aof 工具修复”Redis 提供了 `redis-check-aof 工具来修复损坏的 AOF 文件:
Windows
Section titled “Windows”# 进入 Redis 安装目录cd C:\redis
# 修复 AOF 文件redis-check-aof --fix appendonly.aof
# 如果使用 manifest 文件(多文件 AOFredis-check-aof --fix appendonly.aof.manifestLinux/Mac
Section titled “Linux/Mac”# 进入 Redis 安装目录cd /usr/local/redis
# 修复 AOF 文件./redis-check-aof --fix appendonly.aof
# 使用 manifest 文件(多文件 AOF)./redis-check-aof --fix appendonly.aof.manifest步骤 3:确认修复
Section titled “步骤 3:确认修复”修复过程中会提示是否修复,输入 y 确认:
This will shrink the AOF file to the last valid command.Make sure you have a backup before proceeding.Type 'y' to proceed: y步骤 4:重启 Redis
Section titled “步骤 4:重启 Redis”修复完成后,重启 Redis 服务:
# Windowsredis-server.exe redis.windows.conf
# Linux/Mac./redis-server redis.conf1. 配置 AOF 重写策略
Section titled “1. 配置 AOF 重写策略”在 redis.conf 中配置自动重写:
# 自动重写触发条件auto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mb
# AOF 同步策略appendfsync everysec2. 定期备份
Section titled “2. 定期备份”定期备份 AOF 和 RDB 文件:
# 备份脚本示例#!/bin/bashDATE=$(date +%Y%m%d_%H%M%S)cp /var/lib/redis/appendonly.aof /backup/appendonly.aof.$DATEcp /var/lib/redis/dump.rdb /backup/dump.rdb.$DATE3. 监控 AOF 文件大小
Section titled “3. 监控 AOF 文件大小”# 查看 AOF 文件大小ls -lh appendonly.aof
# 手动触发重写redis-cli BGREWRITEAOF- 修复过程会删除 AOF 文件中损坏的命令,可能会丢失部分最近的操作记录
- 如果数据重要,建议先从 RDB 文件恢复数据
- 生产环境建议同时启用 RDB + AOF 混合持久化策略