Redis
是一种内存型数据库,一旦服务器进程退出,数据库的数据就会丢失,为了解决这个问题,Redis
提供了两种持久化的方案,将内存中的数据保存到磁盘中,避免数据的丢失。
RDB
持久化redis
提供了RDB持久化
的功能,这个功能可以将redis
在内存中的的状态保存到硬盘中,它可以手动执行。
也可以再redis.conf
中配置,定期执行 。
RDB
持久化产生的RDB
文件是一个经过压缩 的二进制文件 ,这个文件被保存在硬盘中,redis
可以通过这个文件还原数据库当时的状态。
内存数据保存到磁盘,在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot
) 优点:速度快,适合做备份,主从复制就是基于RDB
持久化功能实现rdb
通过再redis
中使用save
命令触发 rdb
rdb
持久化示例1. 创建redis
服务配置文件 1 2 3 touch /tmp/redis-config /redis-rdb .conf cd /tmp/redis-config /vim redis-rdb .conf
2. 配置redis
服务项并保存 1 2 3 4 5 6 7 8 9 daemonize yes port 6379 logfile /tmp/redis-config /6379 -redis .log dir /tmp/redis-config dbfilename dbmp.rdb bind 0.0 .0.0 save 900 1 save 300 10 save 60 10000
3. 停止redis
服务 1 2 3 4 5 6 7 8 9 10 11 root@Tony-PC :/tmp/redis-config root 5570 5546 0 10 :54 pts/3 00 :00 :00 vim /etc/redis/redis.conf tony 5782 1 0 10 :55 ? 00 :00 :00 /bin/bash /usr/bin/dde-file -manager-pkexec file:///var/log/redis root 5784 5782 0 10 :55 ? 00 :00 :25 /usr/bin/dde-file -manager file:///var/log/redis -w /home/tony root 5846 1 0 10 :55 ? 00 :00 :27 /opt/sublime_text_3/sublime_text --class =sublime-text-dev /var/log/redis/redis-s erver.logroot 11820 5000 0 13 :41 pts/1 00 :00 :00 redis-cli root 11821 7206 0 13 :41 pts/4 00 :00 :00 redis-cli root 11983 1 0 13 :46 ? 00 :00 :00 redis-server 0.0 .0.0 :6379 root 11989 7362 0 13 :46 pts/5 00 :00 :00 grep redis root@Tony-PC :/tmp/redis-config
4. 使用指定配置文件启动redis
1 root@Tony-PC :/tmp/redis-config
5. 启动redis-cli
1 root@Tony-PC :/tmp/redis-config
6. 设置键值并保存 1 2 3 4 5 6 127.0 .0.1 :6379 > set name tonyOK 127.0 .0.1 :6379 > set age 80 OK 127.0 .0.1 :6379 > saveOK
7. 停止redis
服务 1 2 3 4 5 6 7 8 9 10 11 root@Tony-PC :/tmp/redis-config root 5570 5546 0 10 :54 pts/3 00 :00 :00 vim /etc/redis/redis.conf tony 5782 1 0 10 :55 ? 00 :00 :00 /bin/bash /usr/bin/dde-file -manager-pkexec file:///var/log/redis root 5784 5782 0 10 :55 ? 00 :00 :25 /usr/bin/dde-file -manager file:///var/log/redis -w /home/tony root 5846 1 0 10 :55 ? 00 :00 :27 /opt/sublime_text_3/sublime_text --class =sublime-text-dev /var/log/redis/redis-s erver.logroot 11820 5000 0 13 :41 pts/1 00 :00 :00 redis-cli root 11821 7206 0 13 :41 pts/4 00 :00 :00 redis-cli root 11983 1 0 13 :46 ? 00 :00 :00 redis-server 0.0 .0.0 :6379 root 11989 7362 0 13 :46 pts/5 00 :00 :00 grep redis root@Tony-PC :/tmp/redis-config
8. 启动redis
服务 1 root@Tony-PC :/tmp/redis-config
9. 启动redis-cli
1 root@Tony-PC :/tmp/redis-config
10. 测试键值是否存在 1 2 3 4 5 6 7 127.0 .0.1 :6379 > keys *1 ) "age" 2 ) "name" 127.0 .0.1 :6379 > get age"80" 127.0 .0.1 :6379 > get name"tony"
生成的文件列表:
1 2 3 4 5 root@Tony-PC :/tmp/redis-config 总用量 16 -rw-r--r-- 1 root root 4188 8 月 14 13 :46 6379 -redis .log-rw-r--r-- 1 root root 115 8 月 14 13 :45 dbmp.rdb-rw-r--r-- 1 root root 157 8 月 14 13 :34 redis-rdb .conf
AOF
持久化AOF
(append-only log file) 记录服务器执行的所有变更操作命令(例如set del
等),并在服务器启动时,通过重新执行这些命令来还原数据集AOF
文件中的命令全部以redis
协议的格式保存,新命令追加到文件末尾。 优点:最大程序保证数据不丢 缺点:日志记录非常大
配置参数
1 2 3 4 5 6 AOF持久化配置,两条参数 appendonly yes appendfsync always 总是修改类的操作 everysec 每秒做一次持久化 no 依赖于系统自带的缓存大小机制
aof
持久化示例1. 创建redis
服务配置文件 1 2 root@Tony-PC :/tmp/redis-config root@Tony-PC :/tmp/redis-config
2. 配置redis
服务项并保存 1 2 3 4 5 6 7 8 9 10 daemonize yes post 6380 logfile /tmp/redis-config /6380 -redis .log dir /tmp/redis-config dbfilename 6380 .rdb save 900 1 save 300 10 save 60 10000 appendonly yes appendfsync everysec
3. 使用配置文件启动redis
服务 1 root@Tony-PC :/tmp/redis-config
4. 启动redis-cli
5. 设置键并实时检测aof
文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 127.0 .0.1 :6380 > set name googleOK 127.0 .0.1 :6380 > set age 100 OK root@Tony-PC :/tmp/redis-config *2 $6 SELECT $1 0 *3 $3 set $4 name $6 google *3 $3 set $3 age $3 100
6. 关闭redis
服务 1 2 3 4 5 6 7 8 9 10 root@Tony-PC :/tmp/redis-config root 5570 5546 0 10 :54 pts/3 00 :00 :00 vim /etc/redis/redis.conf tony 5782 1 0 10 :55 ? 00 :00 :00 /bin/bash /usr/bin/dde-file -manager-pkexec file:///var/log/redis root 5784 5782 0 10 :55 ? 00 :00 :30 /usr/bin/dde-file -manager file:///var/log/redis -w /home/tony root 5846 1 0 10 :55 ? 00 :00 :28 /opt/sublime_text_3/sublime_text --class =sublime-text-dev /var/log/redis/redis-s erver.logroot 11983 1 0 13 :46 ? 00 :00 :00 redis-server 0.0 .0.0 :6379 root 12932 1 0 14 :13 ? 00 :00 :00 redis-server *:6380 root 13018 5000 0 14 :17 pts/1 00 :00 :00 redis-cli -p 6380 root 13094 7362 0 14 :17 pts/5 00 :00 :00 grep redis root@Tony-PC :/tmp/redis-config
7. 使用配置文件启动redis
服务 1 root@Tony-PC :/tmp/redis-config
8. 启动redis-cli
并测试键值是否存在 1 2 3 4 5 6 7 root@Tony-PC :~ 127.0 .0.1 :6380 > keys *1 ) "age" 2 ) "name" 127.0 .0.1 :6380 > get age"100"
生成的文件列表:
1 2 3 4 5 6 7 8 root@Tony-PC :/tmp/redis-config 总用量 28 -rw-r--r-- 1 root root 4188 8 月 14 13 :46 6379 -redis .log-rw-r--r-- 1 root root 2487 8 月 14 14 :18 6380 -redis .log-rw-r--r-- 1 root root 147 8 月 14 14 :19 appendonly.aof-rw-r--r-- 1 root root 115 8 月 14 13 :45 dbmp.rdb-rw-r--r-- 1 root root 180 8 月 14 14 :13 redis-aof .conf-rw-r--r-- 1 root root 157 8 月 14 13 :34 redis-rdb .conf
redis
持久化方式的区别rdb
:基于快照的持久化,速度更快,一般用作备份,主从复制也是依赖于rdb
持久化功能
aof
:以追加的方式记录redis
操作日志的文件。可以最大程度的保证redis
数据安全,类似于mysql
的binlog
RDB
备份平滑切换到AOF
备份确保redis
版本在2.2以上 1 2 root@Tony-PC :/tmp/redis-config Redis server v=4.0 .9 sha=00000000 :0 malloc=jemalloc-3 .6.0 bits=64 build=41979 e41b911ab84
前提:我们配置好了使用rdb
配置redis
服务
启动redis
服务 1 root@Tony-PC :/tmp/redis-config /rdb
启动redis-cli
并写入数据 1 2 3 4 5 6 7 8 9 10 oot@Tony-PC :/tmp/redis-config 127.0 .0.1 :6379 > keys *(empty list or set ) 127.0 .0.1 :6379 > set country chinaOK 127.0 .0.1 :6379 > set age 5000 OK 127.0 .0.1 :6379 > keys *1 ) "country" 2 ) "age"
查看目录文件 1 2 3 4 5 root@Tony-PC :/tmp/redis-config /rdb 总用量 20 -rw-r--r-- 1 root root 5178 8 月 14 14 :45 6379 -redis .log-rw-r--r-- 1 root root 120 8 月 14 14 :41 rdb.rdb-rw-r--r-- 1 root root 164 8 月 14 14 :37 redis-rdb .conf
临时开启aof
持久化 1 2 3 4 127.0 .0.1 :6379 > CONFIG set appendonly yesOK 127.0 .0.1 :6379 > CONFIG set save "" OK
查看数据并写入新数据 1 2 3 4 5 6 7 127.0 .0.1 :6379 > keys *1 ) "age" 2 ) "country" 127.0 .0.1 :6379 > get country"china" 127.0 .0.1 :6379 > set city shanghaiOK
查看目录文件 1 2 3 4 5 6 root@Tony-PC :/tmp/redis-config /rdb 总用量 20 -rw-r--r-- 1 root root 5178 8 月 14 14 :45 6379 -redis .log-rw-r--r-- 1 root root 152 8 月 14 14 :44 appendonly.aof-rw-r--r-- 1 root root 120 8 月 14 14 :41 rdb.rdb-rw-r--r-- 1 root root 164 8 月 14 14 :37 redis-rdb .conf
写入配置文件永久生效 将aof
操作,写入到配置文件,永久生效,下次重启后生效
1 2 3 4 5 6 7 8 9 10 daemonize yes port 6379 logfile /tmp/redis-config /rdb/6379 -redis .log dir /tmp/redis-config /rdbappendonly yes appendfsync everysec