默认的持久化设置:
save 900 1 #当有一条Keys数据被改变时,900秒刷新到Disk一次
save 300 10 #当有10条Keys数据被改变时,300秒刷新到Disk一次
save 60 10000 #当有10000条Keys数据被改变时,60秒刷新到Disk一次
利用持久化迁移数据
##########查看配置信息及当前存储的key值###########
[iyunv@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文件###########
[iyunv@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.
[iyunv@web-yv2 ~]# find / -name dump.rdb #查找dump文件
/var/lib/redis/dump.rdb
##########压缩redis文件并拷入另一台机器#########
[iyunv@web-yv2 ~]# tar zcvf redis.tar.gz /var/lib/redis
[iyunv@web-yv2 ~]# scp redis.tar.gz root@10.160.35.67:/var/lib/
#########登陆10.160.35.67机器并做相应配置#######
[iyunv@web-yv1 ~]# vi /etc/redis.conf
dir /var/lib/redis/ #指定RDB文件路径
#########解压缩RDB文件##########################
[iyunv@web-yv1 ~]# cd /var/lib/
[iyunv@web-yv1 ~]# tar xf redis.tar.gz
#########重启Redis服务器########################
[iyunv@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