在2017年,项目上的redis集群需要从2.8版本更换为3.2版本,此时涉及到redis数据key的迁移,经百度、×××,发现有三种方法可以实现
1、第三方工具redis-dump、redis-load实现
2、通过aof机制导入导出,需要开启aof功能
3、通过rdb存储机制迁移数据,此方法最为简单,直接dump $key、restore $key即可
此篇主要介绍第一种方式,redis-dump、redis-load实现redis的数据导入导出,直接进入主题
redis-dump,要求ruby版本>=2.2.2
第一步:安装依赖的服务包(gcc服务包可用CentOS自带源即可)
yum install ruby rubygems ruby-devel gcc -y
第二步:配置ruby官网源
gem sources -a http://gems.ruby-china.org/ ##将国内的源添加至本地
gem source -l ##查看ruby源
gem source --remove http://rubygems.org/ ##去掉对外源,如果使用rubygems源需×××
gem source -l ##核实ruby源
第三步:gem安装redis-dump
gem install redis-dump -V ##redis-dump,执行此步很慢,以下操作基本均略慢,镜像都为copy国外源
ERROR: Error installing redis-dump:redis requires Ruby version >= 2.2.2.
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3 ##安装rvm命令 略慢
gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <[email protected]>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1) ##出现类似信息,获取key成功
curl -L get.rvm.io | bash -s stable ##执行此命令时,需要对配置google DNS /etc/resolv.conf nameserver 8.8.8.8(我也不知道为什么要配置8.8.8.8,但是他就是成功了)
rvm list known ##查看服务ruby所有版本,如果执行失败,执行"source /etc/profile.d/rvm.sh"
echo "export rvm_max_time_flag=20" >> ~/.rvmrc(此步根据提示操作)
rvm install 2.3.3 ##安装ruby2.3.3版本,此过程下载时间极慢
rvm use 2.3.3 --default ##设置ruby最新版本为
rvm remove 2.0.0 ##卸载旧版本,随后再此安装redis-dump
第四步:确认redis-dump安装成功
find / -iname redis-dump ##确认redis-dump安装成功
/usr/local/rvm/gems/ruby-2.3.3/wrappers/redis-dump
/usr/local/rvm/gems/ruby-2.3.3/gems/redis-dump-0.4.0/bin/redis-dump
/usr/local/rvm/gems/ruby-2.3.3/bin/redis-dump
##相关参数可用help查看,这里不做详解
第五步:测试(https://github.com/delano/redis-dump)
数据备份:##redis-dump备份出来的数据格式都是json格式
redis-dump -a 123456 -u 10.111.132.168:9736 > /tmp/wtc.json
more wtc.json
{"db":0,"key":"LMS:0930115529001000512619-18800074594","ttl":-1,"type":"string","value":"AQA=","size":4}
{"db":0,"key":"LMS:0607225238001000808201-18800487249","ttl":-1,"type":"string","value":"AQA=","size":4}
{"db":0,"key":"LMS:0930103825001000502800-18800031090","ttl":-1,"type":"string","value":"AQA=","size":4}
{"db":0,"key":"LMS:0606193159001000503202-18800444032","ttl":-1,"type":"string","value":"AQABAA==","size":8}
{"db":0,"key":"LMS:0930122140001000517332-18800198151","ttl":-1,"type":"string","value":"AQA=","size":4}
{"db":0,"key":"LMS:0606151834001000801439-18800188730","ttl":-1,"type":"string","value":"AQEBAA==","size":8}
{"db":0,"key":"LMS:0930103607001000502525-18800457188","ttl":-1,"type":"string","value":"AQA=","size":4}
{"db":0,"key":"LMS:0606190129001000501706-18800345537","ttl":-1,"type":"string","value":"AQEBAA==","size":8}
{"db":0,"key":"LMS:0930113721001000509873-18800167485","ttl":-1,"type":"string","value":"AQA=","size":4}
{"db":0,"key":"LMS:0930125722001000523748-18800320602","ttl":-1,"type":"string","value":"AQA=","size":4}
{"db":0,"key":"LMS:0606192506001000502796-18800478597","ttl":-1,"type":"string","value":"AQEBAA==","size":8}
数据恢复:
< 160.json redis-load -u 10.111.132.176:9736 -a wtc.com
< /root/161.json redis-load -u 10.111.132.173:19000 -a wtc.com
redis-load
< db_full.json redis-load
$ < db_db15.json redis-load -d 15
# OR
$ < cat db_full | redis-load
$ < cat db_db15.json | redis-load -d 15
以上就是借助第三方工具来做redis数据迁移,但是需要注意一下几点
1、每个key都在相同或者不同的solt区间中,而每个solt都是基于proxy计算方式的来,迁移时需要确认proxy计算方式有咩有改变(我是不知道怎么计算的,都是通过主节点导出,在通过proxy导入)
2、暂时没发现
原文地址:http://blog.51cto.com/wangtianci/2093525
时间: 2024-10-16 11:15:28