redis 复制

下载redis

wget http://download.redis.io/releases/redis-5.0.5.tar.gz

创建用户及目录

groupadd redis
useradd -g redis redis
mkdir -p /u01/redis

解压及安装

gzip -d redis-5.0.5.tar 

tar -xf redis-5.0.5.tar  -C /u01/redis

cd /u01/redis/redis-5.0.5

mkdir data log run

make

maie  install (root用户执行否则报错)

redis 配置

编辑 .bash_profile

PATH=$PATH:$REDIS_HOME/bin:$HOME/bin:/usr/local/bin

export PATH
alias c='cd /u01/redis/redis-5.0.5'

编辑redis.conf

1        daemonize yes #后台启动
2         port 7001 #修改端口号,从7001到7006
3         maxmemory  100M   #设置最大内存
4         maxmemory-policy  allkeys-lru #内存达到最大值时策略
5         pidfile /u01/redis/redis-5.0.5/run/redis_7001.pid
6         logfile /u01/redis/redis-5.0.5/log/7001.log
7         dir /u01/redis/redis-5.0.5/data/7001
8         appendonly yes

复制redis.conf到对应的目录

/u01/redis/redis-5.0.5/conf
[redis@redis01 conf]$ ls
7001 7002 7003 7004 redis.conf redis-sentinel-26379.conf

[redis@redis01 conf]$ cd 7001
[redis@redis01 7001]$ ls
7001.conf nodes.conf

编辑启动脚本

[redis@redis01 ~]$ cat s.sh
pkill -9 redis
redis-server /u01/redis/redis-5.0.5/conf/7001/7001.conf
redis-server /u01/redis/redis-5.0.5/conf/7002/7002.conf
ps -ef|grep redis-server|grep -v grep

在复制节点启动复制

slaveof 192.168.20.201 7001

启动日志如下

1 9713:S 16 Sep 2019 13:28:34.459 * REPLICAOF 192.168.20.201:7001 enabled (user request from 'id=3 addr=192.168.20.202:33566 fd=9 name= age=107 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=48 qbuf-free=32720 obl=0 oll=0 omem=0 events=r cmd=slaveof') ---建立连接套接字
2 9713:S 16 Sep 2019 13:28:35.309 * Connecting to MASTER 192.168.20.201:7001  --连接master
3 9713:S 16 Sep 2019 13:28:35.310 * MASTER <-> REPLICA sync started
4 9713:S 16 Sep 2019 13:28:35.310 * Non blocking connect for SYNC fired the event.
5 9713:S 16 Sep 2019 13:28:35.310 * Master replied to PING, replication can continue...  --从节点发送ping命令返回
6 9713:S 16 Sep 2019 13:28:35.312 * Trying a partial resynchronization (request ce82246b3aecfacf28105aba18eecbb7407e7679:1).
7 9713:S 16 Sep 2019 13:28:35.314 * Full resync from master: 5ad71d8d15caad30b77a50fd6c22eaf088f936dd:0   --开始全量同步数据

查看复制

[redis@redis01 ~]$  redis-cli -h 192.168.20.201 -p 7001 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.20.202,port=7003,state=online,offset=504,lag=0
slave1:ip=192.168.20.202,port=7004,state=online,offset=504,lag=1
master_replid:15fac3f45a2af5a4421a9906e7e1898abd6713d4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:504
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:504
[redis@redis01 ~]$  redis-cli -h 192.168.20.202 -p 7003 info replication
# Replication
role:slave
master_host:192.168.20.201
master_port:7001
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:560
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:15fac3f45a2af5a4421a9906e7e1898abd6713d4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:560
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:560

  这是正常情况下复制,如果在同步过程当中出现网络中断或者主机重启等可以用psync部分复制

psync{runId}{offset},参数含义如下:
·runId:从节点所复制主节点的运行id。
·offset:当前从节点已复制的数据偏移量。

  

redis@redis01 ~]$ redis-cli -h 192.168.20.201 -p 7001 info server|grep run
run_id:92127d4880d2c7a4c5c6da3dcb73d584fc002817

psync 92127d4880d2c7a4c5c6da3dcb73d584fc002817  -1

 优化

如果RDB文件超过6GB以上时要格外小心。传输文件这一步操作非常耗时,需要加大repl-timeout所配置的值(默认60秒)

停止同步

192.168.20.202:7004> slaveof no one
OK

10701:S 16 Sep 2019 21:36:43.615 # CONFIG REWRITE executed with success.
10701:S 16 Sep 2019 21:36:44.138 * Connecting to MASTER 192.168.20.202:7003
10701:S 16 Sep 2019 21:36:44.138 * MASTER <-> REPLICA sync started
10701:S 16 Sep 2019 21:36:44.138 * Non blocking connect for SYNC fired the event.
10701:S 16 Sep 2019 21:36:44.139 * Master replied to PING, replication can continue...
10701:S 16 Sep 2019 21:36:44.139 * Trying a partial resynchronization (request a32990ebbf807015eb5186ce88e049b1cae7cee9:4129548).
10701:S 16 Sep 2019 21:36:44.141 * Full resync from master: e9e7b0c3af955da1330327c91c1ca8f7705fae6d:4127868
10701:S 16 Sep 2019 21:36:44.142 * Discarding previously cached master state.
10701:S 16 Sep 2019 21:36:44.239 * MASTER <-> REPLICA sync: receiving 256 bytes from master
10701:S 16 Sep 2019 21:36:44.239 * MASTER <-> REPLICA sync: Flushing old data
10701:S 16 Sep 2019 21:36:44.261 * MASTER <-> REPLICA sync: Loading DB in memory
10701:S 16 Sep 2019 21:36:44.262 * MASTER <-> REPLICA sync: Finished with success
10701:S 16 Sep 2019 21:36:44.264 * Background append only file rewriting started by pid 14688
10701:S 16 Sep 2019 21:36:44.355 * AOF rewrite child asks to stop sending diffs.
14688:C 16 Sep 2019 21:36:44.355 * Parent agreed to stop sending diffs. Finalizing AOF...
14688:C 16 Sep 2019 21:36:44.355 * Concatenating 0.00 MB of AOF diff received from parent.
14688:C 16 Sep 2019 21:36:44.403 * SYNC append only file rewrite performed
14688:C 16 Sep 2019 21:36:44.404 * AOF rewrite: 8 MB of memory used by copy-on-write
10701:S 16 Sep 2019 21:36:44.446 * Background AOF rewrite terminated with success
10701:S 16 Sep 2019 21:36:44.446 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
10701:S 16 Sep 2019 21:36:44.446 * Background AOF rewrite finished successfully

  

 

原文地址:https://www.cnblogs.com/omsql/p/11528783.html