十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
需要将一个redis实例中的部分keys,转移到另一个redis实例
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于做网站、成都网站设计、岳塘网络推广、微信平台小程序开发、岳塘网络营销、岳塘企业策划、岳塘品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供岳塘建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
#!/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
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
如果源实例与目标实例版本不相同,使用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
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
开启源实例aof持久化功能
config set appendonly yes
手动进行aof持久化
bgrewriteaof
将源实例的redis的aof文件导入新建实例
redis-cli -h 127.0.0.1 -p 6395 -a password --pipe < appendonly.aof