更新时间:2018年12月19日15时21分 来源:传智播客 浏览次数:
持久化(Persistence),即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘)。持久化的主要应用是将内存中的对象存储在数据库中,或者存储在磁盘文件中、XML数据文件中等等。
持久化是将程序数据在持久状态和瞬时状态间转换的机制。 ----摘自百度百科
| 1 2 | 192.168.17.101:6379> saveOK | 
| 1 | 1349:M 30Jul 17:16:48.935* DB saved on disk | 
| 1 2 | 192.168.17.101:6379> bgsaveBackground saving started | 
| 1 2 3 4 | 1349:M 30Jul 17:14:42.991* Background saving started by pid 13571357:C 30Jul 17:14:42.993* DB saved on disk1357:C 30Jul 17:14:42.993* RDB: 4MB of memory used by copy-on-write1349:M 30Jul 17:14:43.066* Background saving terminated with success | 
注:bgsave命令执行期间
SAVE命令会被拒绝
不能同时执行两个BGSAVE命令
不能同时执行BGREWRITEAOF和BGSAVE命令
| 1 2 3 | save 9001# 900秒内有至少有 1个键被改动save 30010# 300秒内有至少有 10个键被改动save 6010000# 60秒内有至少有 1000个键被改动 | 
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 | ################################ SNAPSHOTTING  ################################# 触发自动保存快照# save <seconds> <changes># save <秒> <修改的次数>save 9001save 30010save 6010000# 设置在保存快照出错时,是否停止redis命令的写入stop-writes-on-bgsave-error yes# 是否在导出.rdb数据库文件的时候采用LZF压缩rdbcompression yes#  是否开启CRC64校验rdbchecksum yes# 导出数据库的文件名称dbfilename dump.rdb# 导出的数据库所在的目录dir ./ | 
翻译来自http://www.redis.cn
| 1 | appendonly yes | 
| 1 2 | 192.168.17.101:6379> config set appendonly yesOK | 
| 1 2 | 192.168.17.101:6379> set learnRedis testAOFOK | 
| 01 02 03 04 05 06 07 08 09 10 11 12 | *2$6SELECT$10*3$3set$10learnRedis$7testAOF | 
因为为了提高文件的写入效率,在现代操作系统中,当用户调用write函数,将一些数据写入到文件的时候,os通常会将写入数据暂时保存在一个内存缓冲区里面(例如,unix系统实现在内核中设有缓冲区高速缓存或页高速缓存,当我们向文件写入数据时,内核通常先将数据复制到缓冲区中,然后排入队列,晚些时候再写入磁盘),这种方式称为延迟写,等到缓冲区的空间被填满,或者超过了指定的时限,或者内核需要重用缓冲区存放其它磁盘块数据时,才会真正将缓冲区中的所有数据写入到磁盘里面。
文件写入:只是写入到了内存缓冲区,可能还没有写到文件所拥有的磁盘数据块上
文件同步:将缓冲区中的内容冲洗到磁盘上

这时可能会出现一个问题。服务器可能在程序正在对 AOF 文件进行写入时停机,造成了 AOF 文件出错,那么 Redis 在重启时会拒绝载入这个 AOF 文件,从而确保数据的一致性不会被破坏 当发生这种情况时, 可以用以下方法来修复出错的 AOF 文件:
- 为现有的 AOF 文件创建一个备份。
- 使用 Redis 附带的 redis-check-aof 程序,对原来的 AOF 文件进行修复: redis-check-aof –fix
- (可选)使用 diff -u 对比修复后的 AOF 文件和原始 AOF 文件的备份,查看两个文件之间的不同之处。
- 重启 Redis 服务器,等待服务器载入修复后的 AOF 文件,并进行数据恢复。
| 1 | aof-load-truncated yes | 
| 1 2 | 192.168.17.101:6379> BGREWRITEAOFBackground append only file rewriting started | 
| 1 2 | auto-aof-rewrite-percentage 100#当前AOF文件大小和上一次重写时AOF文件大小的比值auto-aof-rewrite-min-size 64mb  #文件的最小体积 | 
| 1 2 3 4 5 6 7 8 9 | 1349:M 30Jul 17:19:25.311* Background append only file rewriting started by pid 13921349:M 30Jul 17:19:25.379* AOF rewrite child asks to stop sending diffs.1392:C 30Jul 17:19:25.379* Parent agreed to stop sending diffs. Finalizing AOF...1392:C 30Jul 17:19:25.380* Concatenating 0.00MB of AOF diff received from parent.1392:C 30Jul 17:19:25.380* SYNC append only file rewrite performed1392:C 30Jul 17:19:25.381* AOF rewrite: 4MB of memory used by copy-on-write1349:M 30Jul 17:19:25.466* Background AOF rewrite terminated with success1349:M 30Jul 17:19:25.467* Residual parent diff successfully flushed to the rewritten AOF (0.00MB)1349:M 30Jul 17:19:25.467* Background AOF rewrite finished successfully | 
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 | ############################## APPEND ONLY MODE ################################ 是否开启AOF功能appendonly no# AOF文件件名称appendfilename "appendonly.aof"# 写入AOF文件的三种方式# appendfsync alwaysappendfsync everysec# appendfsync no# 重写AOF时,是否继续写AOF文件no-appendfsync-on-rewrite no# 自动重写AOF文件的条件auto-aof-rewrite-percentage 100#百分比auto-aof-rewrite-min-size 64mb #大小# 是否忽略最后一条可能存在问题的指令aof-load-truncated yes |