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

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文件的最小大小

 

posted @ 2018-08-07 13:19  mushou  阅读(568)  评论(0编辑  收藏  举报