数据迁移的作用

数据迁移的作用,第1张

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

Redis是一个开源的高性能键值对数据库。它可以通过提供多种关键数据类型来满足不同场景下的存储需求,借助众多高层接口可以胜任缓存、队列系统等不同角色。

Redis持久化了解

为了获得更好的性能,Redis默认将所有数据存储在内存中。但是当服务器重启或者程序异常崩溃时,Redis的所有数据都会丢失。于是,持久性这个概念就出现了。持久化就是将内存中的数据同步到磁盘上,保证持久化。

1.坚持Redis有两种方式
:RDB和AOF。

RDB持久性可以在指定的时间间隔内生成数据集的时间点快照。

AOF保存服务器执行的所有写命令,并在服务器启动时通过重新执行这些命令来恢复数据集。AOF文件中的所有命令都以Redis协议的格式保存,新命令将被附加到文件的末尾。Redis还可以在后台重写AOF文件,这样AOF文件的容量就不会超过保存数据集状态所需的实际大小。

2.持久数据有什么用?
用于重新启动后的数据恢复。Redis是内存数据库,无论是RDB还是AOF,都只是保证数据恢复的措施;因此,当使用RDB和AOF进行恢复时,Redis将读取RDB或AOF文件,并将它们重新加载到内存中。

默认持久化了解

RDB是时间点快照存储,也是默认的持久模式。RDB可以理解为半持久模式,即按照一定的策略定期将数据保存到磁盘。对应的数据文件是dump.rdb,快照周期由配置文件中的save参数定义。Redis的RDB文件不会被破坏,因为它的写 *** 作是在一个新的进程中进行的。

默认的持久化设置:
保存9001#当一条密钥数据发生变化时,每900秒刷新一次磁盘
保存30010#当十条密钥数据发生变化时,每30秒刷新一次磁盘
保存6010000#当一万条密钥数据发生变化时,每60秒刷新一次磁盘。

利用持久化迁移数据 ##########查看配置信息及当前存储的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 转换Unix时间戳的网址 http://tool.chinaz.com/Tools/unixtime.aspx



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/783844.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-04
下一篇 2022-05-04

发表评论

登录后才能评论

评论列表(0条)

保存