IX redis(3)
加载rdb文件(或加载aof文件);
redis replication;
redis-migrate-tool,此工具适用任何场景,是重点:
https://github.com/vipshop/redis-migrate-tool,Features特点:
Fast.快速;
Multi-Threads.多线程;
Based on redis replication.基于redis复制;
Live migration.实时迁移;
In the process of migrating data, the source redis can also provide services for users.迁移过程中,源集群不影响对外提供服务;
Heterogeneous migration.异构迁移;
Twemproxy and redis cluster support.支持twemproxy集群,redis集群,rdb文件和aof文件;
When the target is twemproxy, keys are direct imported into redis behind the twemproxy.过滤功能;
Migration Status view.迁移状态显示;
Data Verification Mechanism.完善的数据抽样校验;
source来源可以是:单redis实例、redis cluster、rdb文件、aof文件、twemproxy集群;
target目标可以是:单redis实例、redis cluster、rdb文件、twemproxy集群;
配置文件rmt.conf常用配置项如下:
type: The group redis type. Possible values are:
single
twemproxy
redis cluster
rdb file
aof file
servers: The list of redis address in the group. If type is twemproxy, this is same as the twemproxy config file. If type is rdb file, this is the filename.
listen: The listening address and port (name:port or ip:port). Defaults to 127.0.0.1:8888.
以下操作有:单节点迁到单节点、redis集群迁到redis集群,各用两种方法,其中:
单节点迁到单节点(方一,直接复制并加载rdb或aof文件;方二,利用redis复制机制);
redis集群迁到redis集群(两种方法都使用redis-migrate-tool工具;方一,网络隔离情况时,在被迁移端导出rdb文件,再到迁移端加载;方二,相同网段时,在线迁移);
1、redis单节点数据迁到另一单节点上:
方一:
利用redis启动时加载rdb二进制文件方式(或加载aof文本文件方式),直接将被迁移端的dump.rdb文件(即snapshotting快照文件)上传至迁移端主机;
被迁移端注意:
停连接redis的应用,即禁止往redis里写数据;
在redis交互模式下执行>bgsave #(即重新fork一个线程,用于将当前内存中的数据持久化到磁盘上;若使用加载aof文件,此步用>bgrewriteaof命令);
迁移端注意:
将被迁移端的dump.rdb上传至迁移端指定路径下,即要与配置文件中dir和dbfilename配置项一致;
启动前将配置文件中的rdb持久化配置项注释掉,此步重要,即注释掉配置文件中默认的如下三行:
#save 900 1
#save 300 10
#save 60 10000
操作:
此例是在一台主机上启动两个redis实例(6379和6380),将6379上的数据迁到6380上;
在被迁移端(6379)如下操作:
[root@test2 ~]# cd /ane/redis
[root@test2 redis]# redis-cli -p 6379 keys \*
(empty list or set)
[root@test2 redis]# for i in `seq 1 100` ; do redis-cli -p 6379 set key$i value$i ; done #(模拟产生数据)
……
[root@test2 redis]# redis-cli -p 6379
127.0.0.1:6379> keys *
……
127.0.0.1:6379> bgsave
Background saving started
127.0.0.1:6379> exit
在迁移端(6380)上操作:
[root@test2 redis]# redis-cli -p 6380
127.0.0.1:6380> keys *
(empty list or set)
127.0.0.1:6380> exit
[root@test2 redis]# vim redis_6380.conf
#save 900 1
#save 300 10
#save 60 10000
dbfilename dump_6380.rdb
dir /ane/redis/6380/
[root@test2 redis]# redis-cli -p 6380 shutdown
[root@test2 redis]# rm -f /ane/redis/6380/dump_6380.rdb
[root@test2 redis]# cp dump.rdb /ane/redis/6380/dump_6380.rdb #(dump.rdb为6379上的持久化数据)
[root@test2 redis]# redis-server /ane/redis/6380/redis_6380.conf
[root@test2 redis]# redis-cli -p 6380
127.0.0.1:6380> keys *
1)"key64"
2)"key29"
3)"key55"
4)"key67"
5)"key12"
6)"key39"
7)"key26"
8)"key82"
9)"key57"
10)"key48"
……
[root@test2 redis]# vim /ane/redis/6380/redis_6380.conf
save 900 1
save 300 10
save 60 10000
[root@test2 redis]# redis-cli -p 6380 shutdown
[root@test2 redis]# redis-server /ane/redis/6380/redis_6380.conf
[root@test2 redis]# redis-cli -p 6380
127.0.0.1:6380> keys *
1)"key85"
2)"key23"
3)"key94"
4)"key41"
5)"key76"
6)"key24"
7)"key55"
8)"key67"
9)"key43"
10)"key90"
11)"key84"
12)"key86"
……
127.0.0.1:6380> exit
方二:
用redis replication复制机制,用于两主机在相同网段或两主机可互通的情况下;
注意主从次序,主端数据被清空,若从仍开启slaveof项时,从上数据也会被清空,两端数据是实时同步的;
在迁移端(6380)操作:
[root@test2 redis]# pwd
/ane/redis
[root@test2 redis]# redis-cli -p 6380 shutdown #(关闭从redis后改配置)
[root@test2 redis]#vim 6380/redis.conf #(在迁移端即主从模式的从服务器设置主的ip和port,也可在交互模式下配置>slaveof 192.168.23.130 6379)
# slaveof <masterip><masterport>
slaveof 192.168.23.130 6379
[root@test2 redis]# redis-server 6380/redis.conf #(启动从redis)
在被迁移端(6379)操作:
[root@test2 redis]# redis-cli -p 6379 flushall
OK
[root@test2 redis]# for i in `seq 100 1000`; do redis-cli -p 6379 set key$i value$i &> /dev/null ; done #(模拟产生数据,到迁移端查看是否有)
在迁移端(6380)上查看是否有数据:
[root@test2 redis]# redis-cli -p 6380
127.0.0.1:6380> exists key1000
(integer) 1
127.0.0.1:6380> exists key100
(integer) 1
127.0.0.1:6380> exit
[root@test2 redis]# vim 6380/redis.conf #(待数据传完后,改配置,重启;或在命令行交互模式下执行>slave NO ONE,并改配置文件)
#slaveof 192.168.23.1306379
[root@test2 redis]# redis-cli -p 6380 shutdown
[root@test2 redis]# redis-server 6380/redis.conf
[root@test2 redis]# redis-cli -p 6380
127.0.0.1:6380> exists key100
(integer) 1
127.0.0.1:6380> exists key1000
(integer) 1
127.0.0.1:6380> exit
2、redis 集群数据迁到另一redis集群:
使用redis-migrate-tool工具;
操作:
将test1主机redis集群中的数据迁到test2主机;
方一:
适合两台主机不在一个网段,网络隔离情况;
test1和test2均需安装redis-migrate-tool,利用此工具先将test1主机上的redis集群中的数据导出为rdb文件,再将rdb文件上传至test2并恢复到redis集群中;
test1(被迁移端)上操作如下:
[root@test1 ~]# cd /ane
[root@test1 ane]# yum -y install automake libtool autoconf bzip2
[root@test1 ane]# unzip /usr/local/src/redis-migrate-tool-master.zip -d /ane
[root@test1 ane]# cd redis-migrate-tool-master
[root@test1 redis-migrate-tool-master]# autoreconf -fvi
[root@test1 redis-migrate-tool-master]# ./configure
[root@test1 redis-migrate-tool-master]# make
[root@test1 redis-migrate-tool-master]# vim /etc/profile.d/redis.sh
export PATH=$PATH:/ane/redis/bin:/ane/redis-migrate-tool-master/src
[root@test1 redis-migrate-tool-master]# ./etc/profile.d/redis.sh
[root@test1 redis-migrate-tool-master]# which redis-migrate-tool
/ane/redis-migrate-tool-master/src/redis-migrate-tool
[root@test1 redis-migrate-tool-master]# redis-migrate-tool -h
……
-d,--daemonize : run as a daemon
-o,--output=S : set logging file(default: stderr)
-c,--conf-file=S : set configurationfile (default: rmt.conf)
-C,--command=S : set command toexecute (default: redis_migrate)
[root@test1 redis-migrate-tool-master]# cd..
[root@test1 ane]# redis-cli -c -h 192.168.23.129 -p 6379 keys \*
(empty list or set)
[root@test1 ane]# for i in `seq 101 2000` ; do redis-cli -c -h 192.168.23.129 -p 6379 set key$i value$i &> /dev/null ; done #(模拟往test1主机上的redis集群中插入数据)
[root@test1 ane]# redis-cli -c -h 192.168.23.129 -p 6379
192.168.23.129:6379> exists key101
(integer) 1
192.168.23.129:6379> exists key2000
-> Redirected to slot [9780] located at192.168.23.129:6380
(integer) 1
192.168.23.129:6380> exit
[root@test1 ane]# cp redis-migrate-tool-master/rmt.conf redis-migrate-tool-master/rmt.conf.example
[root@test1 ane]# vim redis-migrate-tool-master/rmt.conf
[source]
type: redis cluster
servers:
- 192.168.23.129:6379
[target]
type: rdb file
[common]
listen: 0.0.0.0:8888
[root@test1 ane]# redis-migrate-tool -c redis-migrate-tool-master/rmt.conf -o log.txt -d
[root@test1 ane]# tail -f log.txt #(查看日志是否报错)
……
[root@test1 ane]# cat log.txt #(日志中有详细操作过程)
……
[root@test1 ane]# ll -h #(在当前目录下生成了三个rdb文件(该集群是6个节点,3主3从),将这三个文件上传到迁移端恢复即可)
total 60K
-rw-r--r--. 1 root root 4.2K Mar 17 01:26log.txt
-rw-r--r--. 1 root root 12K Mar 17 01:26 node192.168.23.129:6379-1489739168652097-11514.rdb
-rw-r--r--. 1 root root 12K Mar 17 01:26 node192.168.23.129:6380-1489739168552203-11514.rdb
-rw-r--r--. 1 root root 12K Mar 17 01:26 node192.168.23.129:6381-1489739168455476-11514.rdb
drwxr-xr-x. 9 root root 4.0K Mar 2 18:52 redis
drwxr-xr-x. 9 root root 4.0K Mar 17 01:23redis-migrate-tool-master
-rwxr-xr-x. 1 root root 163 Mar 3 00:32 shutdown_redis.sh
-rwxr-xr-x. 1 root root 140 Mar 3 00:34 startup_redis.sh
[root@test1 ane]# mv node192.168.23.129\:6379-1489739168652097-11514.rdb node129-6379.rdb #(将生成的这三个文件改名,防止特殊字符(如冒号等)影响操作)
[root@test1 ane]# mv node192.168.23.129\:6380-1489739168552203-11514.rdb node129-6380.rdb
[root@test1 ane]# mv node192.168.23.129\:6381-1489739168455476-11514.rdb node129-6381.rdb
[root@test1 ane]# scp node129-63*.rdbroot@192.168.23.130:/ane #(传至迁移端)
[root@test1 ane]# redis-migrate-tool-h 127.0.0.1 -p 8888 shutdown #(关闭redis-migrate-tool)
test2上操作如下:
安装redis-migrate-tool;
[root@test2 ane]# redis-cli -c -h 192.168.23.130-p 6379
192.168.23.130:6379> keys *
(empty list or set)
192.168.23.130:6379> exit
[root@test2 ane]# vim redis-migrate-tool-master/rmt.conf
[source]
type: rdb file
servers:
- /ane/node129-6379.rdb
- /ane/node129-6380.rdb
- /ane/node129-6381.rdb
[target]
type: redis cluster
servers:
- 192.168.23.130:6379
[common]
listen: 0.0.0.0:8888
[root@test2 ane]# redis-migrate-tool -c redis-migrate-tool-master/rmt.conf -o log.txt -d
[root@test2 ane]# tail -f log.txt
……
[root@test2 ane]# redis-cli -c -h 192.168.23.130 -p 6379
192.168.23.130:6379> exists key101
(integer) 1
192.168.23.130:6379> exists key2000
-> Redirected to slot [9780] located at192.168.23.130:6380
(integer) 1
192.168.23.130:6380> keys *
……
192.168.23.130:6380> exit
[root@test2 ane]# redis-migrate-tool -h 127.0.0.1 -p 8888 shutdown
方二:
适合两台主机在一个网段,在线迁移:
确保两台主机的redis集群都在运行状态;
两个主机(被迁移端和迁移端)任意一个上安装redis-migrate-tool即可,此例是在test2上安装;
在迁移端test2上操作:
[root@test2 ~]# cd /ane
[root@test2 ane]# redis-cli -c -h 192.168.23.130 -p 6379
192.168.23.130:6379> flushall
OK
192.168.23.130:6379> keys *
(empty list or set)
192.168.23.130:6379> exit
[root@test2 ane]# vim redis-migrate-tool-master/rmt.conf
[source]
type: redis cluster
servers:
- 192.168.23.129:6379
[target]
type: redis cluster
servers:
- 192.168.23.130:6379
[common]
listen: 0.0.0.0:8888
[root@test2 ane]# redis-migrate-tool -c redis-migrate-tool-master/rmt.conf -o log.txt -d
[root@test2 ane]# tail -f log.txt #(查看是否有报错)
[root@test2 ane]# redis-cli -c -h 192.168.23.130 -p 6379 #(查看是否有数据)
192.168.23.130:6379> exists key2000
-> Redirected to slot [9780] located at192.168.23.130:6380
(integer) 1
192.168.23.130:6380> exit
[root@test2 ane]# redis-cli-h 127.0.0.1 -p 8888 #(连接redis-migrate-tool监控的端口)
127.0.0.1:8888> info
# Server
version:0.1.0
os:Linux 2.6.32-431.el6.x86_64 x86_64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:25904
tcp_port:8888
uptime_in_seconds:196
uptime_in_days:0
config_file:/ane/redis-migrate-tool-master/rmt.conf
# Clients
connected_clients:1
max_clients_limit:100
total_connections_received:1
# Memory
mem_allocator:jemalloc-4.0.4
# Group
source_nodes_count:3
target_nodes_count:3
# Stats
all_rdb_received:1
all_rdb_parsed:1
all_aof_loaded:0
rdb_received_count:3
rdb_parsed_count:3
aof_loaded_count:0
total_msgs_recv:1901
total_msgs_sent:1901
total_net_input_bytes:35215
total_net_output_bytes:76137
total_net_input_bytes_human:34.39K
total_net_output_bytes_human:74.35K
total_mbufs_inqueue:0
total_msgs_outqueue:0
127.0.0.1:8888> exit
[root@test2 ane]# redis-migrate-tool-c redis-migrate-tool-master/rmt.conf -o log.txt -C redis_check #(数据校验)
Check job is running...
Checked keys: 1000
Inconsistent value keys: 0
Inconsistent expire keys : 0
Other check error keys: 0
Checked OK keys: 1000
All keys checked OK!
Check job finished, used 0.045s
[root@test2 ane]# redis-migrate-tool -h 127.0.0.1 -p 8888 shutdown