可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot)。
优点:速度快,适合于用做备份,主从复制也是基于RDB持久化功能实现的。
缺点:会有数据丢失
RDB持久化核心参数vim /opt/redis_cluster/redis_6379/conf/redis_6379.conf dir /opt/redis_cluster/redis_6379/data save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error no rdbcompression yes rdbchecksum yes repl-diskless-sync noRBD持久化操作
RBD持久化是将数据存储在内存之中,重启redis试自动写入到redis
root@localhost redis_6379]# vim /opt/redis_cluster/redis_6379/conf/redis_6379.conf ### 以守护进程模式启动 daemonize yes ### 绑定的主机地址 bind 127.0.0.1 192.168.40.1 ### 监听的端口 port 6379 ### pid文件和log文件的保存地址 pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log ### 设置数据库的数量,默认数据库为0 databases 16 ### 指定本地持久化文件的文件名,默认是dump.rdb dbfilename redis_6379.rdb ### 本地数据库的目录 dir /opt/redis_cluster/redis_6379 dbfilename redis_6379.rdb save 900 1 #900秒(15分钟)内有1一个更改 save 300 10 #300秒(5分钟)内有10个更改 save 60 10000 #60秒内有10000个更改 stop-writes-on-bgsave-error no rdbcompression yes rdbchecksum yes repl-diskless-sync no
配置完成参数后关闭在打开redis使得配置生效,
但是关闭redis试会自动清楚所有数据。
[root@localhost ~]#redis-cli shutdown #关闭redis
[root@localhost ~]#redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf #启动redis
#使用for循环向redis中写入数据
[root@localhost ~]# for i in {1..2000}; do redis-cli set k_${i} v_${i}; echo "k_${i} is ok"; done
#查看写入的数据
[root@localhost ~]# redis-cli
127.0.0.1:6379> DBSIZE
(integer) 2000
#如果使用参数redis的RDB持久化没有生效也可以手动配置
127.0.0.1:6379> BGSAVE
#配置完成之后再关闭redis
[root@localhost ~]#redis-cli shutdown #关闭redis
#打开查询会发现数据还是在的
[root@localhost ~]# redis-cli
127.0.0.1:6379> DBSIZE
(integer) 2000
AOF持久化优缺点
AOF持久化(append-only log file)优缺点
记录切执行的所有操作命令,并在服务器启动时,通过重新执行这些命令来还原数据。
AOF文件中的命令全部以redis协议的格式来保存,新的命令会被追加到文件的末尾。
优点:可以最大程度保证数据不丢
缺点:日志记录量比较大
AOF持久化配置参数appendonly yes #是否打开aof日志功能 appendfilename "appendonly.aof" appendfsync always #每1个命令,都立即同步到aof appendfsync everysec #每秒写1次 appendfync no #写入工作交给系统操作,由操作系统判断缓冲区大小,统一写入到aofRDB和AOF缺点和有点
RDB 优点:恢复速度快,空间小 缺点:可能会丢失 AOF 优点:数据安全,不容易丢失 缺点:恢复速度慢,空间大AOF操作举例
redis配置文档详细
[root@localhost redis_6379]# cat conf/redis_6379.conf ### 以守护进程模式启动 daemonize yes ### 绑定的主机地址 bind 127.0.0.1 192.168.40.1 ### 监听的端口 port 6379 ### pid文件和log文件的保存地址 pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log ### 设置数据库的数量,默认数据库为0 databases 16 ### 指定本地持久化文件的文件名,默认是dump.rdb dbfilename redis_6379.rdb ### 本地数据库的目录 dir /opt/redis_cluster/redis_6379 #dbfilename redis_6379.rdb #save 900 1 #900秒(15分钟)内有1一个更改 #save 300 10 #300秒(5分钟)内有10个更改 #save 60 10000 #60秒内有10000个更改 appendonly yes appendfilename "appendonly.aof" appendfsync always appendfsync everysec appendfsync no
配置完成后就可以向redis库中插入数据
#先关闭redis然后在打开 redis使得配置生效
[root@localhost ~]#redis-cli shutdown #关闭redis
[root@localhost ~]#redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf #启动redis
#向redis中插入两千条数据
[root@localhost redis_6379]#for i in {1..2000}; do redis-cli set k_${i} v_${i}; echo "k_${i} is ok"; done
#指定的目录下会生成一个aof的文件,里面记录着所有set添加的内容启动后会自动还原到reidis库中
[root@localhost redis_6379]# ll
总用量 72
-rw-r--r--. 1 root root 71809 5月 10 22:53 appendonly.aof
drwxr-xr-x. 2 root root 57 5月 10 22:55 conf
drwxr-xr-x. 2 root root 28 3月 25 15:38 logs
drwxr-xr-x. 2 root root 28 5月 10 22:53 pid
#我们来查看一下文件内容
[root@localhost redis_6379]# tail -10 appendonly.aof
k_1999
$6
v_1999
*3
$3
set
$6
k_2000
$6
v_2000
#连接到redis中查看一下插入的数据
[root@localhost redis_6379]# redis-cli
127.0.0.1:6379> DBSIZE
(integer) 2000
#重启redis查看aof是否生效
[root@localhost ~]#redis-cli shutdown #关闭redis
[root@localhost ~]#redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf #启动redis
#连接到redis中查看一下插入的数据
[root@localhost redis_6379]# redis-cli
127.0.0.1:6379> DBSIZE
(integer) 2000
这边是可以看到数据还是保存在redis中没有丢失
redis热更新机制
redis是支持配置文件热更新的
使用CONFIG GET *命令可以在不重启的情况下加载配置文件 127.0.0.1:6379> CONFIG GET * 1) "dbfilename" 2) "redis_6379.rdb" 3) "requirepass" 4) "" 5) "masterauth" 6) "" 7) "unixsocket" 8) "" 9) "logfile" 10) "/opt/redis_cluster/redis_6379/logs/redis_6379.log" 11) "pidfile" 12) "/opt/redis_cluster/redis_6379/pid/redis_6379.pid" 13) "slave-announce-ip" 14) "" 15) "maxmemory" 16) "0" 17) "maxmemory-samples" 18) "5" 19) "timeout" 20) "0" 21) "auto-aof-rewrite-percentage" 22) "100" 23) "auto-aof-rewrite-min-size" 24) "67108864" 25) "hash-max-ziplist-entries" 26) "512" 27) "hash-max-ziplist-value" 28) "64" 29) "list-max-ziplist-size" 30) "-2" 31) "list-compress-depth" 32) "0" 33) "set-max-intset-entries" 34) "512" 35) "zset-max-ziplist-entries" 36) "128" 37) "zset-max-ziplist-value" 38) "64" 39) "hll-sparse-max-bytes" 40) "3000" 41) "lua-time-limit" 42) "5000" 43) "slowlog-log-slower-than" 44) "10000" 45) "latency-monitor-threshold" 46) "0" 47) "slowlog-max-len" 48) "128" 49) "port" 50) "6379" 51) "tcp-backlog" 52) "511" 53) "databases" 54) "16" 55) "repl-ping-slave-period" 56) "10" 57) "repl-timeout" 58) "60" 59) "repl-backlog-size" 60) "1048576" 61) "repl-backlog-ttl" 62) "3600" 63) "maxclients" 64) "10000" 65) "watchdog-period" 66) "0" 67) "slave-priority" 68) "100" 69) "slave-announce-port" 70) "0" 71) "min-slaves-to-write" 72) "0" 73) "min-slaves-max-lag" 74) "10" 75) "hz" 76) "10" 77) "cluster-node-timeout" 78) "15000" 79) "cluster-migration-barrier" 80) "1" 81) "cluster-slave-validity-factor" 82) "10" 83) "repl-diskless-sync-delay" 84) "5" 85) "tcp-keepalive" 86) "300" 87) "cluster-require-full-coverage" 88) "yes" 89) "no-appendfsync-on-rewrite" 90) "no" 91) "slave-serve-stale-data" 92) "yes" 93) "slave-read-only" 94) "yes" 95) "stop-writes-on-bgsave-error" 96) "no" 97) "daemonize" 98) "yes" 99) "rdbcompression" 100) "yes" 101) "rdbchecksum" 102) "yes" 103) "activerehashing" 104) "yes" 105) "protected-mode" 106) "yes" 107) "repl-disable-tcp-nodelay" 108) "no" 109) "repl-diskless-sync" 110) "no" 111) "aof-rewrite-incremental-fsync" 112) "yes" 113) "aof-load-truncated" 114) "yes" 115) "maxmemory-policy" 116) "noeviction" 117) "loglevel" 118) "notice" 119) "supervised" 120) "no" 121) "appendfsync" 122) "everysec" 123) "syslog-facility" 124) "local0" 125) "appendonly" 126) "no" 127) "dir" 128) "/opt/redis_cluster/redis_6379" 129) "save" 130) "900 1 300 10 60 10000" 131) "client-output-buffer-limit" 132) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60" 133) "unixsocketperm" 134) "0" 135) "slaveof" 136) "" 137) "notify-keyspace-events" 138) "" 139) "bind" 140) "127.0.0.1 192.168.40.1"redis知识点
如果同时配置的AOF和RDB启动的时候会默认读取AOF的文件 如果配置了RDB或者AOF不小心吧RDB或者AOF的文件删除的情况下重启了redis他也会自动把库中的信息载入到内存里面,不会丢失数据。 redis执行shutdown命令会自动先执行bgsave然后在执行shutdown



