redis部署:(单机版)
tar zxvf redis-4.0.14.tar.gz
make
make install
mkdir -p /usr/local/redis/bin
mkdir -p /usr/local/redis/etc
cp /data/yinkp/redis-4.0.14/redis.conf /usr/local/redis/etc
cd /data/yinkp/redis-4.0.14/src
cp mkreleasdhdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server /usr/local/redis/bin
vi redis.conf
将daemonize属性改为yes
redis启动:
redis-server /usr/local/redis/etc/redis.conf
redis集群部署
配置: vi redis.conf
port 6370 #6370~6374 一个端口相当于一个redis
logfile /data/redis/6370/redis.log
dbfilename dump.6370.rdb
appendfilename "appendonly.6370.aof"
dir /data/redis/6370
#集群配置
cluster-enabled yes
cluster-config-file /data/redis/etc/6370/nodes.conf
cluster-node-timeout 5000
其它节点执行命令:端口需要改动
sed -i "s/6370/6371/g" redis1.conf
mkdir /data/redis/6371 -p
mkdir -p /data/redis/etc/6371
mkdir -p /data/redis/logs/6371
启动:
redis-server redis0.conf
redis-server redis1.conf
redis-server redis2.conf
redis-server redis3.conf
redis-server redis4.conf
创建集群:
./redis-trib.rb create --replicas 0 10.1.236.82:6370 10.1.236.82:6371 10.1.236.82:6372 10.1.236.82:6373 10.1.236.82:6374
redis常用命令:
1. 进入shell:
/usr/local/codis/src/github.com/CodisLabs/codis/bin/redis-cli
2.查看所有key:
keys *
3.查看一个key的值
get key
4. 查看数据库中:
dbsize
5.查看服务器信息:
info
6.查看redis服务器正在执行的操作:
monitor
7.清空redis
flushdb
8.匹配查詢:
keys “name*”
9. 插入操作:
set 1 "test"
10. 删除操作:
del key
redis-dump安装:
cd /data/yinkp/
安装ruby:
wget https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.gz
tar -zxvf ruby-2.4.1.tar.gz
cd ruby-2.6.3/
./configure --prefix=/usr/local/ruby
make && make install
添加用户环境变量:/usr/local/ruby/bin
ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
cd /usr/bin
ln -s /usr/local/ruby/bin/ruby ruby
# 安装redis-dump
gem install redis-dump -V
/usr/local/ruby/lib/ruby/gems/2.6.0/gems/redis-dump-0.4.0/bin
# 导出命令
redis-dump -u 127.0.0.1:6379 > test.json
/usr/local/ruby/lib/ruby/gems/2.6.0/gems/redis-dump-0.4.0/bin/redis-dump -u 127.0.0.1:6379 > test.json
# 导出指定数据库数据
redis-dump -u 127.0.0.1:6379 -d 15 > test.json
# 如果redis设有密码
redis-dump –u :password@127.0.0.1:6379 > test.json
#导入命令
< test.json redis-load
# 如果redis设有密码
< test.json redis-load -u :password@127.0.0.1:6379
< test.json /usr/local/ruby/lib/ruby/gems/2.6.0/gems/redis-dump-0.4.0/bin/redis-load
注:通过命令 dbsize 查看数据是否有丢失。容器pod中的redis集群模式下,通过redis-dump工具导出数据失败。
手动迁移脚本:(1000条/s)
#!/bin/bash
redis-cli -c -h 202.102.221.11 -p 6379 -a password -n 0 keys "*" | while read key
do
redis-cli -c -h 202.102.221.11 -p 6379 -a password -n 0 --raw dump $key | perl -pe 'chomp if eof' | redis-cli -c -h 202.102.221.12 -p 6379 -a password -n 1 -x restore $key 0
echo "migrate key $key"
done
注:如果是单机环境去掉参数:-c