Redis数据导入导出
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis数据导入导出相关的知识,希望对你有一定的参考价值。
Redis导入导出数据库的方法
本文摘自:https://www.cnblogs.com/hjfeng1988/p/7146009.html
方法一、Redis数据导出导入-redis-dump and redis-load
一:安装redis-dump
1、yum install ruby rubygems ruby-devel //安装rubygems 以及相关包
# 更改gem源
2、 gem sources -a https://ruby.taobao.org/
[[email protected] ~]# gem sources -a http://ruby.taobao.org Error fetching http://ruby.taobao.org: bad response Not Found 404 (http://ruby.taobao.org/specs.4.8.gz)
访问http://ruby.taobao.org,公告通知镜像维护站点已迁往Ruby China镜像
#gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ [[email protected] ~]# gem sources --add http://gems.ruby-china.org/ --remove http://rubygems.org/ http://gems.ruby-china.org/ added to sources source http://rubygems.org/ not present in cache [[email protected] ~]# gem sources -l *** CURRENT SOURCES *** http://gems.ruby-china.org/ 3、 gem install redis-dump -V
#######报版本错误########
ERROR: Error installing redis-dump: redis requires Ruby version >= 2.2.2.
解决办法:
1.安装RVM: gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3 curl -L get.rvm.io | bash -s stable find / -name rvm -print /usr/local/rvm /usr/local/rvm/src/rvm /usr/local/rvm/src/rvm/bin/rvm /usr/local/rvm/src/rvm/lib/rvm /usr/local/rvm/src/rvm/scripts/rvm /usr/local/rvm/bin/rvm /usr/local/rvm/lib/rvm /usr/local/rvm/scripts/rvm 2, source /usr/local/rvm/scripts/rvm 查看rvm库中已知的ruby版本 rvm list known 3,安装一个版本 rvm install 2.3.3 4.使用一个ruby版本 rvm use 2.3.3 [5].设置默认版本 rvm use 2.3.3 --default [[email protected] /data/redis 22:14:11]# rvm use 2.3.3 --default Gemset '' does not exist, 'rvm ruby-2.3.3 do rvm gemset create ' first, or append '--create'. [[email protected] /data/redis 22:14:22]# rvm use 2.3.3 --default --create ruby-2.3.3 - #gemset created /usr/local/rvm/gems/ruby-2.3.3 ruby-2.3.3 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list ruby-2.3.3 - #generating default wrappers.............. Using /usr/local/rvm/gems/ruby-2.3.3 [6].卸载一个已知版本 rvm remove 2.0.0 查看ruby版本: ruby --version 二、重新安装redis-dump gem install redis-dump -V
2、redis-dump导出
redis-dump -u 127.0.0.1:6379 > test.json redis-dump -u :[email protected]:6379 > 127.0.0.1.json
3、redis-load导入
cat 172.20.0.1.json | redis-load -u :[email protected]:6379 cat test.json |redis-load -u 127.0.0.1:6379
方法二、aof导入方式
1、源实例生成aof数据
# 清空上文目标实例全部数据 [[email protected] ~]# redis-cli -h 172.20.0.2 -a password flushall OK # 源实例开启aof功能,将在dir目录下生成appendonly.aof文件 [[email protected] ~]# redis-cli -h 172.20.0.1 -a password config set appendonly yes OK
2、目标实例导入aof数据
# 假设appendonly.aof就在当前路径下
[[email protected] ~]# redis-cli -h 172.20.0.2 -a password --pipe < appendonly.aof All data transferred. Waiting for the last reply... Last reply received from server. errors: 0, replies: 5 # 源实例关闭aof功能 [[email protected] ~]# redis-cli -h 172.20.0.1 -a password config set appendonly no OK
方法三、rdb文件迁移方式
暂略
方法四、源实例db0迁移至目标实例db1
[[email protected] ~]# cat redis_mv.sh #!/bin/bash redis-cli -h 172.20.0.1 -p 6379 -a password -n 0 keys "*" | while read key do redis-cli -h 172.20.0.1 -p 6379 -a password -n 0 --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h 172.20.0.2 -p 6379 -a password -n 1 -x restore $key 0 echo "migrate key $key" done
以上是关于Redis数据导入导出的主要内容,如果未能解决你的问题,请参考以下文章