redis 复制

Posted omsql

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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部分复制

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

  

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

psync 92127d4880d2c7a4c5c6da3dcb73d584fc002817  -1

  

以上是关于redis 复制的主要内容,如果未能解决你的问题,请参考以下文章

图解Redis,Redis主从复制与Redis哨兵机制

第五章Redis主从复制介绍

详解Redis 主从复制及主从复制原理

Redis源码阅读高可用设计——复制

Redis的复制(基础篇系列四)

Redis 主从复制