Redis利用持久化进行数据迁移

前言

      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 6379
redis 10.160.35.86:6379> info
redis_version:2.4.10
redis_git_sha1:00000000
redis_git_dirty:0
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.6
process_id:11911
uptime_in_seconds:2154256
uptime_in_days:24
lru_clock:1527581
used_cpu_sys:1145.31
used_cpu_user:1430.18
used_cpu_sys_children:56.20
used_cpu_user_children:207.71
connected_clients:147
connected_slaves:0
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:12
used_memory:6762928
used_memory_human:6.45M
used_memory_rss:19816448
used_memory_peak:10441776
used_memory_peak_human:9.96M
mem_fragmentation_ratio:2.93
mem_allocator:jemalloc-2.2.5
loading:0
aof_enabled:0
changes_since_last_save:166
bgsave_in_progress:0
last_save_time:1420367541
bgrewriteaof_in_progress:0
total_connections_received:1387982
total_commands_processed:25568539
expired_keys:1838499
evicted_keys:0
keyspace_hits:529489
keyspace_misses:1838207
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:947
vm_enabled:0
role:master
db0:keys=18255,expires=17326
#########保存最新的key值################
redis 10.160.35.86:6379> BGSAVE
Background 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 [email protected]:/var/lib/
#########登陆10.160.35.67机器并做相应配置#######
[root@web-yv1 ~]# vi /etc/redis.conf
dir /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> BGSAVE
Background saving started

Redis–LASTSAVE
LASTSAVE
返回最近一次 Redis 成功将数据保存到磁盘上的时间,以 UNIX 时间戳格式表示。
可用版本:>= 1.0.0时间复杂度:O(1)返回值:一个 UNIX 时间戳。
redis> LASTSAVE
(integer) 1324043588

猜你喜欢

转载自horsemen.iteye.com/blog/2242805