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文件的最小大小
Redis缓存数据库的安装与配置(3)的更多相关文章
- Redis缓存数据库的安装与配置(1)
1.安装 tarxf redis-3.2.5.tar.gz cd redis-3.2.5 make mkdir -p /usr/local/redis/bin src目录下这些文件作用如下 redis ...
- Redis缓存数据库的安装与配置(2)
1.为php安装redis客户端扩展 wget https://github.com/nicolasff/phpredis/archive/master.zip tar xf phpredis-mas ...
- 快速搭建Redis缓存数据库
之前一篇随笔——Redis安装及主从配置已经详细的介绍过Redis的安装于配置.本文要讲的是如何在已经安装过Redis的机器上快速的创建出一个新的Redis缓存数据库. 一.环境介绍 1) Linux ...
- Django缓存机制以及使用redis缓存数据库
目录 Django 配置缓存机制 缓存系统工作原理 Django settings 中 默认cache 缓存配置 利用文件系统来缓存 使用Memcache来缓存: 使用Local-memory来缓存: ...
- SSD Cloud Hosting–Linode-Mysql数据库的安装与配置
接着上一篇的话题:SSD Cloud Hosting - Linode的配置和部署,搭建Java环境 8.Mysql数据库的安装与配置 安装 检查yum里边有没有mysql: yum list|gre ...
- mysql数据库的安装与配置
mysql数据库的安装与配置及workbench的简单使用 mysql数据库社区版下载:https://dev.mysql.com/downloads/installer/ 我这里选的是社区安装版(适 ...
- mongoDB数据库的安装与配置
noSql数据库MongoDB的安装地址:https://www.mongodb.com/download-center?jmp=nav#community 选择相应的版本进行下载,在此以window ...
- linux学习之centos(三):mysql数据库的安装和配置
前言:mysql简介 说到数据库,我们大多想到的是关系型数据库,比如mysql.oracle.sqlserver等等,这些数据库软件在windows上安装都非常的方便,在Linux上如果要安装数据库, ...
- linux应用之mysql数据库的安装及配置(centos)
CentOS下Mysql数据库的安装与配置 如果要在Linux上做j2ee开发,首先得搭建好j2ee的开发环境,包括了jdk.tomcat.eclipse的安装(这个在之前的一篇随笔中已经有详细讲 ...
随机推荐
- 本地数据库(sql server)插入一条新数据时,同步到服务器数据库
之前有个同学问我,本地数据库插入新数据时怎么同步到服务器上,当时我先想到是程序逻辑控制,作相应的处理. 但有时候我们程序不太好处理,那能不能从数据库入手呢,数据库不是有触发器(Trigger)吗,应该 ...
- 【Leetcode】【Easy】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 字符串相关函数-strcpy()与strcmp()
一些小问题,避免出现低级错误. 1.strcmp(s1,s2): 字符串指针不见'\0'不回头,这个常在与单个字符作比较时写着写着就忘了. char* p_ch1="this is an e ...
- selenium:IE浏览器获取cookie提示Could not retrieve cookies
from selenium import webdriver url = "https://www.baidu.com" dr = webdriver.Ie() dr.get(ur ...
- c++下各种编码格式转换的方法
1.利用c++11新特性 std::wstring_convert配合std::codecvt模板类 作者:Gomo Psivarh链接:https://www.zhihu.com/question/ ...
- (转)每天一个linux命令(1):ls命令
ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...
- Python爬虫利器之Beautiful Soup,Requests,正则的用法(转)
https://cuiqingcai.com/1319.html https://cuiqingcai.com/2556.html https://cuiqingcai.com/977.html
- IOS开发之——IOS模拟器调试蓝牙BLE
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/24399887 因为在iPhone 4s ...
- netbackup 8.1安装前注意事项
一.NetBackup 主服务器的 Web 服务器用户/组设置步骤 文章 ID:100034818 上次发布时间:2017-08-24 产品:NetBackup 问题 从 NetBackup 8. ...
- HDU 2586 How far away ?【LCA】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...