3 Redis主动同步设置方法 Redis主从同步

1.Redis主从同步特点

一个master可以拥有多个slave

多个slave可以连接同一个master,还可以连接到其他slave

主从复制不会阻塞master,在同步数据时,master可以继续处理client请求。

提高系统的伸缩性

2.Redis主从同步的过程

配置好slave服务器连接master后,slave会建立和master的连接,然后发送sync命令。

无论是第一次同步建立的连接还是连接断开后的重新连接,master都会启动一个后台进程,将数据库快照保存到磁盘文件中,同时master主进程会开始收集新的写命令并缓存起来。

当后台进程完成写磁盘文件后,master就将快照文件发送给slave,slave将文件保存到磁盘上,然后加载到内存将数据库快照恢复到slave上。

slave完成快照文件的恢复后,master就会把缓存的命令都转发给slave,slave更新内存数据库。

后续master收到的写命令都会通过开始建立的连接发送给slave。从master到slave的同步数据的命令和从client到master发送的命令使用相同的协议格式。当master和slave的连接断开时,slave可以自动重新建立连接。如果master同时收到多个slave发来的同步连接命令,只会使用启动一个进程来写数据库镜像,然后发送给所有slave。

3 Redis主动同步设置方法

1 )在redis.conf配置文件中设置

通过简单的配置slave(master端无需配置),用户就能使用redis的主从复制
我们让端口6379的redis做master;另一台端口6379的redis做slave

我们修改slave主机的redis.conf的配置文件
vim   redis.conf | sed -n '189,215p'
   189  ################################# REPLICATION #################################
   190 
   191  # Master-Slave replication. Use slaveof to make a Redis instance a copy of
   192  # another Redis server. Note that the configuration is local to the slave
   193  # so for example it is possible to configure the slave to save the DB with a
   194  # different interval, or to listen to another port, and so on.
   195  #
   196  # slaveof <masterip> <masterport>
   197  slaveof 172.16.1.2 6379              在此处添加本行内容,指定主master的IP和端口
   198  # If the master is password protected (using the "requirepass" configuration
   199  # directive below) it is possible to tell the slave to authenticate before
   200  # starting the replication synchronization process, otherwise the master will
   201  # refuse the slave request.
   202  #
   203  # masterauth <master-password>
   204  masterauth 123456                    在此处添加本行内容,指定验证的密码
   205  # When a slave loses its connection with the master, or when the replication
   206  # is still in progress, the slave can act in two different ways:
   207  #
   208  # 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
   209  #    still reply to client requests, possibly with out of date data, or the
   210  #    data set may just be empty if this is the first synchronization.
   211  #
   212  # 2) if slave-serve-stale-data is set to 'no' the slave will reply with
   213  #    an error "SYNC with master in progress" to all the kind of commands
   214  #    but to INFO and SLAVEOF.
   215  #

2)进行redis主从同步测试

redis-cli   -a  123456  get name      #获取master    redis6379的键name的值
"benet"
[root@redis-master ~]# redis-cli -a 123456  set name xxxxx    #向redis6379里存一个key=name,value=xxxxx的数据
OK
[root@redis-master ~]# redis-cli -a 123456  get name  #获取redis6379的键name的值
"xxxxx"

3.Append-Only File(追加式的操作日志)

  • 另外由于快照方式是在一定间隔时间做一次的,所以如果redis意外down掉的话,就会丢失最后一次快照后的所有修改。如果应用要求不能丢失任何修改的话,可以采用aof持久化方式。下面介绍Append-only file。
  • aof比快照方式有更好的持久化性,是由于在使用aof持久化方式时,redis会将每一个收到的写命令都通过write函数追加到文件中(默认是appendonly.aof)。当redis重启时会通过重新执行文件中保存的写命令来在内存中重建整个数据库的内容.当然由于os会在内核中缓存write做的修改,所以可能不是立即写到磁盘上。这样aof方式的持久化也还是有可能会丢失部分修改。不过我们可以通过配置文件告诉redis我们想要通过fsync函数强制os写入到磁盘的时机。有三种方式如下(默认是:每秒fsync一次)
  • appendonly yes #启用aof持久化方式
  • appendfsync always #收到写命令就立即写入磁盘,最慢,但是保证完全的持久化
  • appendfsync everysec #美秒钟写入磁盘一次,在性能和持久化方面做了很好的折中
  • appendfsync no #完全依赖os,性能最好,持久化没保证
  • redis还支持一种追加式的操作日志记录,叫append only file,其日志文件以aof结尾,我们一般各为aof文件。要开启aof日志的记录,你需要在配置文件中进行如下设置:

aof引发的问题:

aof的方式也同时带来了另一个问题。持久化文件会变得越来越大.例如我们调用incr test命令100次,文件中必须保存全部的100条命令,其实有99条都是多余的。因为要恢复数据库的状态其实文件中保存一条set test 100 就够了。为了压缩aof的持久化文件。redis提供了bgrewriteaof命令。收到此命令redis将使用与快照类似的方式将内存中的数据以命令的方式保存到临时文件中,最后替换原来的文件。具体过程如下:

  • redis调用fork,现在有父子两个进程
  • 子进程根据内存中的数据库快照,往临时文件中写入重建数据库状态的命令。
  • 父进程继续处理client请求,除了把写命令写入到原来的aof文件中。同时把收到的写命令缓存起来.这样就能保证如果子进程重写失败的话并不会出问题。
  • 当子进程把快照内容写入已命令方式写到临时文件中后,子进程发信号通知父进程。然后父进程把缓存的写命令也写入到临时文件。
  • 现在父进程可以使用临时文件替换老的aof文件,并重命令名,后面收到的写命令也开始往新的aof文件中追加。

需要注意到是重写aof文件的操作,并没有读取旧的aof文件,而是将整个内存中的数据库内容用命令的方式重写了一个新的aof文件,这点和快照有点类似。接下来我们看一下实际的例子。

开启bgrewriteaof重写的方式

##开启AOF
vim /usr/local/redis/conf/redis.conf
   449  appendonly yes                  #修改本行内容开启AOF
  
#重启redis服务
[root@redis-master redis]# redis-cli -a 123456 shutdown
[4022] 08 Oct 23:27:22.183 # User requested shutdown...
[4022] 08 Oct 23:27:22.183 * Saving the final RDB snapshot before exiting.
[4022] 08 Oct 23:27:22.195 * DB saved on disk
[4022] 08 Oct 23:27:22.195 # Redis is now ready to exit, bye bye...
[1]+  Done                    redis-server /usr/local/redis/conf/redis.conf
[root@redis-master redis]# redis-server /usr/local/redis/conf/redis.conf &

#关于bgrewriteaof重写的配置文件代码如下:
vim   /usr/local/redis/conf/redis.conf 
   503  # Automatic rewrite of the append only file.
   504  # Redis is able to automatically rewrite the log file implicitly calling
   505  # BGREWRITEAOF when the AOF log size grows by the specified percentage.
   506  #
   507  # This is how it works: Redis remembers the size of the AOF file after the #它是如何工作的呢?redis会记住AOF文件的大小
   508  # latest rewrite (if no rewrite has happened since the restart, the size of #当最后一次重写的时候,如果在重启时没有重写发生。
   509  # the AOF at startup is used).  #那么AOF文件会在开始时被使用
   510  #
   511  # This base size is compared to the current size. If the current size is
   512  # bigger than the specified percentage, the rewrite is triggered. Also
   513  # you need to specify a minimal size for the AOF file to be rewritten, this
   514  # is useful to avoid rewriting the AOF file even if the percentage increase
   515  # is reached but it is still pretty small.
   516  #
   517  # Specify a percentage of zero in order to disable the automatic AOF
   518  # rewrite feature.
   519 
   520  auto-aof-rewrite-percentage 100 #当100%达到最小大小的时候才会执行重写
   521  auto-aof-rewrite-min-size 64mb  #自动重写aof文件的最小大小

Redis缓存数据库的安装与配置(3)的更多相关文章

  1. Redis缓存数据库的安装与配置(1)

    1.安装 tarxf redis-3.2.5.tar.gz cd redis-3.2.5 make mkdir -p /usr/local/redis/bin src目录下这些文件作用如下 redis ...

  2. Redis缓存数据库的安装与配置(2)

    1.为php安装redis客户端扩展 wget https://github.com/nicolasff/phpredis/archive/master.zip tar xf phpredis-mas ...

  3. 快速搭建Redis缓存数据库

    之前一篇随笔——Redis安装及主从配置已经详细的介绍过Redis的安装于配置.本文要讲的是如何在已经安装过Redis的机器上快速的创建出一个新的Redis缓存数据库. 一.环境介绍 1) Linux ...

  4. Django缓存机制以及使用redis缓存数据库

    目录 Django 配置缓存机制 缓存系统工作原理 Django settings 中 默认cache 缓存配置 利用文件系统来缓存 使用Memcache来缓存: 使用Local-memory来缓存: ...

  5. SSD Cloud Hosting–Linode-Mysql数据库的安装与配置

    接着上一篇的话题:SSD Cloud Hosting - Linode的配置和部署,搭建Java环境 8.Mysql数据库的安装与配置 安装 检查yum里边有没有mysql: yum list|gre ...

  6. mysql数据库的安装与配置

    mysql数据库的安装与配置及workbench的简单使用 mysql数据库社区版下载:https://dev.mysql.com/downloads/installer/ 我这里选的是社区安装版(适 ...

  7. mongoDB数据库的安装与配置

    noSql数据库MongoDB的安装地址:https://www.mongodb.com/download-center?jmp=nav#community 选择相应的版本进行下载,在此以window ...

  8. linux学习之centos(三):mysql数据库的安装和配置

    前言:mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库, ...

  9. linux应用之mysql数据库的安装及配置(centos)

    CentOS下Mysql数据库的安装与配置   如果要在Linux上做j2ee开发,首先得搭建好j2ee的开发环境,包括了jdk.tomcat.eclipse的安装(这个在之前的一篇随笔中已经有详细讲 ...

随机推荐

  1. Windows平台字符串完全指南

    The Complete Guide to C++ Strings : The Complete Guide to C++ Strings, Part I - Win32 Character Enco ...

  2. Consul 配置ACLs

    比如consul.exe 在D:\consul来个json配置文件在 D:\consul\config.jsonjson 格式{ "acl_datacenter": "d ...

  3. std::string::find_last_not_of

    public member function <string> std::string::find_last_not_of C++98 C++11 string (1) size_t fi ...

  4. AndroidStudio 添加 AndroidAnnotations

    1.添加对apt的依赖 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt. ...

  5. Android 回调的理解,觉得写得好就转过来。。。收藏一下

    转自:一个经典例子让你彻彻底底理解java回调机制 以前不理解什么叫回调,天天听人家说加一个回调方法啥的,心里想我草,什么叫回调方法啊?然后自己就在网上找啊找啊找,找了很多也不是很明白,现在知道了,所 ...

  6. 用ant打包apkbuilder找不到了的解决办法

    apkbuilder的情况下生成apk文件,其实apkbuilder是一个批处理文件,打开里面就能发现,其实他内部执行的是sdklib.jar里面的一个class,所以就知道怎么做了,很简单,我们自己 ...

  7. spring 跨域 CORS (Cross Origin Resources Share) 跨域

    Spring提供了三种方式跨域 1.CorsFilter 过滤器 2.<mvc:cors> Bean(全局,推荐使用) 3.@CrossOrigin注解 以上三种方式本质都是用来配置Cor ...

  8. 【luogu P3931 SAC E#1 - 一道难题 Tree】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3931 肉眼观察题目感觉可以跑最大流. 证明是如果拆断一棵树,可以最小割,最小割等于最大流. 注意: 图是无向 ...

  9. js标准对象——Date

    在JavaScript中,Date对象用来表示日期和时间. 要获取系统当前的时间: var now = new Date(); alert(now); now;//Mon Oct 23 2017 11 ...

  10. Mac通过域名查询IP地址

    Mac通过域名查询IP地址 方法一:使用Mac自带的"网络实用工具" 步骤: 搜索"网络使用工具",并打开: 点击LookUp,输入互联网地址,点击Lookup ...