1. 程式人生 > >Redis數據導入導出

Redis數據導入導出

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
[root@docker /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'.
[root@docker /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數據導入導出