前言
Redis是一个开源的高性能键值对数据库。它通过提供多种键值数据类型来适应不同场景下的存储需求,并借助许多高层级的接口使其可以胜任如缓存、队列系统等不同的角色。 Redis持久化了解
为了让性能更加优异,Redis默认是把所有的数据都存在内存中的。但是当服务器重启或程序异常崩溃时,Redis的数据就会全部丢失。因此出现了持久化的概念。持久化就是将存在内存中的数据同步到磁盘来保证持久化。
1、Redis持久化的方式
两种: RDB 和 AOF
RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot)。
AOF 持久化记录服务器执行的所有写操作命令,并在服务器启动时,通过重新执行这些命令来还原数据集。 AOF 文件中的命令全部以 Redis 协议的格式来保存,新命令会被追加到文件的末尾。 Redis 还可以在后台对 AOF 文件进行重写(rewrite),使得 AOF 文件的体积不会超出保存数据集状态所需的实际大小。
2、持久化的数据有什么用?
用于重启后的数据恢复。Redis是一个内存数据库,无论是RDB还是AOF,都只是其保证数据恢复的措施;所以Redis在利用RDB和AOF进行恢复的时候,都会读取RDB或AOF文件,重新加载到内存中。 默认持久化了解
其中RDB就是point-in-time snapshot快照存储,也是默认的持久化方式。对于RDB可理解为半持久化模式,即按照一定的策略周期性的将数据保存到磁盘。对应产生的数据文件为dump.rdb,通过配置文件中的save参数来定义快照的周期。Redis的RDB文件不会坏掉,因为其写操作是在一个新进程中进行的。
默认的持久化设置:
save 900 1 #当有一条Keys数据被改变时,900秒刷新到Disk一次
save 300 10 #当有10条Keys数据被改变时,300秒刷新到Disk一次
save 60 10000 #当有10000条Keys数据被改变时,60秒刷新到Disk一次 利用持久化迁移数据
##########查看配置信息及当前存储的key值###########[root@web-yv2 ~]# redis-cli -h 10.160.35.86 -p 6379redis 10.160.35.86:6379> inforedis_version:2.4.10redis_git_sha1:00000000redis_git_dirty:0arch_bits:64multiplexing_api:epollgcc_version:4.4.6process_id:11911uptime_in_seconds:2154256uptime_in_days:24lru_clock:1527581used_cpu_sys:1145.31used_cpu_user:1430.18used_cpu_sys_children:56.20used_cpu_user_children:207.71connected_clients:147connected_slaves:0client_longest_output_list:0client_biggest_input_buf:0blocked_clients:12used_memory:6762928used_memory_human:6.45Mused_memory_rss:19816448used_memory_peak:10441776used_memory_peak_human:9.96Mmem_fragmentation_ratio:2.93mem_allocator:jemalloc-2.2.5loading:0aof_enabled:0changes_since_last_save:166bgsave_in_progress:0last_save_time:1420367541bgrewriteaof_in_progress:0total_connections_received:1387982total_commands_processed:25568539expired_keys:1838499evicted_keys:0keyspace_hits:529489keyspace_misses:1838207pubsub_channels:0pubsub_patterns:0latest_fork_usec:947vm_enabled:0role:masterdb0:keys=18255,expires=17326#########保存最新的key值################redis 10.160.35.86:6379> BGSAVEBackground saving started##########查看是否保存成功##############redis 10.160.35.86:6379> LASTSAVE(integer) 1420367903##########关闭redis服务器##############redis-cli -h 10.160.35.86 -p 6379 SHUTDOWN#########查看Redis的RDB文件###########[root@web-yv2 ~]# grep "dir" /etc/redis.conf #查看RDB文件的存放位置# The working directory.# The DB will be written inside this directory, with the filename specified# above using the 'dbfilename' configuration directive.# Also the Append Only File will be created inside this directory.# Note that you must specify a directory here, not a file name.dir /var/lib/redis/ #RDB文件存放于此# directive below) it is possible to tell the slave to authenticate before# using the following configuration directive.# the swap file under /tmp is not secure. Create a dir with access granted# configuration directives.[root@web-yv2 ~]# find / -name dump.rdb #查找dump文件/var/lib/redis/dump.rdb##########压缩redis文件并拷入另一台机器#########[root@web-yv2 ~]# tar zcvf redis.tar.gz /var/lib/redis[root@web-yv2 ~]# scp redis.tar.gz root@10.160.35.67:/var/lib/#########登陆10.160.35.67机器并做相应配置#######[root@web-yv1 ~]# vi /etc/redis.confdir /var/lib/redis/ #指定RDB文件路径#########解压缩RDB文件##########################[root@web-yv1 ~]# cd /var/lib/[root@web-yv1 ~]# tar xf redis.tar.gz#########重启Redis服务器########################[root@web-yv1 ~]# service redis restart附加信息 Redis–BGSAVE
在后台异步(Asynchronously)保存当前数据库的数据到磁盘。BGSAVE 命令执行之后立即返回 OK ,然后 Redis fork 出一个新子进程,原来的 Redis 进程(父进程)继续处理客户端请求,而子进程则负责将数据保存到磁盘,然后退出。客户端可以通过 LASTSAVE 命令查看相关信息,判断 BGSAVE 命令是否执行成功。请移步 持久化文档 查看更多相关细节。可用版本:>= 1.0.0时间复杂度:O(N), N 为要保存到数据库中的 key 的数量。返回值:反馈信息。redis> BGSAVEBackground saving startedRedis–LASTSAVE
LASTSAVE返回最近一次 Redis 成功将数据保存到磁盘上的时间,以 UNIX 时间戳格式表示。可用版本:>= 1.0.0时间复杂度:O(1)返回值:一个 UNIX 时间戳。redis> LASTSAVE(integer) 1324043588转换Unix时间戳的网址
http://tool.chinaz.com/Tools/unixtime.aspx