redis-dump数据导出以及redis-load还原数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis-dump数据导出以及redis-load还原数据相关的知识,希望对你有一定的参考价值。
一 、安装Ruby依赖
1)安装Ruby环境
yum -y install ruby ruby-devel
yum -y install rubygems
2)添加taobao Ruby镜像
添加淘宝ruby源;
#gem sources --add http://gems.ruby-china.org --removehttps://rubygems.org/
http://gems.ruby-china.org added to sources
source https://rubygems.org/ not present in cache
查看现有ruby源;
#gem sources --l
*** CURRENT SOURCES ***
http://rubygems.org/
http://gems.ruby-china.org
二、安装redis-dump
Centos默认支持ruby到2.0.0,可gem 安装redis需要最低是2.2.2
解决办法是 先安装rvm,再把ruby版本提升至2.3.3
#gem install redis-dump
select id,user,host,db,command,time,state,info from information_schema.processlist where command <>‘sleep‘ order by time;
ERROR: Error installing redis-dump:
redis requires Ruby version >= 2.2.2.
#gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
gpg: directory `/root/.gnupg‘ created
gpg: new configuration file `/root/.gnupg/gpg.conf‘ created
gpg: WARNING: options in `/root/.gnupg/gpg.conf‘ are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg‘ created
gpg: keyring `/root/.gnupg/pubring.gpg‘ created
gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net
gpg: /root/.gnupg/trustdb.gpg: trustdb created
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)
1.安装curl
#sudo yum install curl
-
安装RVM
#curl -L get.rvm.io | bash -s stable -
source/usr/local/rvm/scripts/rvm
-
查看rvm库中已知的ruby版本
#rvm list known #MRI Rubies [ruby-]2.0.0[-p648] [ruby-]2.1[.10] [ruby-]2.2[.7] [ruby-]2.3[.4] [ruby-]2.4[.1]
-
安装一个ruby版本
#rvm install 2.3.4 这里要选择正确 //rvm install 2.3会失败 - 使用一个ruby版本
#rvmuse2.3.4
#rvm --create ruby-2.3.4
Using /usr/local/rvm/gems/ruby-2.3.4
#ruby --version
ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-linux]
- 卸载一个已知版本rvmremove2.0.0
安装redis-dump
#gem install redis-dump
三、redis简单初步优化以及启动
配置文件参数:
[[email protected] ~]# grep -Ev "^#|^$" /usr/local/redis/redis.conf |egrep "bind|port|tcp-backlog|daemonize|pidfile|loglevel|logfile|dir|requirepass|appendonly|appendfsync"
bind 127.0.0.1
port 6379
tcp-backlog 511
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/data/redis/log/redis.log"
dir /data/redis/data/
requirepass zykjdr#558996
appendonly no
appendfilename "appendonly.aof"
启动:redis-server /usr/local/redis/redis.conf
[[email protected] log]# redis-cli -h 127.0.0.1 -p 6379 -a ‘zykjdr#558996‘
127.0.0.1:6379> get key
(nil)
127.0.0.1:6379> set key01 name01
OK
127.0.0.1:6379> set key02 name02
OK
127.0.0.1:6379>
四、导出redis数据
#redis-dump -u 127.0.0.1:6379 -a ‘zykjdr#558996‘ > test.json
[[email protected] ~]# redis-dump -u 127.0.0.1:6379 -a ‘zykjdr#558996‘ > test.json
-bash: redis-dump: command not found
原因是没有指定ruby-2.3.4的环境,导致找不到redis-dump命令
解决办法:
[[email protected] ~]# rvm --create ruby-2.3.4
Warning! PATH is not properly set up, ‘/usr/local/rvm/gems/ruby-2.3.4/bin‘ is not available.
Usually this is caused by shell initialization files. Search for ‘PATH=...‘ entries.
You can also re-add RVM to your profile by running: ‘rvm get stable --auto-dotfiles‘.
To fix it temporarily in this shell session run: ‘rvm use ruby-2.3.4‘.
To ignore this error add rvm_silence_path_mismatch_check_flag=1 to your ~/.rvmrc file.
Using /usr/local/rvm/gems/ruby-2.3.4
[[email protected] ~]# which redis-dump
/usr/local/rvm/gems/ruby-2.3.4/bin/redis-dump
数据导出
[[email protected] ~]# redis-dump -u 127.0.0.1:6379 -a ‘zykjdr#558996‘ > test.json
[[email protected] ~]# cat test.json
{"db":0,"key":"key02","ttl":-1,"type":"string","value":"name02","size":6}
{"db":0,"key":"key01","ttl":-1,"type":"string","value":"name01","size":6}
[[email protected] ~]#
在redis-server 中删除数据
127.0.0.1:6379> del key02
(integer) 1
127.0.0.1:6379> get key02
(nil)
127.0.0.1:6379> del key01
(integer) 1
127.0.0.1:6379> get key01
(nil)
redis-load 还原数据
[[email protected] ~]# < test.json redis-load -u 127.0.0.1:6379 -a ‘zykjdr#558996‘
[[email protected] log]# redis-cli -h 127.0.0.1 -p 6379 -a ‘zykjdr#558996‘
127.0.0.1:6379> get key01
"name01"
127.0.0.1:6379> get key02
"name02"
127.0.0.1:6379>
同时可以设置环境变量来使用redis-dump 和redis-load导出和导入redis数据
在redis.conf配置文件中去掉redis-server的密码,
设定一个环境变量
[[email protected] ~]# export REDIS_URI=127.0.0.1:6379
[[email protected] ~]# redis-dump >test1.json
[[email protected] ~]# cat test1.json
{"db":0,"key":"key01","ttl":-1,"type":"string","value":"name01","size":6}
{"db":0,"key":"key02","ttl":-1,"type":"string","value":"name02","size":6}
登录redis-server删除key值
[[email protected] log]# redis-cli -h 127.0.0.1 -p 6379 -a ‘zykjdr#558996‘
127.0.0.1:6379> del key01
(integer) 1
127.0.0.1:6379> del key02
(integer) 1
导入数据并查看导入的key值``
[[email protected] ~]# <test1.json redis-load
# export REDIS_URI=127.0.0.1:6379
[[email protected] ~]# <test1.json redis-load
查看key值
127.0.0.1:6379> get key02
"name02"
127.0.0.1:6379> get key01
"name01"
127.0.0.1:6379>
[[email protected] ~]# <test1.json redis-load
#export REDIS_URI=127.0.0.1:6379
#< test.json redis-load
ERROR (Yajl::ParseError): lexical error: invalid bytes in UTF8 string.
},"size":274} {"db":0,"key":"0000\u0005t\u0000#100776963
(right here) ------^
[[email protected]_hx_193_16 bohai]# cp test.json test.json-bak
如果报错,请使用-n选项,使用请参考官方,请谨慎使用!
#< test.json redis-load -n //-n (以二进制形式导入)
参考文献:
利用redis-dump导出、还原数据-->http://www.ywnds.com/?p=7115
redis-dump安装使用手册--->https://github.com/delano/redis-dump
redis requires Ruby version >= 2.2.2问题 ---https://www.cnblogs.com/carryping/p/7447823.html
以上是关于redis-dump数据导出以及redis-load还原数据的主要内容,如果未能解决你的问题,请参考以下文章
redis 导入导出redis-load,redis-dump详解