快照(Snapshot)
启用redis的snapshot,只需简单的几个参数即可实现,大多参数默认即可。
1、配置快照保存策略:
1 | # Save the DB on disk: |
2 | # |
3 | # save <seconds> <changes> |
4 | # |
5 | # Will save the DB if both the given number of seconds and the given |
6 | # number of write operations against the DB occurred. |
7 | # |
8 | # In the example below the behaviour will be to save: |
9 | # after 900 sec (15 min) if at least 1 key changed |
10 | # after 300 sec (5 min) if at least 10 keys changed |
11 | # after 60 sec if at least 10000 keys changed |
12 | # |
13 | # Note: you can disable saving at all commenting all the "save" lines. |
14 |
15 | save 900 1 |
16 | save 300 10 |
17 | save 60 10000 |
2、设置dump文件是否压缩:
1 | # Compress string objects using LZF when dump .rdb databases? |
2 | # For default that's set to 'yes' as it's almost always a win. |
3 | # If you want to save some CPU in the saving child set it to 'no' but |
4 | # the dataset will likely be bigger if you have compressible values or keys. |
5 | rdbcompression no |
3、设置dump文件名:
1 | # The filename where to dump the DB |
2 | dbfilename dump.rdb |
4、设置文件存放路径:
貌似AOF的保存路径也是这个
1 | # Note that you must specify a directory here, not a file name. |
2 | dir /var/lib/redis/ |
5、可以进到存放目录查看文件是否生成:
1 | [root@biao ~] # ll /var/lib/redis/ |
2 | total 0 |
6、启动redis:
1 | [root@biao ~] # redis-server /etc/redis.conf |
2 | [root@biao ~] # ps aux|grep redis-server |
3 | root 5187 0.2 0.0 31068 1116 ? Ssl 02:50 0:00 redis-server /etc/redis.conf |
4 | root 5191 0.0 0.0 4032 684 pts/1 R+ 02:50 0:00 grep redis-server |
7、测试:
连接redis并执行操作,记得执行save命令保存快照:
1 | [root@biao ~] # redis-cli |
2 | redis 127.0.0.1:6379> set a 1 |
3 | OK |
4 | redis 127.0.0.1:6379> set b 2 |
5 | OK |
6 | redis 127.0.0.1:6379> set c 3 |
7 | OK |
8 | redis 127.0.0.1:6379> get a |
9 | "1" |
10 | redis 127.0.0.1:6379> get b |
11 | "2" |
12 | redis 127.0.0.1:6379> get c |
13 | "3" |
14 | redis 127.0.0.1:6379> save |
15 | OK |
停止redis服务:
1 | redis 127.0.0.1:6379> shutdown |
2 | redis 127.0.0.1:6379> quit |
3 | [root@biao ~] # ps aux|grep redis |
4 | root 5504 0.0 0.0 4036 684 pts/1 R+ 02:56 0:00 grep redis |
重启redis并查看上次保存的数据:
1 | [root@biao ~] # redis-server /etc/redis.conf |
2 | [root@biao ~] # ps aux|grep redis |
3 | root 5539 0.0 0.0 31068 1124 ? Rsl 02:57 0:00 redis-server /etc/redis.conf |
4 | root 5559 0.0 0.0 4032 680 pts/1 R+ 02:58 0:00 grep redis |
5 | [root@biao ~] # redis-cli |
6 | redis 127.0.0.1:6379> get a |
7 | "1" |
8 | redis 127.0.0.1:6379> get b |
9 | "2" |
10 | redis 127.0.0.1:6379> get c |
11 | "3" |
12 | redis 127.0.0.1:6379> |
如上所示,执行save命令后,之前操作的数据被持久化到dump文件中,而后重启redis将会自动加载到内存。
AOF(Append Only File)
启用redis的AOF,只需将参数appendonly设置为yes,其他参数默认即可。
1 | ############################## APPEND ONLY MODE ############################### |
2 |
3 | # By default Redis asynchronously dumps the dataset on disk. If you can live |
4 | # with the idea that the latest records will be lost if something like a crash |
5 | # happens this is the preferred way to run Redis. If instead you care a lot |
6 | # about your data and don't want to that a single record can get lost you should |
7 | # enable the append only mode: when this mode is enabled Redis will append |
8 | # every write operation received in the file appendonly.aof. This file will |
9 | # be read on startup in order to rebuild the full dataset in memory. |
10 | # |
11 | # Note that you can have both the async dumps and the append only file if you |
12 | # like (you have to comment the "save" statements above to disable the dumps). |
13 | # Still if append only mode is enabled Redis will load the data from the |
14 | # log file at startup ignoring the dump.rdb file. |
15 | # |
16 | # IMPORTANT: Check the BGREWRITEAOF to check how to rewrite the append |
17 | # log file in background when it gets too big. |
18 |
19 | appendonly yes |
启动redis成功后客户端连接并执行操作:
1 | [root@biao ~] # redis-server /etc/redis.conf |
2 | [root@biao ~] # ps aux|grep redis |
3 | root 5986 0.0 0.0 31068 1100 ? Ssl 03:06 0:00 redis-server /etc/redis.conf |
4 | root 6007 0.0 0.0 4032 688 pts/1 R+ 03:07 0:00 grep redis |
5 | [root@biao ~] # ll /var/lib/redis/ |
6 | total 4 |
7 | -rw-r--r-- 1 root root 0 Mar 19 03:06 appendonly.aof |
8 | -rw-r--r-- 1 root root 10 Mar 19 03:06 dump.rdb |
如上所以,客户端执行的操作全部被记录到了appendonly.aof文件中。