需要將一個redis實例中的部分keys,轉(zhuǎn)移到另一個redis實例
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、成都網(wǎng)站設(shè)計、岳塘網(wǎng)絡(luò)推廣、微信平臺小程序開發(fā)、岳塘網(wǎng)絡(luò)營銷、岳塘企業(yè)策劃、岳塘品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供岳塘建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址: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
時間復(fù)雜度: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
如果源實例與目標(biāo)實例版本不相同,使用migrate進行遷移的時候會有如下錯誤:
1935 migrate key esf_common_auth_code_18587656289
(error) ERR Target instance replied with error: ERR DUMP payload version or checksum are wrong
如果源實例與目標(biāo)實例版本不相同,使用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文件導(dǎo)入新建實例
redis-cli -h 127.0.0.1 -p 6395 -a password --pipe < appendonly.aof