一、RDB
1、RDB持久化配置
打开redis的配置文件(redis.conf)找到如下位置进行修改进行修改;
save 900 1 #在900秒(15分钟)之后,如果至少有1个key发生变化,则dump内存快照。
save 300 10 #在300秒(5分钟)之后,如果至少有10个key发生变化,则dump内存快照。
save 60 10000 #在60秒(1分钟)之后,如果至少有10000个key发生变化,则dump内存快照。
在redis持久化中默认是打开RDB持久化的;
默认的上面三个配置,有时候是不会达到测试持久化的要求的,故有时候重启redis服务时,数据会全部丢失。此时,我们则可以根据实际需要自定义save来满足数据持久化的目的;
2、演示
(1)使用默认save配置
操作:快速添加三个值,等3s左右马上重启;
[root@master bin]# ./redis-server ./redis.conf
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -a 123456
192.168.xxx.21:6379> DBSIZE
(integer) 0
192.168.xxx.21:6379> set aa AA
OK
192.168.xxx.21:6379> set bb BB
OK
192.168.xxx.21:6379> set cc CC
OK
192.168.xxx.21:6379> keys *
1) "bb"
2) "cc"
3) "aa"
192.168.xxx.21:6379>
[root@master bin]# netstat -lnp | grep 6379
tcp 0 0 192.168.xxx.21:6379 0.0.0.0:* LISTEN 3516/./redis-server
[root@master bin]# kill -9 3516
[root@master bin]# netstat -lnp | grep 6379
[root@master bin]# ./redis-server ./redis.conf
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -a 123456
192.168.xxx.21:6379> DBSIZE
(integer) 0
192.168.xxx.21:6379> keys *
(empty list or set)
192.168.xxx.21:6379>
结果:并没有进行持久化;因为增加记录之后停留时间太短就重启redis服务,不满足持久化条件;
(2)增加满足测试数据持久化的save条件
操作:快速添加三个值,等3s左右马上重启;
[root@master bin]# netstat -lnp | grep 6379
tcp 0 0 192.168.xxx.21:6379 0.0.0.0:* LISTEN 3533/./redis-server
[root@master bin]# kill -9 3533
[root@master bin]# netstat -lnp | grep 6379
[root@master bin]# ./redis-server ./redis.conf
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -a 123456
192.168.xxx.21:6379> keys *
(empty list or set)
192.168.xxx.21:6379> set aa AA
OK
192.168.xxx.21:6379> set bb BB
OK
192.168.xxx.21:6379> set cc CC
OK
192.168.xxx.21:6379> keys *
1) "cc"
2) "bb"
3) "aa"
192.168.xxx.21:6379>
[root@master bin]# netstat -lnp | grep 6379
tcp 0 0 192.168.xxx.21:6379 0.0.0.0:* LISTEN 3570/./redis-server
[root@master bin]# kill -9 3570
[root@master bin]# netstat -lnp | grep 6379
[root@master bin]# ./redis-server ./redis.conf
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -a 123456
192.168.xxx.21:6379> DBSIZE
(integer) 3
192.168.xxx.21:6379> keys *
1) "cc"
2) "bb"
3) "aa"
192.168.xxx.21:6379>
结果:持久化成功
二、AOF
1、AOF的持久化配置
appendonly yes
appendfsync always #每次有数据修改发生时都会写入AOF文件。
appendfsync everysec #每秒钟同步一次,该策略为AOF的缺省策略。
appendfsync no #从不同步。高效但是数据不会被持久化。
AOF持久化,默认是关闭的;appendonly yes,可以打开AOF持久化机制。
2、演示
(1)关闭aof,使用默认save配置
操作:快速添加三个值,等3s左右马上重启;
[root@master bin]# ./redis-server ./redis.conf
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -a 123456
192.168.xxx.21:6379> DBSIZE
(integer) 3
192.168.xxx.21:6379> select 1
OK
192.168.xxx.21:6379[1]> DBSIZE
(integer) 0
192.168.xxx.21:6379[1]> set ff FF
OK
192.168.xxx.21:6379[1]> set tt TT
OK
192.168.xxx.21:6379[1]> set hh HH
OK
192.168.xxx.21:6379[1]> DBSIZE
(integer) 3
192.168.xxx.21:6379[1]>
[root@master bin]# netstat -lnp | grep 6379
tcp 0 0 192.168.xxx.21:6379 0.0.0.0:* LISTEN 3656/./redis-server
[root@master bin]# kill -9 3656
[root@master bin]# netstat -lnp | grep 6379
[root@master bin]# ./redis-server ./redis.conf
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -a 123456
192.168.xxx.21:6379> select 1
OK
192.168.xxx.21:6379[1]> DBSIZE
(integer) 0
192.168.xxx.21:6379[1]>
结果:并没有进行持久化;因为增加记录之后停留时间太短就重启redis服务,不满足持久化条件;
(2)打开aof
操作:快速添加三个值,等3s左右马上重启;
[root@master bin]# netstat -lnp | grep 6379
tcp 0 0 192.168.xxx.21:6379 0.0.0.0:* LISTEN 3675/./redis-server
[root@master bin]# kill -9 3675
[root@master bin]# netstat -lnp | grep 6379
[root@master bin]# ./redis-server ./redis.conf
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -a 123456
192.168.xxx.21:6379> select 1
OK
192.168.xxx.21:6379[1]> DBSIZE
(integer) 0
192.168.xxx.21:6379[1]> set ff FF
OK
192.168.xxx.21:6379[1]> set gg GG
OK
192.168.xxx.21:6379[1]> set tt TT
OK
192.168.xxx.21:6379[1]> keys *
1) "tt"
2) "gg"
3) "ff"
192.168.xxx.21:6379[1]>
[root@master bin]# netstat -lnp | grep 6379
tcp 0 0 192.168.xxx.21:6379 0.0.0.0:* LISTEN 3722/./redis-server
[root@master bin]# kill -9 3722
[root@master bin]# netstat -lnp | grep 6379
[root@master bin]# ./redis-server ./redis.conf
[root@master bin]# ./redis-cli -h 192.168.xxx.21 -a 123456
192.168.xxx.21:6379> select 1
OK
192.168.xxx.21:6379[1]> DBSIZE
(integer) 3
192.168.xxx.21:6379[1]>
结果:持久化成功