天天看点

Redis数据迁移方案

场景

Redis实例A ---> Redis实例B,整库全量迁移

方案一:

mac环境

brew install npm

npm install redis-dump -g

针对RedisA: redis-dump -h host1 -p 6379 -d 1 --json > mydb.json

针对RedisB: cat mydb.json | redis-dump --convert | redis-cli 

方案二:

参考: http://www.zlovezl.cn/articles/migrate-redis-server-seamlessly/, 利用redis的主从复制,从RedisA同步到RedisB,完成后,恢复RedisB的master身份。

方案三:(针对阿里云Redis数据迁移)

RedisA实例的dump.rdb文件拷贝到一台阿里云ecs服务器上,该服务器应该能够连接到RedisB实例,在该服务器上自建一个redis实例RedisC, 并RedisC的redis.conf配置文件中的下列配置作出修改:

# The filename where to dump the DB

dbfilename dump.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 /data/mydirectory/  #此处路径为你拷贝过来的dump.rdb文件所在路径

启动RedisC实例,即可导入RedisA实例的所有内容

方案四:

开启用户现有 Redis 实例的 AOF 功能(如果实例已经启用 AOF 功能则忽略此步骤)。

# redis-cli -h old_instance_ip -p old_instance_port config set appendonly yes

通过 AOF 文件将数据导入到新的 ApsaraDB for Redis 实例 (假定生成的 AOF 文件名为 append.aof)。

# redis-cli -h aliyun_redis_instance_ip -p 6379 -a password --pipe < appendonly.aof

注意:如果原有旧的 Redis 实例不需要一直开启 AOF,可在导入完成后通过以下命令关闭。

# redis-cli -h old_instance_ip -p old_instance_port config set appendonly no

-------------------------------------- 两个阿里云redis实例间的数据迁移,综合方案三,四 --------------------------------------------------------------

1)旧redis实例的*.rdb文件拷贝到一台阿里云ecs服务器(ServerA)上,执行方案三

保存为dump.rdb到目录 /var/lib/redis/dump.rdb

重启redis: service redis restart

2)执行方案四(在ServerA上运行如下命令)

redis-cli config set appendonly yes(针对本地自建redis)

redis-cli -h 新redis实例host  -a 密码 --pipe < /var/lib/appendonly.aof

/var/lib/appendonly.aof 表示 appendonly.aof文件在ServerA上的绝对路径

redis-cli config set appendonly no (针对本地自建redis)

      本文转自zsdnr  51CTO博客,原文链接:http://blog.51cto.com/12942149/1949810,如需转载请自行联系原作者