Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of Redis data import and export and data migration

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the Redis data import and export and data migration methods, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

1. Aof import method.

Because this method is relatively simple, so I will introduce it first.

It is implemented in two steps. The first step is to let the source Redis generate AOF data files.

# Clean all data of the above target instance redis-cli-h destination RedisIP-a password flushall# source instance enable the aof function, and generate the appendonly.aof file redis-cli-h source RedisIP-a password config set appendonly yes in the dir directory

The dir directory, which is available through the config get dir directory.

Config get dir#, for example, after executing the above command on my Mac, it returns the following content: 1) "dir" 2) "/ usr/local/var/db/redis"

From the above command, we can see that my local dir directory is: / usr/local/var/db/redis.

Now let's do the second step to get the target Redis instance to import aof data.

# put the appendonly.aof file under the current path, redis-cli-h target RedisIp-a password-- pipe

< appendonly.aof# 源实例关闭 aof 功能redis-cli -h 源RedisIp -a password config set appendonly no 上面的第一个命令,执行后,如果出现以下内容,则表示导入 aof 数据成功。 All data transferred. Waiting for the last reply...Last reply received from server.errors: 0, replies: 5 我这里是测试,数据比较少,所以提示有 5 个导入成功了。 AOF 的缺点也很明显,就是速度慢,并且如果内容多的话,文件也比较大。而且开启 AOF 后,QPS 会比 RDB 模式写的 QPS 低。还有就是 AOF 是一个定时任务,可能会出现数据丢失的情况。 2、通过我的 xttblog_redis_mv.sh 脚本来实现。 我的脚本内容如下: #!/bin/bash#redis 源ipsrc_ip=192.168.1.4#redis 源portsrc_port=6379#redis 目的ipdest_ip=127.0.0.1#redis 目的portdest_port=6389#要迁移的key前缀key_prefix=i=1redis-cli -h $src_ip -p $src_port -a password keys "${key_prefix}*" | while read keydo redis-cli -h $dest_ip -p $dest_port -a password del $key redis-cli -h $src_ip -p $src_port -a password --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h $dest_ip -p $dest_port -a password -x restore $key 0 echo "$i migrate key $key" ((i++))done 大家在使用的时候,只需要替换 IP 即可。 这个脚本同样有一个问题就是使用了 keys *,然后一个一个遍历,如果是生产环境,不建议这样使用!当然我的脚本也是可以再进行优化的! 3、使用 redis-dump 工具。 Redis-Dump 是一个用于 Redis 数据导入 / 导出的工具,是基于 Ruby 实现的,可以方便的进行 redis 的数据备份。这个工具需要先安装,以我的 Mac 为例,安装教程如下: # 没安装 ruby 的话,先安装 rubybrew install ruby# 移除 gem 自带源gem sources --remove https://rubygems.org/ # 添加淘宝源gem sources -a https://ruby.taobao.org/ # 安装 redis-dumpgem install redis-dump -V 目前我发现,淘宝的镜像已经出现 bad response Not Found 404 了,被告知镜像维护站点已迁往 Ruby China 镜像。 # 替换镜像地址gem sources --add http://gems.ruby-china.org/ --remove http://rubygems.org/# 确认镜像地址是否替换成功gem sources -l# 替换成功后再安装 redis-dumpgem install redis-dump -V 安装完成后,就可以使用 redis-dump 工具进行数据的导入导出了! # redis-dump 导出redis-dump -u :password@源RedisIp:6379 >

Source Redis data file .json # redis-load imports cat source Redis data file .json | redis-load-u: password@ destination RedisIp:6379

Cat source Redis data file. Json | redis-load-u: password@ destination RedisIp:6379

Linux systems or Window systems are similar. After installing redis-dump tools, you can directly use redis-dump export and redis-load import to complete data backup and migration.

The redis-dump tool is very powerful, so I suggest you go to the official website and take a look at its official documents.

4. Rdb file migration

The trouble with redis-dump is that it needs to be installed. If my Redis already has a backup mechanism, such as rdb files, then we can directly migrate rdb files to achieve the same goal.

First, we can turn off the aof feature of the source Redis instance. If you do not close aof,Redis, the aof file is used by default to recover the data.

# disable aof feature for source instance redis-cli-h source RedisIp-a password config set appendonly no

Then use the save command to solidify the data into the rdb file.

# solidify data to RDB file save

After the save is completed, the location of the saved RDB data file is still obtained through the config get dir command.

Next, we need to kill the redis process. Kill the current redis process, otherwise the next step to copy the rdb file, rdb is open, the copied file will occupy the same handle.

Kill-9 redis# or pkill-9 redis# or means to shut down Redis service

Then copy the rdb file of the source redis to the dir data directory of the target Redis, named the rdb file name of the redis you want to migrate.

After the replication is complete, restart the target Redis instance, and the data migration is complete. After the restart is complete, you can verify that the data has been successfully copied.

Thank you for reading this article carefully. I hope the article "what are the import and export of Redis data and the methods of data migration" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report