一、redis的持久化(RDB)
- redis为什么要做持久化?参考【大海_sea】的博客
redis基于内存操作,默认是会进行持久化(默认rdb),相当于一个自动备份,将内存中的数据写入到磁盘中,然后每次重新启动的时候进行加载。
-
如何查找和查看redis的快照文件dump.rdb
查找 linux命令find,可以查看【MR-GTT】博客[root@izuf6aisea6jr1yf1z5p3mz ~]# find / -name dump.rdb /opt/redis-6.0.8/dump.rdb /opt/redis-6.0.8/src/dump.rdb
查看 A:linux上查看;B:windows本地查看
A: 参考博客
安装 python-lzf :加快解析速度pip install rdbtools
安装中提示:
Successfully installed rdbtools-0.1.15 redis-3.5.3 You are using pip version 9.0.1, however version 20.2.4 is available. You should consider upgrading via the 'pip install --upgrade pip' command. [root@izuf6aisea6jr1yf1z5p3mz redis-6.0.8]# ^C [root@izuf6aisea6jr1yf1z5p3mz redis-6.0.8]# pip install --upgrade pip
安装完成后 执行后导出成json格式
rdb --command json /opt/redis-6.0.8/dump.rdb > /opt/redis-6.0.8/dump.json
WARNING: python-lzf package NOT detected. Parsing dump file will be very slow unless you install it. To install, run the following command:pip install python-lzf
需要先安装yum install python-devel,再安装pip install python-lzf
安装过程中弹出确认框,请选择yes导出成csv格式,分析内存快照(MySql)
rdb -c memory /opt/redis-6.0.8/dump.rdb --bytes 128 -f /opt/redis-6.0.8/dump_memory.csv
生成CSV格式的内存报告。包含的列有:数据库ID,数据类型,key,内存使用量(byte),编码。内存使用量包含key、value和其他值。
如图:
注意事项:参考文章
运行时会 -c memory生成一个CSV报告,其中包含该密钥使用的近似内存。–bytes C并且‘–largest N可用于将输出限制为大于C字节的键或N个最大键。
由于redis中的key为本地测试用,实际限制128的时候,过滤出来为空,可将128改为1。 -
测试持久化(默认的rdb方式)
在程序中用redisTemplate set 一个key,然后通过看下dump快照里面是否存在,一段时间后,再看看是否同步到了dump快照里-
导出text
rdb --command diff /opt/redis-6.0.8/dump.rdb | sort > dump1.txt
执行程序后
@RequestMapping("/redis") public String TestRedis(){ redisUtil.set("testRDB","Test22222222222"); return "helloRedis"; }
rdb --command diff /opt/redis-6.0.8/dump.rdb | sort > dump2.txt
导出的数据一致,说明还没有同步到磁盘上:
- 客户端连接查看key
127.0.0.1:6379> get testRDB "\"Test22222222222\""
-
-
配置rdb更新频率(redis.conf)
# Save the DB on disk: # # save <seconds> <changes> # # Will save the DB if both the given number of seconds and the given # number of write operations against the DB occurred. # # In the example below the behaviour will be to save: # after 900 sec (15 min) if at least 1 key changed # after 300 sec (5 min) if at least 10 keys changed # after 60 sec if at least 10000 keys changed # # Note: you can disable saving completely by commenting out all "save" lines. # # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # # save "" save 900 1 save 300 10 save 60 10000
save+时间内(s)+次数可以触发rdb更新条件,上述的三个save条件同时生效;
不想使用rdb方式存储,可以将写成【save “”】的方式。 -
文件存储修改
# The filename where to dump the DB dbfilename dump.rdb # Remove RDB files used by replication in instances without persistence # enabled. By default this option is disabled, however there are environments # where for regulations or other security concerns, RDB files persisted on # disk by masters in order to feed replicas, or stored on disk by replicas # in order to load them for the initial synchronization, should be deleted # ASAP. Note that this option ONLY WORKS in instances that have both AOF # and RDB persistence disabled, otherwise is completely ignored. # # An alternative (and sometimes better) way to obtain the same effect is # to use diskless replication on both master and replicas instances. However # in the case of replicas, diskless is not always an option. rdb-del-sync-files no # The working directory. # # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. dir ./
文件名配置:dump.rdb
路径配置:./ ./是指用户所在的当前目录
二、redis的持久化(AOF)
-
AOF的特点:
AOP模式做持久化写入磁盘文件的是写的操作命令,也就是说使用AOF会将客户端对于redis的操作(查询除外)以一个字符串的形式记录到磁盘的文件中去,而在启动redis的时候会去读取这一个文件,将命令执行。 -
开启AOF(redis.conf)
appendonly no # The name of the append only file (default: "appendonly.aof") appendfilename "appendonly.aof"
改为:appendonly yes
文件名也可以自定义一个 -
三种模式
# Redis supports three different modes: # # no: don't fsync, just let the OS flush the data when it wants. Faster. # always: fsync after every write to the append only log. Slow, Safest. # everysec: fsync only one time every second. Compromise. # # The default is "everysec", as that's usually the right compromise between # speed and data safety. It's up to you to understand if you can relax this to # "no" that will let the operating system flush the output buffer when # it wants, for better performances (but if you can live with the idea of # some data loss consider the default persistence mode that's snapshotting), # or on the contrary, use "always" that's very slow but a bit safer than # everysec. # # More details please check the following article: # http://antirez.com/post/redis-persistence-demystified.html # # If unsure, use "everysec". # appendfsync always appendfsync everysec # appendfsync no
三种模式分别为:
no: 不同步写入(fsync)让操作系统在需要的时候刷新数据。更快。
always: 同步写入( fsync)在每次写入append only日志后执行。缓慢的,安全的。
everysec: 同步写入( fsync)每秒只有一次。推荐的适中方案。 -
重写机制
# 当AOF的持久化文件大小的增长率大于此配置时,自动开启重写, # redis会自动执行“BGREWRITEAOF”命令; auto-aof-rewrite-percentage 100 # 当AOF的持久化文件大小大于此配置时,自动开启重写, # redis会自动执行“BGREWRITEAOF”命令; auto-aof-rewrite-min-size 64mb
由于AOF模式持久化记录的是操作命令,比如说当有这两个命令set key “value1”, set key "value2"依次执行后,实际上要恢复数据只需要执行set key "value2"即可。经过类似的压缩,可以为原本已经很大的文件“瘦身”,以下的内容,即为执行此“瘦身”操作的配置。
-
AOF模式的优缺点
优点:AOF是增量操作 append only,速度更快
AOF模式可以更好的保护数据不丢失,在redis因为非正常原因挂掉时,其保存数据的完整度理论上高于RDB模式,因为采用appendfsync everysec去写入持久化文件,最多丢失一秒到两秒的数据;而RDB模式丢失的数据根据其配置的写入频率决定;缺点:
而AOF的缺点如下:
(1)对于同样的数据,通常AOF文件的大小回比RDB的要大;
(2)因为AOF存的是命令而不是数据,所以恢复数据时可能较慢 -
AOF文件未显示
需要注意的是:即使已经在redis.conf文件中把appendonly 从no改为了yes,把服务器重启了的情况也没有appendonly.aof文件时,必须要执行
redis-cli config set appendonly yes
这两个命令后才会在安装目录下出现appendonly.aof文件 -
测试是否写入AOF文件
执行代码redisUtil.set("testRDB2","Test22222222222");
AOF文件内容
testRDB2^Q"Test22222222222"
参考博文:
https://blog.csdn.net/y506798278/article/details/103541097
https://blog.csdn.net/gxiaop/article/details/54978997
https://www.cnblogs.com/cheyunhua/p/10598181.html
http://www.mamicode.com/info-detail-1968985.html