Redis(四)------redis主从复制的实现
上一节我们搭建了redis主从复制,redis主从复制主要作用读写分离和数据备份的作用。主机负责写入数据,从机负责读取数据。
如果主机节点崩溃了,造成redis服务关闭,需要认为手动启动redis服务。
因此我们集成哨兵模式来实现解决这个问题,实现redis的高可用作用。
服务器 | 用途 | redis端口号 | master-name | 备注 |
---|---|---|---|---|
centos7 192.168.1.6 | 主机Master(写) | 6379 | redis1 | redis5.0 、 sentinel.conf |
centos7 192.168.1.4 | 从机Slave(读) | 6379 | redis2 | redis5.0 、 sentinel.conf |
centos7 192.168.1.5 | 从机Slave(读) | 6379 | redis3 | redis5.0 、 sentinel.conf |
第一步:复制sentinel.conf
将/usr/local/redis-5.0.7目录下的sentinel.conf文件复制到/usr/local/redis//bin/目录下
cp /usr/local/redis-5.0.7/sentinel.conf /usr/local/redis/bin/
清空文件内容,添加如下配置
【192.168.1.6 主机master的sentinel.conf文件】
#关闭保护模式
protected-mode no
#默认端口
port 26379
#允许后台运行
daemonize yes
#sentinel日志文件,注意日志文件的目录必须存在
logfile "/var/log/redis/sentinel.log"
#设置本机ip
sentinel announce-ip 192.168.1.6
#master
sentinel monitor mymaster 192.168.1.6 6379 2
#修改心跳为5000毫秒
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 2
sentinel failover-timeout mymaster 180000
#master如果设置密码 没有可以不用这一行
sentinel auth-pass mymaster 123456
配置说明:
sentinel monitor <master-name> <ip> <redis-port> <quorum>
监听192.168.1.6
主机。master-name为自定义名称,quorum是一个数字,指明当有多少个sentinel认为一个master失效时,master才算真正失效。master-name只能包含英文字母
,数字
,和.-_
【192.168.1.4 从机slave的sentinel.conf文件】
#关闭保护模式
protected-mode no
#默认端口
port 26379
#允许后台运行
daemonize yes
#sentinel日志文件,注意日志文件的目录必须存在
logfile "/var/log/redis/sentinel.log"
#设置本机ip
sentinel announce-ip 192.168.1.4
#master
sentinel monitor mymaster 192.168.1.6 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 2
sentinel failover-timeout mymaster 180000
sentinel auth-pass mymaster 123456
【192.168.1.5 从机slave的sentinel.conf文件】
#关闭保护模式
protected-mode no
#默认端口
port 26379
#允许后台运行
daemonize yes
#sentinel日志文件,注意日志文件的目录必须存在
logfile "/var/log/redis/sentinel.log"
#设置本机ip
sentinel announce-ip 192.168.1.5
#master
sentinel monitor mymaster 192.168.1.6 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 2
sentinel failover-timeout mymaster 180000
sentinel auth-pass mymaster 123456
2.1、启动sentinel
cd /usr/local/redis/bin/
#启动
./redis-sentinel sentinel.conf
2.2、查看sentinel进程
ps -ef|grep redis
2.3、防火墙添加23679端口号
#添加端口号
firewall-cmd --permanent --zone=public --add-port=26379/tcp
#重启防火墙
firewall-cmd --reload
#检查端口号是否生效
firewall-cmd --zone=public --query-port=26379/tcp
2.4、查看sentinel信息
【查看192.168.1.6主机】
./redis-cli -a 123456 -h 192.168.1.6 -p 26379
info sentinel
【查看192.168.1.4从机】
./redis-cli -a 123456 -h 192.168.1.4 -p 26379
info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.6:6379,slaves=2,sentinels=3
【查看192.168.1.5从机】
./redis-cli -a 123456 -h 192.168.1.5 -p 26379
info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.6:6379,slaves=2,sentinels=3
#查看sentinel.log日志
tail -f /var/log/redis/sentinel.log
一、WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys
将net.core.somaxconn = 1024
添加到/etc/sysctl.conf
中,然后执行sysctl -p
生效配置。
第一步:关闭192.168.1.6主机的redis服务
shutdown
#或者kill-9 xx杀掉进程
第二步:查看主机转移位置
【对192.168.1.4】
info sentinel
192.168.1.4:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.4:6379,slaves=2,sentinels=3
【对192.168.1.5】
info sentinel
192.168.1.5:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.4:6379,slaves=2,sentinels=3
发现主机由原来的192.168.1.6
转移到了192.168.1.4
注意:
redis主master一旦变更为从slave机后,就算重启启动服务,或者重启服务器后,redis角色也不会变更。
本质上哨兵机制实现自动更改sentinel.conf
文件配置,实现持久化的作用。
protected-mode no
#默认端口
port 26379
#允许后台运行
daemonize yes
#sentinel日志文件,注意日志文件的目录必须存在
logfile "/var/log/redis/sentinel.log"
#设置本机ip
sentinel myid 0e52436bc7a3a79bcb5936d32dae6848f9e6f907
#master
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.1.4 6379 2
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 1
# Generated by CONFIG REWRITE
dir "/"
sentinel leader-epoch mymaster 1
sentinel known-replica mymaster 192.168.1.6 6379
sentinel known-replica mymaster 192.168.1.5 6379
sentinel known-sentinel mymaster 192.168.1.5 26379 01f6728a7f36c9a72d2ef973a5337080326a4639
sentinel known-sentinel mymaster 192.168.1.6 26379 f37bee99e3ba2f9e4c19594675bf97b7558e3245
sentinel current-epoch 1
sentinel announce-ip "192.168.1.4"
第三步:向新主机写入数据
现在通过192.168.1.4
写入数据,因为只有主机才可以写入数据,如果数据写入成功,表示配置无误
[root@192 bin]# ./redis-cli -a 123456
127.0.0.1:6379> set k1 helloworld
OK
在192.168.1.5
读取数据。发现主从复制功能无误。
第四步:重新启动原来的主机192.168.1.6
启动原来的主机,检查主机192.168.1.6的节点角色是否有了变化,如果变为slave
,表示哨兵直接无误。
systemctl restart redis
ps -ef|grep redis
./redis-cli -a 123456
get k1
由上图发现,192.168.1.7
的节点角色变为savle,表示哨兵机制没有问题,但是发现master_link_status:down
,表示未连接上主机
查看日志
tail -f /var/log/redis/redis.log
参考Redis(四)------redis主从复制的实现
如果主机添加了密码,需要在从机添加masterauth 参数,否则提示验证失败。
原因:因为192.168.1.7
配置了redis密码,现在转换为从机, masterauth 参数没有配置
在192.168.1.7
redis.conf 添加内容
vi /usr/local/redis/bin/redis.conf
#主master的redis密码
masterauth 123456
#重启redis
systemctl restart redis
master_link_status:up
,连接上主机了
主从复制机制没有问题。
前面我们已经设置了redis开机自启,sentinel 设置与之相同。
系统服务目录里创建redis-sentinel.service文件
vi /lib/systemd/system/redis-sentinel.service
写入内容【ExecStart配置成自己的路径】
[Unit]
Description=redis-sentinel-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/bin/sentinel.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#设置开机自启动
systemctl enable redis-sentinel.service
如果你觉得本篇文章对你有所帮助的话,麻烦请点击头像右边的关注按钮,谢谢!
技术在交流中进步,知识在分享中传播
文章浏览阅读1.3k次,点赞45次,收藏22次。关联容器是C++标准库的一部分,用于存储和管理具有键值对的数据元素。与顺序容器(如vector和list)不同,关联容器的主要特点是它们能够快速查找特定键的值。这是因为关联容器内部使用了高效的数据结构(如二叉树或哈希表)来组织数据。std::set:一个集合,包含唯一元素,按特定顺序排序。std::map:一个映射,包含键值对,其中键是唯一的。std::multiset和std::multimap:类似于set和map,但允许键的重复。_c++的关联容器
文章浏览阅读5.5w次,点赞30次,收藏144次。一:如何修改el-select下拉框中选项的样式,网上的方法一般有两种:1.找到下拉框的类名,写一个全局的样式。2.通过/deep/来修改.el-select-dropdown__item的样式内容3.通过popper-class设置的类名添加样式以上几种方式中,第二种和第三种是无法生效的,而第一种形式虽然可以,但是会造成样式污染,在打包上传到服务器的时候,其他地方的样式可能会因此发生改变。二:问题分析上图中显示的是当选中el-select的选项时页面的结构,选项的容器并不在挂载的div#ap_el-select hover
文章浏览阅读904次。cdh配置hive.exec.dynamic.partition.mode看图说话,我是用的server2,所以根据自己情况配置_cdh hive.exec.dynamic.partition.mode
文章浏览阅读813次。请问大牛们:在使用Matlab:错误使用 mprint (line 231) Wrong # rnames in mprint,怎么解决?程序如下:T=12;N=10;W=normw(W1);y=A(:,[23]);x=A(:,[17:22]);for t=1:Tt1=(t-1)*N+1;t2=t*N;wx(t1:t2,:)=W*x(t1:t2,:);endxconstant=ones(N*T,1..._matlab print报错
文章浏览阅读467次。public String substring(int beginIndex, int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,一直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。示例: "ham_java substring的用法
文章浏览阅读1.5w次,点赞2次,收藏13次。C++做项目的过程中,需要实现文件打开保存的一个功能,当我对文件tmp.dat进行写操作以后,想要第二次对此文件进行写操作,此时用fopen_s,_wfopen_s均出现返回int error = 13也就是EACCES (Permission denied)的错误。而由于项目是Unicode编码,没办法用fopen进行文件操作(其实只要在预编译中加入_CRT_SECURE_NO_WARN_fopen_s和fopen的区别
文章浏览阅读55次。为什么需要socket.io? node.js提供了高效的服务端运行环境,但是由于浏览器端对HTML5的支持不一,为了兼容所有浏览器,提供卓越的实时的用户体验,并且为程序员提供客户端与服务端一致的编程体验,于是socket.io诞生。 socket.io设计的目标是支持任何的浏览器,任何Mobile设备。目前支持主流的PC浏览器(IE,Safari,Chrome,..._/package-my/msg-html5/msg-html5?url=ht
文章浏览阅读2.8k次。 对于需要插入引号字符的问题,可以通过下面的方法解决: 1. 如果需要输入单引号,可以连续输入两个单引号, 则会插入一个单引号到表中, 第一个单引号会被作为转义符, 如: db2 insert into sales(region) values() 则会插入一个单引号到字段region中。 db2 insert into sales(region) value_db2单引号入库
文章浏览阅读348次。文章目录三. etcd运行机制启动脚本参数查看成员信息验证当前etcd所有成员状态查看etcd数据信息etcd增删改查数据etcd数据watch机制四. etcd数据备份与恢复机制etcd v3版本数据备份与恢复五. 网络通信机制--CNI1. calico查看pod路由走向=2. flannelflannel pod状态VxLAN Directrouting=Flannel不同node上的pod的通信流程三. etcd运行机制etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建一个
文章浏览阅读1k次。1. 第一个QT问题: has modification time 18271224 s in the future具体问题如下图和文字:/Qt5.5.0/5.5/gcc_64/mkspecs/qconfig.pri’ has modification time 18271224 s in the future问题原因:qconfig.pri’ has modification time 18271224 s in the future 这句话中文意思就是qconfig.pri 文件的时间比较早_make warning qconfig.pri
文章浏览阅读8.6k次,点赞4次,收藏15次。之前做的一个项目有这么个要求,在日志管理系统里,需要将某些日志信息存储到数据库里,供用户、管理员查看分析。因此我就花了点时间搞了一下这一功能。说白了就是在用户在调用某接口的时候,相应的会在数据库进行保存,是谁对什么做了什么样的操作,要写明白,在这里使用spring提供的AOP来将切面类织入个个接口中,话不多说,直接上代码public class LogAspect {//自定义一个切面类 ..._logge日志怎么写入数据库
文章浏览阅读5.9k次,点赞2次,收藏12次。树莓派使用硬件加速视频转码现在随着智能设备普及以及宽带的升级,越来越的的视频素材在不断的产生。无论是我们自己拍摄的视频,还是从网上收集来的电影、电视剧,并不是全部都值得我们保存最高清的版本。打个比方,比如你下载了一个 1080P 甚至是 4K 的电影视频,看完了之后,觉得这个电影值得收藏,但又不至于非常喜欢。一个 1080P 2个小时左右的视频可能大概有 5G 左右,如果将其压缩到 720P,那..._树莓派4b硬编码