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文件不会坏掉,因为其写操作是在一个新进程中进行的。
默认的持久化设置:
1 2 3 |
save 900 1 #当有一条Keys数据被改变时,900秒刷新到Disk一次 save 300 10 #当有10条Keys数据被改变时,300秒刷新到Disk一次 save 60 10000 #当有10000条Keys数据被改变时,60秒刷新到Disk一次 |
利用持久化迁移数据
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
##########查看配置信息及当前存储的key值########### [root@iZ113oau64sZ ~]# redis-cli [-h 127.0.0.1] [-p 6379] 127.0.0.1:6379> info # Server redis_version:3.2.1 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:534457225b137ffe redis_mode:standalone os:Linux 2.6.32-573.22.1.el6.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll gcc_version:4.4.7 process_id:7772 run_id:9544e674136eae52fe569eac24c7476b13effe8c tcp_port:6379 uptime_in_seconds:15 uptime_in_days:0 hz:10 lru_clock:1145914 executable:/usr/local/redis/redis-server config_file:/usr/local/redis/conf/redis.conf # Clients connected_clients:1 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # Memory used_memory:1182184 used_memory_human:1.13M used_memory_rss:7819264 used_memory_rss_human:7.46M used_memory_peak:1182184 used_memory_peak_human:1.13M total_system_memory:8254705664 total_system_memory_human:7.69G used_memory_lua:37888 used_memory_lua_human:37.00K maxmemory:0 maxmemory_human:0B maxmemory_policy:noeviction mem_fragmentation_ratio:6.61 mem_allocator:jemalloc-4.0.3 # Persistence loading:0 rdb_changes_since_last_save:0 rdb_bgsave_in_progress:0 rdb_last_save_time:1477540907 rdb_last_bgsave_status:ok rdb_last_bgsave_time_sec:-1 rdb_current_bgsave_time_sec:-1 aof_enabled:0 aof_rewrite_in_progress:0 aof_rewrite_scheduled:0 aof_last_rewrite_time_sec:-1 aof_current_rewrite_time_sec:-1 aof_last_bgrewrite_status:ok aof_last_write_status:ok # Stats total_connections_received:1 total_commands_processed:1 instantaneous_ops_per_sec:0 total_net_input_bytes:31 total_net_output_bytes:5888248 instantaneous_input_kbps:0.00 instantaneous_output_kbps:0.00 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:0 migrate_cached_sockets:0 # Replication role:master connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 # CPU used_cpu_sys:0.01 used_cpu_user:0.01 used_cpu_sys_children:0.00 used_cpu_user_children:0.00 # Cluster cluster_enabled:0 # Keyspace db0:keys=21,expires=0,avg_ttl=0 #########保存最新的key值################ 127.0.0.1:6379> bgsave Background saving started ##########查看是否保存成功############## 127.0.0.1:6379> lastsave (integer) 1477541019 ##########关闭redis服务器############## redis-cli shutdown |
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 26 |
#########查看Redis的RDB文件########### [root@iZ113oau64sZ ~]# grep 'dir' /usr/local/redis/conf/redis.conf #查看RDB文件的存放位置 # The working directory. # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # The Append Only File will also be created inside this directory. # Note that you must specify a directory here, not a file name. dir /usr/local/redis/db # directive below) it is possible to tell the slave to authenticate before # 2) Diskless: The Redis master creates a new process that directly writes the # configuration directive. # latency-monitor-threshold configuration directive. When its value is set # threshold. These thresholds can be configured using the following directives. # The syntax of every client-output-buffer-limit directive is the following: [root@iZ113oau64sZ ~]# cd /usr/local/redis/db ##########压缩redis数据dump文件并拷入另一台机器######### [root@iZ113oau64sZ ~]# tar -zcvf redis.tar.gz dump.rdb [root@iZ113oau64sZ ~]# scp redis.tar.gz root@192.168.1.103:/usr/local/redis/db #########登陆192.168.1.103机器并做相应配置####### [root@iZ113oau64sZ ~]# vi /usr/local/redis/conf/redis.conf dir /usr/local/redis/db #指定RDB文件路径 #########解压缩RDB文件########################## [root@iZ113oau64sZ ~]# cd /usr/local/redis/db [root@iZ113oau64sZ ~]# tar -zxvf redis.tar.gz #########重启Redis服务器######################## [root@iZ113oau64sZ ~]# service redis restart |
ps:
Redis–BGSAVE
在后台异步(Asynchronously)保存当前数据库的数据到磁盘。 BGSAVE 命令执行之后立即返回 OK ,然后 Redis fork 出一个新子进程,原来的 Redis 进程(父进程)继续处理客户端请求,而子进程则负责将数据保存到磁盘,然后退出。
客户端可以通过 LASTSAVE 命令查看相关信息,判断 BGSAVE 命令是否执行成功。
可用版本:>= 1.0.0时间复杂度:O(N), N 为要保存到数据库中的 key 的数量。返回值:反馈信息。
1 2 |
redis> BGSAVE Background saving started |
Redis–LASTSAVE
LASTSAVE
返回最近一次 Redis 成功将数据保存到磁盘上的时间,以 UNIX 时间戳格式表示。 可用版本:>= 1.0.0时间复杂度:O(1)返回值:一个 UNIX 时间戳。
1 2 |
redis> LASTSAVE (integer) 1324043588 |