redis的搭建及应用(三)-Redis主从配置

Redis主从配置

为提升Redis的高可用性,需要搭建多个Redis集群以保证高可用性。常见搭建方式有:主从,哨兵集群等,本节我们搭建一主二从的多Redis架构。

redis主从安装1主2从的方式配置,以端口号为redis的主从文件夹。

主(master): 6379

从(slave): 6380, 6381

image-20230727164649219

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
image-20231125102224377
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
image-20231125102552377
修改日志
修改日志级别为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
image-20231125105436936
修改日志的输出位置

定义日志文件的输出位置到/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 ""
image-20231125105737726
配置本机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
image-20231125110817091
查看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
image-20231125163336145

redis从服务器配置(6380)

配置6380/conf/redis.conf文件

上传配置文件redis.conf

image-20230727161242302

配置主从 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>
image-20231125164603387
配置从只读

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

image-20231125173329121
配置服务器ip地址

修改redis.conf文件中的replica-announce-ip/port为本机(linux)的ip和docker映射的地址。

 732 replica-announce-ip 192.168.198.128733 replica-announce-port 6380
image-20231125165040194
创建运行容器
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
查看启动日志
  1. 启动信息

image-20231125170346321

  1. 主从连接信息

image-20231125170726876

  1. 检查master(6379)服务器日志

image-20231125171032937

进入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
image-20231125173804746

第二个redis从服务器(6381)

配置6380/conf/redis.conf文件

上传配置文件redis.conf

image-20230727161242302

配置主从 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>
image-20231125164603387
配置服务器ip地址

修改redis.conf文件中的replica-announce-ip/port为本机(linux)的ip和docker映射的地址。

 732 replica-announce-ip 192.168.198.128733 replica-announce-port 6381
image-20231125181355725
配置从只读

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

image-20231125173329121
创建运行容器
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
查看启动日志
  1. 启动信息

image-20231125182028290

  1. 主从连接信息
image-20231125182127682
  1. 检查master(6379)服务器日志

image-20231125182232026

进入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
image-20231125182422137

进入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
image-20231125182651344

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/580849.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python作业题百度网盘,python作业答案怎么查

大家好&#xff0c;小编来为大家解答以下问题&#xff0c;python作业题百度网盘&#xff0c;python作业答案怎么查&#xff0c;今天让我们一起来看看吧&#xff01; 1 以下代码的输出结果为&#xff1a; alist [1, 2, 3, 4] print(alist.reverse()) print(alist) A.[4, 3, 2, …

从 Google Gemini 到 OpenAI Q*(Q-Star):调研重塑生成人工智能(AI)的研究

文章目录 一、前言二、主要内容三、总结 &#x1f349; CSDN 叶庭云&#xff1a;https://yetingyun.blog.csdn.net/ 一、前言 这篇综述探讨了生成式人工智能不断发展的前景&#xff0c;重点关注混合专家&#xff08;MoE&#xff09;、多模态学习的变革性影响&#xff0c;以及对…

解决xcode15下载模拟器慢以及没有断点续传

问题描述&#xff1a;Xcode15 为了最小化安装包大小&#xff0c;iOS17模拟器需要单独安装。然而下载模拟器的时候&#xff0c;经常出现Could not download iOS... 的下载失败提示。 以下为解决方案&#xff1a; 一、直接下载IOS17模拟器的包 以下两种方式都可以 方法一&…

学习笔记:数据挖掘与机器学习

文章目录 一、数据挖掘、机器学习、深度学习的区别&#xff08;一&#xff09;数据挖掘&#xff08;二&#xff09;机器学习&#xff08;三&#xff09;深度学习&#xff08;四&#xff09;总结 二、数据挖掘体系三、数据挖掘的流程四、典型的数据挖掘系统 一、数据挖掘、机器学…

2023:代码岁月如歌,技术之路踏实前行

前言 转眼之间&#xff0c;2023年即将谢幕&#xff0c;这一年对于我而言充满了挑战、收获与成长。在这篇博客中&#xff0c;我将分享我在技术领域的一些心得体会&#xff0c;以及在项目和职场中的所思所感。愿这些文字能够为你带来启发&#xff0c;同时让我能够在反思中更进一…

Vuex状态管理(报警信息数量跟随变化)

需求&#xff1a;侧边栏显示报警信息数量 在store/project.js文件中定义相关状态 // 存储项目信息 const projectInfo JSON.parse(sessionStorage.getItem(projectInfo)) ? JSON.parse(sessionStorage.getItem(projectInfo)) : ; import { getUntreatedProjectAlarm } from …

Qt Creator可视化交互界面exe快速入门4

上一期介绍了信号与槽&#xff0c;本期介绍加法计算器 我们来新建一个项目 然后拖动设置按钮 还需要个输出框 这里拖动Line Edit 我这里只是简单演示一下&#xff0c;做个低配版计算器&#xff0c;再加个加号和一个等于号就结束了。 然后回到代码编辑部分&#xff0c;我们需要…

VGG网络分析与demo实例

参考自 up主的b站链接&#xff1a;霹雳吧啦Wz的个人空间-霹雳吧啦Wz个人主页-哔哩哔哩视频这位大佬的博客 Fun_机器学习,pytorch图像分类,工具箱-CSDN博客 VGG 在2014年由牛津大学著名研究组 VGG&#xff08;Visual Geometry Group&#xff09;提出&#xff0c;斩获该年 Imag…

Java 新手常踩得坑,清个缓存就解决了?

【IDEA教程】IDEA 如何清除缓存&#xff1f; 大家好&#xff0c;我是 JavaPub。 最近遇到群里小伙伴遇到一个很大的难题&#xff0c;相信这个问题很多人在初入行时都遇到过。 事情是这样&#xff0c;一个小伙伴刚入职一家公司&#xff0c;公司给了他一个任务&#xff0c;虽然…

公司使用了加密软件,文件无法复制

在当今数字化时代&#xff0c;企业面临着越来越多的数据泄露和信息安全威胁。为了保护公司的敏感信息和知识产权&#xff0c;许多企业选择使用加密软件来加强数据的安全性。其中一项重要的功能是防止未经授权的文件复制。本文将探讨公司使用加密软件后&#xff0c;为何文件无法…

枚举算法:解决问题的穷举之道(二)

&#x1f497;&#x1f497;&#x1f497;欢迎来到我的博客&#xff0c;你将找到有关如何使用技术解决问题的文章&#xff0c;也会找到某个技术的学习路线。无论你是何种职业&#xff0c;我都希望我的博客对你有所帮助。最后不要忘记订阅我的博客以获取最新文章&#xff0c;也欢…

macOS系统下载安装PyCharm社区版本的流程(详细)

第一步 进入PyCharm官网&#xff0c;链接&#xff1a;Get Your Educational Tool - JetBrains 第二步 选择下拉框&#xff0c;根据自己的电脑芯片选择下载版本&#xff08;芯片查看位置&#xff1a;设置-通用-关于本机&#xff09;然后点击Download按钮 ​​​​​​​ -- 第…

科研学习|论文解读——融合类目偏好和数据场聚类的协同过滤推荐算法研究

论文链接&#xff08;中国知网&#xff09;&#xff1a; 融合类目偏好和数据场聚类的协同过滤推荐算法研究 - 中国知网 (cnki.net) 摘要&#xff1a;[目的/意义]基于近邻用户的协同过滤推荐作为推荐系统应用最广泛的算法之一&#xff0c;受数据稀疏和计算可扩展问题影响&#x…

005.HCIA 传输层

传输层定义了主机应用程序之间端到端的连通性。传输层中最为常见的两个协议分别是传输控制协议TCP (Transmission Control Protocol)和用户数据包协议UDP (User Datagram Protocol)。 1、相关概念 a. 传输层的端口 端口范围&#xff1a;0-65535 知名端口&#xff1a;0-1023&…

图灵日记之java奇妙历险记--类和对象

目录 类的定义和使用类的定义格式 类的实例化类和对象的说明 this引用this引用的特性 对象的构造及初始化就地初始化构造方法 封装包导入包中的类自定义包 static成员static修饰成员变量static修饰成员方法 代码块代码块概念及分类构造代码块静态代码块 匿名对象 类的定义和使用…

运维工程师的出路到底在哪里

运维工程师的出路到底在哪里&#xff1f; 你是不是也常常听到身边的运维人员抱怨&#xff0c;他们的出路到底在哪里呢&#xff1f;别着急&#xff0c;让我告诉你&#xff0c;运维人员就像是IT界的“万金油”&#xff0c;他们像“修理工”一样维修服务器&#xff0c;像“消防员…

sleep(0)、sleep(1)与sleep(1000)函数是不是很迷?!

随着计算机科学和软件开发的飞速发展&#xff0c;开发者们常常需要在程序中引入一些时间控制的手段。其中&#xff0c;sleep函数成为了一种常见的工具&#xff0c;用于控制程序的执行速度、等待异步操作完成或者调度多线程任务。在这篇博客中&#xff0c;我们将深入研究三种睡眠…

安装、卸载、使用docker-compose

文章目录 Docker Compose一、安装Docker Compose二、卸载Docker Compose三、 使用docker compose编排nginxspringboot项目 Docker Compose 一、安装Docker Compose # Compose目前已经完全支持Linux、Mac OS和Windows&#xff0c;在我们安装Compose之前&#xff0c;需要先安装D…

在word文档中插入Latex格式的公式

用此方法可以不用在word中一点点插入公式&#xff0c;直接用Latex版的公式代码生成公式。 1.获取latex版公式 如我要在word中插入画框的公式&#xff0c;左边是该公式的latex版 也可以对公式截图使用如下的网页将公式的截图转为latex版 https://simpletex.cn/ai/latex_ocr …

Vue3超详细的ref()用法,看这一篇就够了

ref( ) 接受一个内部值&#xff0c;返回一个ref 对象&#xff0c;这个对象是响应式的、可更改的&#xff0c;且只有一个指向其内部值的属性 .value。 ref() 将传入参数的值包装为一个带 .value 属性的 ref 对象。 1、ref 对象是可更改的&#xff0c;即可以为 .value 赋予新的值…