官网介绍看这里 http://redis.io/topics/replication

主从复制:就是主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主

Redis replication is a very simple to use and configure master-slave replication that allows slave Redis servers to be exact copies of master servers.

  • 用处:读写分离,性能扩展; 容灾快速回复

特点:

  • Redis uses asynchronous replication  异步复制
  • A master can have multiple slaves  一主可以多从 并联关系
  • Slaves are able to accept connections from other slaves   串联关系
  • Redis replication is non-blocking on the master side    复制是非阻塞模式
  • Replication is also non-blocking on the slave side
  • avoid the cost of having the master write the full dataset to disk  防止数据溢出写入硬盘

一、配从(服务器)不配主(服务器)

配置三个服务器6379,6380,6381

需要复制三份配置文件,并分别更改端口号,pid文件名字,dump.rdb名字,

Appendonly关掉;

 info replication查询主从复制的信息,初始时都是master

salveof <ip> <port> 设置主仆关系,

①           并联关系:80和81都是79的slave

②           串联关系: 79是80的master,80是81的master

先来并联关系:

①           并联关系:80和81都是79的slave

1 切入点问题?slave1、slave2是从头开始复制还是从切入点开始复制?比如从k4进来,那之前的123是否也可以复制 .

2 从机是否可以写?set可否?

127.0.0.1:6380> set k8 v8

(error) READONLY You can't write against a read only slave.

Read-only slave

Since Redis 2.6, slaves support a read-only mode that is enabled by default. This behavior is controlled by the slave-read-only option in the redis.conf file, and can be enabled and disabled at runtime using CONFIG SET.

Read-only slaves will reject all write commands, so that it is not possible to write to a slave because of a mistake. This does not mean that the feature is intended to expose a slave instance to the internet or more generally to a network where untrusted clients exist, because administrative commands like DEBUG or CONFIG are still enabled. However, security of read-only instances can be improved by disabling commands in redis.conf using the rename-commanddirective.

You may wonder why it is possible to revert the read-only setting and have slave instances that can be target of write operations. While those writes will be discarded if the slave and the master resynchronize or if the slave is restarted, there are a few legitimate use case for storing ephemeral data in writable slaves. However in the future it is possible that this feature will be dropped.

3 主机shutdown后情况如何?从机是上位还是原地待命

4 主机又回来了后,主机新增记录,从机还能否顺利复制?

5 其中一台从机down后情况如何?依照原有它能跟上大部队吗?

配置文件

串联关系: 79是80的master,80是81的master

  • 薪火相传
  • 上一个slave可以是下一个slave的Master,slave同样可以接收其他slaves的连接和同步请求,那么该slave作为了链条中下一个的master, 可以有效减轻master的写压力,去中心化降低风险。
  • 用 slaveof  <ip>  <port>
  • 中途变更转向:会清除之前的数据,重新建立拷贝最新的
  • 风险是一旦某个slave宕机,后面的slave都没法备份
  • 反客为主 
  • 当一个master宕机后,后面的slave可以立刻升为master,其后面的slave不用做任何修改。
  • 用 slaveof  no one  将从机变为主机。

主从复制的原理:

每次从机联通后,都会给主机发送sync指令,主机立刻进行存盘操作,发送RDB文件给从机 ,

从机收到RDB文件后,进行全盘加载,之后每次主机的写操作,都会立刻发送给从机,从机执行相同的命令

How Redis replication works

If you set up a slave, upon connection it sends a PSYNC command.

If this is a reconnection and the master has enough backlog, only the difference (what the slave missed) is sent. Otherwise what is called a full resynchronization is triggered.触发再同步

When a full resynchronization is triggered, the master starts a background saving process in order to produce an RDB file. At the same time it starts to buffer all new write commands received from the clients. When the background saving is complete, the master transfers the database file to the slave, which saves it on disk, and then loads it into memory. The master will then send all buffered commands to the slave. This is done as a stream of commands and is in the same format of the Redis protocol itself.

You can try it yourself via telnet. Connect to the Redis port while the server is doing some work and issue the SYNCcommand. You'll see a bulk transfer and then every command received by the master will be re-issued in the telnet session.

Slaves are able to automatically reconnect when the master-slave link goes down for some reason. If the master receives multiple concurrent slave synchronization requests, it performs a single background save in order to serve all of them.

二、哨兵模式sentinal

  • 反客为主的自动版,能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库.

(1)、新建哨兵配置文件

  • 自定义的/myredis目录下新建sentinel.conf文件,名字绝不能错
  • 在配置文件中填写内容:

sentinel  monitor  mymaster  127.0.0.1  6379  1

  • 其中mymaster为监控对象起的服务器名称, 1 为 至少有多少个哨兵同意迁移的数量。
  • 执行redis-sentinel  /myredis/sentinel.conf  启动哨兵模式
  • 可以看到Running in sentinel mode ,和显示79master 80,81slave的信息

故障恢复

主机79shutdown

可以看到new epoch-选举leader—elect leader -----switch master 成81,

转载于:https://www.cnblogs.com/qianqianchen/p/6024918.html

Redis主从复制-Replication相关推荐

  1. Redis主从复制(Replication)和哨兵模式(Sentinel)

    1.主从复制 1.什么是主从复制 redis的复制功能是支持多个数据库之间的数据同步.一类是主数据库(master)一类是从数据库(slave),主数据库可以进行读写操作,当发生写操作的时候自动将数据 ...

  2. Redis 主从复制(replication)

    主从复制配置 例如一主多从,203 是主节点,在每个slave 节点的redis.conf 配置文件增加一行 slaveof 192.168.8.203 6379 // 在主从切换的时候,这个配置会被 ...

  3. Redis工作笔记-主从复制Replication

    目录 概述 操作 概述 在Redis中配置Master-Slave模式非常简单,Redis中主从复制的特点和优势: 1. 同一个Master可以同步多个Slaves. 2. Slave同样可以接受其他 ...

  4. cxgrid主从表 点+号展开_深入理解Redis主从复制

    一.背景 前面的文章中,我们介绍过Redis的持久化机制,它可以实现Redis实例数据的crash-safe.但是这里有一个问题,就是Redis其实还存在着单点故障问题,比如说Redis的硬盘坏掉了, ...

  5. Redis主从复制下的工作原理

    Redis主从复制下的工作原理 Redis主从复制的配置十分简单,它可以使从服务器是主服务器的完全拷贝.需要清楚Redis主从复制的几点重要内容: 1)Redis使用异步复制.但从Redis 2.8开 ...

  6. php连接redis 主从复制,redis怎么进行主从复制

    redis主从复制同步实现的过程 1.从服务发送一个sync同步命令给主服务要求全量同步 (推荐学习:Redis视频教程) 2.主服务接收到从服务的sync同步命令时,会fork一个子进程后台执行bg ...

  7. Redis主从复制的搭建与.哨兵.数据持久

    目录 一.redis主从复制 1.主从复制的概述 2.一主一从结构的配置 二.哨兵服务 1. 哨兵服务的简单介绍 2.配置哨兵服务 三.数据持久化 1.RedisDataBase(RDB) 2.App ...

  8. 深入Redis 主从复制原理

    复制原理 1.复制过程 复制的过程步骤如下: 1.从节点执行 slaveof 命令        2.从节点只是保存了 slaveof 命令中主节点的信息,并没有立即发起复制        3.从节点 ...

  9. [转载] 深入剖析 redis 主从复制

    转载自http://www.cnblogs.com/daoluanxiaozi/p/3724299.html 主从概述 redis 支持 master-slave(主从)模式,redis server ...

  10. Redis 主从复制的原理及演进

    本文作者:百度基础架构部工程师,王钰 Redis 的主从复制经历了多次演进,本文将从最基本的原理和实现讲起,并层层递进,逐步呈现 Redis 主从复制的演进历史.大家将了解到 Redis 主从复制的原 ...

最新文章

  1. 【青少年编程竞赛交流】11月份微信图文索引
  2. 公基考计算机知识吗,2021河北唐山事业单位公基备考知识:计算机常识
  3. 与Brian Goetz聊Java的数据类
  4. Git 常用命令记录
  5. stm32 DMA使用详解
  6. (转)SQL 优化原则
  7. java agent_如何脚踏实地构建Java Agent
  8. vue 组件间传值、兄弟组件 、bus方式 ( 1 分钟看懂 )
  9. 单例模式(Singleton )的几种用法以及使用条件
  10. Oracle P6培训系列:09定义计划编制视图
  11. arduino并口屏_Arduino教程 12864绘图功能库的使用(并口通信,仅适用ST7920)
  12. 希腊字母发音表及所对应的Markdown KaTex代码
  13. pyinstaller打包有pandas和numpy库过程中遇到的bug及处理
  14. 密码:大写字母 小写字母 数字 特殊字符(四种里至少三种)
  15. 快速排序随机选取主元的重要性
  16. 阿里云物联网平台搭建
  17. 【机器学习】数值分析02——任意方程求根
  18. php 无限极分销,PHP实现无限极分类的两种方式
  19. 英语-新视野大学英语四课后翻译(全)
  20. WS2812原理及实现

热门文章

  1. 两个域名指向同一服务器的非80端口
  2. Ubuntu使用sudo cp命令复制文件夹时出现“cp: omitting directory”问题
  3. (转)一个初学者RHCE学习考试之路
  4. [计算机网络-03] 数据链路层
  5. 显卡的优化以提高计算机性能作用,显卡优化,教您如何设置NVIDIA(英伟达)显卡玩游戏性能更高...
  6. linux2.6内核驱动程序注册函数,于PCI9656设备驱动程序的Linux2.6内核研究
  7. 自动化运维工具——Ansible
  8. 如何修改计算机mac,苹果电脑MAC地址修改的方法
  9. 从狂热的苹果粉丝们来看中国式“杯具”
  10. 动词ing形式的5种用法_动词 ing 形式用法归纳