在 CentOS 上配置 Redis 持久化,主要是修改 redis.conf 文件,Redis 支持两种主要的持久化方式:
特点:定时把内存数据生成快照保存到磁盘,适合备份,恢复快,但可能丢最近数据。
vi /etc/redis.conf
# 或
vi /etc/redis/redis.conf
save 900 1 # 900秒内有1次修改
save 300 10 # 300秒内有10次修改
save 60 10000 # 60秒内有10000次修改
dbfilename dump.rdb
dir /var/lib/redis
systemctl restart redis
特点:记录每条写命令,数据更安全,文件会变大,恢复稍慢。
vi /etc/redis.conf
appendonly yes
appendfilename "appendonly.aof"
# 每秒同步(推荐)
appendfsync everysec
# 可选:
# appendfsync always # 每次写都同步(最安全,最慢)
# appendfsync no # 由系统决定(最快,可能丢数据)
systemctl restart redis
结合 RDB + AOF 优点:
aof-use-rdb-preamble yes
redis-cli
> CONFIG GET save
> CONFIG GET appendonly
> INFO persistence
Redis 用户通常是 redis:
chown -R redis:redis /var/lib/redis
如果你用的是 源码安装 / Docker / 特定版本,可以告诉我,我给你更精准的配置。