温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

redis数据迁移

发布时间:2020-05-03 05:59:45 来源:网络 阅读:2292 作者:苏黎世1995 栏目:关系型数据库

1.需求

需要将一个redis实例中的部分keys,转移到另一个redis实例

2.迁移方案

2.1 源实例与目标实例版本相同
2.1.1 使用dump命令
#!/bin/bash

#redis 源ip
src_ip=127.0.0.1
#redis 源port
src_port=6392

#redis 目的ip
dest_ip=127.0.0.1
#redis 目的port
dest_port=6393

#要迁移的key前缀
key_prefix=test

i=1

redis-cli -h $src_ip -p $src_port keys "${key_prefix}*" | while read key
do
    redis-cli -h $dest_ip -p $dest_port del $key
    redis-cli -h $src_ip -p $src_port --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h $dest_ip -p $dest_port -x restore $key 0
    echo "$i migrate key $key"
    ((i++))
done
2.1.2 使用migrate命令

migrate用法:

MIGRATE host port key destination-db timeout [COPY] [REPLACE] 

起始版本:2.6.0
时间复杂度:This command actually executes a DUMP+DEL in the source instance, and a RESTORE in the target instance. See the pages of these commands for time complexity. Also an O(N) data transfer between the two instances is performed.

迁移脚本

#!/bin/bash

#redis 源ip
src_ip=127.0.0.1
#redis 源port
src_port=6392

#redis 目的ip
dest_ip=127.0.0.1
#redis 目的port
dest_port=6393

#要迁移的key前缀
key_prefix=test

i=1

redis-cli -h $src_ip -p $src_port keys "${key_prefix}*" | while read key
do
    redis-cli -h $src_ip -p $src_port migrate $dest_ip $dest_port $key 0 1000 replace
    echo "$i migrate key $key"
    ((i++))
done
2.2 源实例与目标实例版本不相同
2.2.1 不可行方案
  • 如果源实例与目标实例版本不相同,使用migrate进行迁移的时候会有如下错误:

    1935 migrate key esf_common_auth_code_18587656289
    (error) ERR Target instance replied with error: ERR DUMP payload version or checksum are wrong
  • 如果源实例与目标实例版本不相同,使用dump进行迁移的时候会有如下错误

    (error) ERR DUMP payload version or checksum are wrong
  • 如果源实例与目标实例版本不相同,直接复制rdbw文件搭建从库为有如下报错
    5453:S 23 Nov 18:13:14.153 * MASTER <-> SLAVE sync: Flushing old data
    5453:S 23 Nov 18:13:14.153 * MASTER <-> SLAVE sync: Loading DB in memory
    5453:S 23 Nov 18:13:14.153 # Can't handle RDB format version 8
    5453:S 23 Nov 18:13:14.153 # Failed trying to load the MASTER synchronization DB from disk
2.2.2 可行方案
  1. 开启源实例aof持久化功能

    config set appendonly yes
  2. 手动进行aof持久化

    bgrewriteaof
  3. 新建一个redis实例,与目标实例版本相同并启动
  4. 将源实例的redis的aof文件导入新建实例

    redis-cli -h 127.0.0.1 -p 6395 -a password --pipe < appendonly.aof
  5. 通过dump或者migrate的方式将新实例的key迁移到目标实例
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI