redis的搭建及应用(三)-Redis主从配置
Redis主从配置
为提升Redis的高可用性,需要搭建多个Redis集群以保证高可用性。常见搭建方式有:主从,哨兵集群等,本节我们搭建一主二从的多Redis架构。
redis主从安装1主2从的方式配置,以端口号为redis的主从文件夹。
主(master): 6379
从(slave): 6380, 6381

redis主服务器(master:6379)
使用vim工具打开配置文件,修改里面的内容。
NETWORK模块
################################## NETWORK #####################################47 48 # By default, if no "bind" configuration directive is specified, Redis listens49 # for connections from all available network interfaces on the host machine.50 # It is possible to listen to just one or multiple selected interfaces using51 # the "bind" configuration directive, followed by one or more IP addresses.52 # Each address can be prefixed by "-", which means that redis will not fail to53 # start if the address is not available. Being not available only refers to54 # addresses that does not correspond to any network interfece. Addresses that55 # are already in use will always fail, and unsupported protocols will always BE48 # By default, if no "bind" configuration directive is specified, Redis listens49 # for connections from all available network interfaces on the host machine.50 # It is possible to listen to just one or multiple selected interfaces using51 # the "bind" configuration directive, followed by one or more IP addresses.52 # Each address can be prefixed by "-", which means that redis will not fail to53 # start if the address is not available. Being not available only refers to54 # addresses that does not correspond to any network interfece. Addresses that55 # are already in use will always fail, and unsupported protocols will always BE56 # silently skipped.57 #58 # Examples:59 #60 # bind 192.168.1.100 10.0.0.1 # listens on two specific IPv4 addresses61 # bind 127.0.0.1 ::1 # listens on loopback IPv4 and IPv662 # bind * -::* # like the default, all available interfaces63 #64 # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the65 # internet, binding to all the interfaces is dangerous and will expose the66 # instance to everybody on the internet. So by default we uncomment the67 # following bind directive, that will force Redis to listen only on the68 # IPv4 and IPv6 (if available) loopback interface addresses (this means Redis69 # will only be able to accept client connections from the same host that it is70 # running on).71 #72 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES73 # JUST COMMENT OUT THE FOLLOWING LINE.74 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~75 bind 127.0.0.1 -::1
修改ip绑定地址为全网可访问。
bind *0.0.0.0 全网可访问
75 bind 0.0.0.0

protected-mode
77 # Protected mode is a layer of security protection, in order to avoid that78 # Redis instances left open on the internet are accessed and exploited.79 #80 # When protected mode is on and if:81 #82 # 1) The server is not binding explicitly to a set of addresses using the83 # "bind" directive.84 # 2) No password is configured.85 #86 # The server only accepts connections from clients connecting from the87 # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain88 # sockets.89 #90 # By default protected mode is enabled. You should disable it only if91 # you are sure you want clients from other hosts to connect to Redis92 # even if no authentication is configured, nor a specific set of interfaces93 # are explicitly listed using the "bind" directive.94 protected-mode yes
- 保护模式是一个避免你在互联网(外网)访问redis的机制。
- 当启用保护模式,而且没有密码时,服务器只接受来自IPv4地址(127.0.0.1)、IPv6地址(::1)或Unix套接字本地连接。(没密码+保护模式启动=本地访问)
- 默认是开启的
94 protected-mode no

修改日志
修改日志级别为DEBUG
293 # Specify the server verbosity level.294 # This can be one of:295 # debug (a lot of information, useful for development/testing)296 # verbose (many rarely useful info, but not a mess like the debug level)297 # notice (moderately verbose, what you want in production probably)298 # warning (only very important / critical messages are logged)299 loglevel notice

修改日志的输出位置
定义日志文件的输出位置到/var/log/redis.log
301 # Specify the log file name. Also the empty string can be used to force302 # Redis to log on the standard output. Note that if you use standard303 # output for logging but daemonize, logs will be sent to /dev/null304 logfile ""

配置本机ip和端口
主服务器部署在Docker或者其他网络代理工具,会使主服务器的ip,端口改变时,可以在配置// 文件中声明主服务器原始的ip和端口。
定义replica-announce-ip 和端口
tip: ip,port是linux的ip地址和端口号(不是容器内部ip,否则外界不能访问)。
192.168.xxx.yyy —linux服务器的ip地址
706 # A Redis master is able to list the address and port of the attached707 # replicas in different ways. For example the "INFO replication" section708 # offers this information, which is used, among other tools, by709 # Redis Sentinel in order to discover replica instances.710 # Another place where this info is available is in the output of the711 # "ROLE" command of a master.712 #713 # The listed IP address and port normally reported by a replica is714 # obtained in the following way:715 #716 # IP: The address is auto detected by checking the peer address717 # of the socket used by the replica to connect with the master.718 #719 # Port: The port is communicated by the replica during the replication720 # handshake, and is normally the port that the replica is using to721 # listen for connections.722 #723 # However when port forwarding or Network Address Translation (NAT) is724 # used, the replica may actually be reachable via different IP and port725 # pairs. The following two options can be used by a replica in order to726 # report to its master a specific set of IP and port, so that both INFO727 # and ROLE will report those values.728 #729 # There is no need to use both the options if you need to override just730 # the port or the IP address.731 #732 # replica-announce-ip 5.5.5.5733 # replica-announce-port 1234

查看redis状态
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:9ab01d97e6c3f5bd43ea60ddfc7cc42dddfa5fc4
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

redis从服务器配置(6380)
配置6380/conf/redis.conf文件
上传配置文件redis.conf
配置主从 Master-Replica replication
458 ################################# REPLICATION #################################459 460 # Master-Replica replication. Use replicaof to make a Redis instance a copy of461 # another Redis server. A few things to understand ASAP about Redis replication.462 #463 # +------------------+ +---------------+464 # | Master | ---> | Replica |465 # | (receive writes) | | (exact copy) |466 # +------------------+ +---------------+467 #468 # 1) Redis replication is asynchronous, but you can configure a master to469 # stop accepting writes if it appears to be not connected with at least470 # a given number of replicas.471 # 2) Redis replicas are able to perform a partial resynchronization with the472 # master if the replication link is lost for a relatively small amount of473 # time. You may want to configure the replication backlog size (see the next474 # sections of this file) with a sensible value depending on your needs.475 # 3) Replication is automatic and does not need user intervention. After a476 # network partition replicas automatically try to reconnect to masters477 # and resynchronize with them.478 #479 # replicaof <masterip> <masterport>

配置从只读
在配置文件末尾添加: slave-read-only yes(旧版本),新版本默认为从只读。

配置服务器ip地址
修改redis.conf文件中的replica-announce-ip/port为本机(linux)的ip和docker映射的地址。
732 replica-announce-ip 192.168.198.128733 replica-announce-port 6380

创建运行容器
docker run -it \
--name redis_6380 \
--privileged \
-p 6380:6379 \
--network wn_docker_net \
--ip 172.18.12.11 \
--sysctl net.core.somaxconn=1024 \
-e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" \
-v /usr/local/software/redis/6380/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/software/redis/6380/data/:/data \
-v /usr/local/software/redis/6380/log/redis.log:/var/log/redis.log \
-d redis \
/usr/local/etc/redis/redis.conf
查看启动日志
- 启动信息
- 主从连接信息
- 检查master(6379)服务器日志
进入redis-cli查看主从状态
[root@localhost conf]# docker exec -it redis_6380 bash
root@eb572dc438ae:/data# redis-cli127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.198.128
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_read_repl_offset:2800
slave_repl_offset:2800
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:d55333204ec41a62dd7f1074d6167392c21b6c24
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2800
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2731
repl_backlog_histlen:70

第二个redis从服务器(6381)
配置6380/conf/redis.conf文件
上传配置文件redis.conf
配置主从 Master-Replica replication
458 ################################# REPLICATION #################################459 460 # Master-Replica replication. Use replicaof to make a Redis instance a copy of461 # another Redis server. A few things to understand ASAP about Redis replication.462 #463 # +------------------+ +---------------+464 # | Master | ---> | Replica |465 # | (receive writes) | | (exact copy) |466 # +------------------+ +---------------+467 #468 # 1) Redis replication is asynchronous, but you can configure a master to469 # stop accepting writes if it appears to be not connected with at least470 # a given number of replicas.471 # 2) Redis replicas are able to perform a partial resynchronization with the472 # master if the replication link is lost for a relatively small amount of473 # time. You may want to configure the replication backlog size (see the next474 # sections of this file) with a sensible value depending on your needs.475 # 3) Replication is automatic and does not need user intervention. After a476 # network partition replicas automatically try to reconnect to masters477 # and resynchronize with them.478 #479 # replicaof <masterip> <masterport>

配置服务器ip地址
修改redis.conf文件中的replica-announce-ip/port为本机(linux)的ip和docker映射的地址。
732 replica-announce-ip 192.168.198.128733 replica-announce-port 6381

配置从只读
在配置文件末尾添加: slave-read-only yes(旧版本),新版本默认为从只读。

创建运行容器
docker run -it \
--name redis_6381 \
--privileged \
-p 6381:6379 \
--network wn_docker_net \
--ip 172.18.12.12 \
--sysctl net.core.somaxconn=1024 \
-e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" \
-v /usr/local/software/redis/6381/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v /usr/local/software/redis/6381/data/:/data \
-v /usr/local/software/redis/6381/log/redis.log:/var/log/redis.log \
-d redis \
/usr/local/etc/redis/redis.conf
查看启动日志
- 启动信息
- 主从连接信息

- 检查master(6379)服务器日志
进入redis-cli查看主从状态
[root@localhost log]# docker exec -it redis_6380 bash
root@eb572dc438ae:/data# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.198.128
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_read_repl_offset:6748
slave_repl_offset:6748
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:d55333204ec41a62dd7f1074d6167392c21b6c24
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6748
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2731
repl_backlog_histlen:4018

进入master查看主从信息
[root@localhost log]# docker exec -it redis_6379 bash
root@751e44287904:/data# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.198.128,port=6380,state=online,offset=6902,lag=0
slave1:ip=192.168.198.128,port=6381,state=online,offset=6902,lag=0
master_failover_state:no-failover
master_replid:d55333204ec41a62dd7f1074d6167392c21b6c24
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:6902
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:6902

相关文章:

redis的搭建及应用(三)-Redis主从配置
Redis主从配置 为提升Redis的高可用性,需要搭建多个Redis集群以保证高可用性。常见搭建方式有:主从,哨兵集群等,本节我们搭建一主二从的多Redis架构。 redis主从安装1主2从的方式配置,以端口号为redis的主从文件夹。 主…...

Java学习,一文掌握Java之SpringBoot框架学习文集(1)
🏆作者简介,普修罗双战士,一直追求不断学习和成长,在技术的道路上持续探索和实践。 🏆多年互联网行业从业经验,历任核心研发工程师,项目技术负责人。 🎉欢迎 👍点赞✍评论…...

javaWeb学生信息管理系统2
一、学生信息管理系统SIMS 一款基于纯Servlet技术开发的学生信息管理系统(SIMS),在设计中没有采用SpringMVC和Spring Boot等框架。系统完全依赖于Servlet来处理HTTP请求和管理学生信息,实现了信息的有效存储、检索和更新…...
Linux Shell 019-文本行处理工具sed
Linux Shell 019-文本行处理工具sed 本节关键字:Linux、Bash Shell、文本行处理工具 相关指令:sed、 sed介绍 sed是Stream Editor(流编辑器)的缩写,简称流编辑器;用来处理文件的。sed是一行一行读取文件…...

Ubuntu中fdisk磁盘分区并挂载、扩容逻辑卷
Ubuntu中fdisk磁盘分区并挂载、扩容逻辑卷 一:fdisk磁盘分区并挂载1.查看磁盘分区信息2.分区3.强制系统重新读取分区(避免重启系统)4.格式化分区5.创建挂载目录6.设置开机自动挂载:7.验证并自动挂载(执行了该命令不需要重启系统)8.查看挂载007.异常情况处…...
【leetcode】栈与队列总结
本文内容来自于代码随想录 栈 用栈实现队列 两个栈实现队列。思路:两个栈分别表示入栈和出栈。 入队:直接入栈出队: a. 出栈为空,先把入栈中的元素全部放到出栈中(相当于反过来,这样在出栈的时候先进的元…...
[EFI]HP Spectre 13 v102nl电脑 Hackintosh 黑苹果efi引导文件
硬件型号驱动情况主板 HP Spectre 13 v102nl 处理器Intel Core i7-7500U (7th gen - Kaby Lake)已驱动内存8 GB LPDDR3-1866 SDRAM已驱动硬盘512 GB Toshiba NVMe™ M.2 SSD已驱动显卡Intel HD Graphics 620已驱动声卡Conexant CX8200 (0x2008)已驱动网卡I1211 Gigabit Etherne…...

【Pytorch】学习记录分享8——PyTorch自然语言处理基础-词向量模型Word2Vec
【Pytorch】学习记录分享7——PyTorch自然语言处理基础-词向量模型Word2Vec 1. 词向量模型Word2Vec)1. 如何度量这个单词的?2.词向量是什么样子?3.词向量对应的热力图:4.词向量模型的输入与输出,其实…...

用Xshell连接虚拟机的Ubuntu20.04系统记录。虚拟机Ubuntu无法上网。本机能ping通虚拟机,反之不能。互ping不通
先别急着操作,看完再试。 如果是:本机能ping通虚拟机,反之不能。慢慢看到第8条。 如果是:虚拟机不能上网(互ping不通),往下一直看。 系统是刚装的,安装步骤:VMware虚拟机…...
人机对话--关于意识机器
人机对话–关于意识机器 这段内容是我和《通义千问》的对话。这本身展示的是人工智能的效果,同时这里面的内容也有人工智能相关,与各位分享。 我:阿尼尔赛斯 《意识机器》这本书写的是什么? 通义千问: 阿尼尔赛斯教…...
八股文打卡day16——计算机网络(16)
面试题:TCP连接是如何确保可靠性的? 我的回答: 1.数据分块控制。应用数据被分成被认为最适合传输的数据块大小,再发送到传输层,数据块被称为数据报文段或数据段。 2.序列号和确认应答。TCP为每一个数据包分配了一个序…...

Java Object浅克隆深克隆
对象克隆 把A对象的属性值完全拷贝给B对象,也叫对象拷贝,对象复制。 实现Cloneable接口,表示当前类的对象就可以被克隆,反之,表示当前类的对象就不能克隆。 如果一个接口里面没有抽象方法,表示当前的接口…...

概率的 50 个具有挑战性的问题 [8/50]:完美的桥牌
一、说明 我最近对与概率有关的问题产生了兴趣。我偶然读到了弗雷德里克莫斯特勒(Frederick Mosteller)的《概率论中的五十个具有挑战性的问题与解决方案》)一书。我认为创建一个系列来讨论这些可能作为面试问题出现的迷人问题会很有趣。每篇…...

自动驾驶学习笔记(二十四)——车辆控制开发
#Apollo开发者# 学习课程的传送门如下,当您也准备学习自动驾驶时,可以和我一同前往: 《自动驾驶新人之旅》免费课程—> 传送门 《Apollo开放平台9.0专项技术公开课》免费报名—>传送门 文章目录 前言 控制算法 控制标定 控制协议…...

【起草】【第十二章】定制ChatGPT数字亲人
身为普普通通的我们,不知道亲人们在哪一天就要离开这个世界 ? 作为普普通通的程序员,我们可以为我们的亲人做点什么 ? 让他们以数字资产形式留在人世间 ? 对话|6岁女孩病逝捐器官,妈妈:她去…...
MySQL数据库索引
索引的定义 索引是一个排序的列表,包含索引字段的值和其对应的行记录的数据所在的物理地址 索引的作用 加快表的查询速度,还可以对字段排序 索引的副作用 会额外占用磁盘空间;更新包含索引的表会花费更多的时间,效率会更慢 …...

【LLM 】7个基本的NLP模型,为ML应用程序赋能
在上一篇文章中,我们已经解释了什么是NLP及其在现实世界中的应用。在这篇文章中,我们将继续介绍NLP应用程序中使用的一些主要深度学习模型。 BERT 来自变压器的双向编码器表示(BERT)由Jacob Devlin在2018年的论文《BERT:用于语言…...

数字人私人定制
数字人是什么? 在回答这个问题之前,我们先回答另一个问题,人如何与人工智能交流?目前可以通过文字、语音、电脑屏幕、手机屏幕、平板、虚拟现实设备等和人工智能交流,为了得到更好的交流体验,人工智能必然…...
CollectionUtils
使用 CollectionUtils 类的常用方法 在Java开发中,我们经常需要对集合进行各种操作,而Apache Commons Collections库提供了一个方便的工具类 CollectionUtils,其中包含了许多实用的方法。在这篇博客中,我们将深入了解一些常用的方…...

IDEA运行Tomcat出现乱码问题解决汇总
最近正值期末周,有很多同学在写期末Java web作业时,运行tomcat出现乱码问题,经过多次解决与研究,我做了如下整理: 原因: IDEA本身编码与tomcat的编码与Windows编码不同导致,Windows 系统控制台…...

(十)学生端搭建
本次旨在将之前的已完成的部分功能进行拼装到学生端,同时完善学生端的构建。本次工作主要包括: 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...
React Native 导航系统实战(React Navigation)
导航系统实战(React Navigation) React Navigation 是 React Native 应用中最常用的导航库之一,它提供了多种导航模式,如堆栈导航(Stack Navigator)、标签导航(Tab Navigator)和抽屉…...

23-Oracle 23 ai 区块链表(Blockchain Table)
小伙伴有没有在金融强合规的领域中遇见,必须要保持数据不可变,管理员都无法修改和留痕的要求。比如医疗的电子病历中,影像检查检验结果不可篡改行的,药品追溯过程中数据只可插入无法删除的特性需求;登录日志、修改日志…...

循环冗余码校验CRC码 算法步骤+详细实例计算
通信过程:(白话解释) 我们将原始待发送的消息称为 M M M,依据发送接收消息双方约定的生成多项式 G ( x ) G(x) G(x)(意思就是 G ( x ) G(x) G(x) 是已知的)࿰…...
Objective-C常用命名规范总结
【OC】常用命名规范总结 文章目录 【OC】常用命名规范总结1.类名(Class Name)2.协议名(Protocol Name)3.方法名(Method Name)4.属性名(Property Name)5.局部变量/实例变量(Local / Instance Variables&…...

【机器视觉】单目测距——运动结构恢复
ps:图是随便找的,为了凑个封面 前言 在前面对光流法进行进一步改进,希望将2D光流推广至3D场景流时,发现2D转3D过程中存在尺度歧义问题,需要补全摄像头拍摄图像中缺失的深度信息,否则解空间不收敛…...

【SQL学习笔记1】增删改查+多表连接全解析(内附SQL免费在线练习工具)
可以使用Sqliteviz这个网站免费编写sql语句,它能够让用户直接在浏览器内练习SQL的语法,不需要安装任何软件。 链接如下: sqliteviz 注意: 在转写SQL语法时,关键字之间有一个特定的顺序,这个顺序会影响到…...
第25节 Node.js 断言测试
Node.js的assert模块主要用于编写程序的单元测试时使用,通过断言可以提早发现和排查出错误。 稳定性: 5 - 锁定 这个模块可用于应用的单元测试,通过 require(assert) 可以使用这个模块。 assert.fail(actual, expected, message, operator) 使用参数…...

Psychopy音频的使用
Psychopy音频的使用 本文主要解决以下问题: 指定音频引擎与设备;播放音频文件 本文所使用的环境: Python3.10 numpy2.2.6 psychopy2025.1.1 psychtoolbox3.0.19.14 一、音频配置 Psychopy文档链接为Sound - for audio playback — Psy…...