redis数据迁移
admin
2023-02-07 10:20:08
0

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迁移到目标实例

相关内容

热门资讯

今日重大发现“微乐陕西挖坑到底... 家人们!今天小编来为大家解答微乐陕西挖坑透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里...
我来教教您“麻友圈2挪来挪去到... 家人们!今天小编来为大家解答麻友圈2挪来挪去透视挂怎么安装这个问题咨询软件客服徽4282891的挂在...
男子点燃商贩大串气球被刑拘,目... 极目新闻记者 刘孝斌一名商贩将三轮车停在路边,车上绑着一大串等待售卖的气球,一过路男子突然用打火机将...
【第一消息】“同乐吧真的有挂吗... 网上科普关于“同乐吧有没有挂”话题很是火热,小编也是针对同乐吧作*弊开挂的方法以及开挂对应的知识点,...
【第一财经】“熊猫来了怎么装挂... 有 亲,根据资深记者爆料熊猫来了是可以开挂的,确实有挂(咨询软件无需打开...
【第一资讯】“红心自由麻将怎么... 家人们!今天小编来为大家解答红心自由麻将透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里...
迷诺科技取得路侧停泊巡检无人机... 国家知识产权局信息显示,迷诺科技(重庆)有限公司取得一项名为“一种路侧停泊巡检无人机”的专利,授权公...
重磅消息“锦绣山河可以开挂吗?... 重磅消息“锦绣山河可以开挂吗?”(确实真的有挂)您好,锦绣山河这个游戏其实有挂的,确实是有挂的,需要...
终于了解“皇豪互娱到底有挂吗?... 网上科普关于“皇豪互娱有没有挂”话题很是火热,小编也是针对皇豪互娱作*弊开挂的方法以及开挂对应的知识...
今日重大发现“西南九九开挂器?... 网上科普关于“西南九九有没有挂”话题很是火热,小编也是针对西南九九作*弊开挂的方法以及开挂对应的知识...