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

news2025/1/13 0:18:47

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 listens
  49 # for connections from all available network interfaces on the host machine.
  50 # It is possible to listen to just one or multiple selected interfaces using
  51 # 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 to
  53 # start if the address is not available. Being not available only refers to
  54 # addresses that does not correspond to any network interfece. Addresses that
  55 # are already in use will always fail, and unsupported protocols will always BE
  48 # By default, if no "bind" configuration directive is specified, Redis listens
  49 # for connections from all available network interfaces on the host machine.
  50 # It is possible to listen to just one or multiple selected interfaces using
  51 # 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 to
  53 # start if the address is not available. Being not available only refers to
  54 # addresses that does not correspond to any network interfece. Addresses that
  55 # are already in use will always fail, and unsupported protocols will always BE
  56 # silently skipped.
  57 #
  58 # Examples:
  59 #
  60 # bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses
  61 # bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6
  62 # bind * -::*                     # like the default, all available interfaces
  63 #
  64 # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
  65 # internet, binding to all the interfaces is dangerous and will expose the
  66 # instance to everybody on the internet. So by default we uncomment the
  67 # following bind directive, that will force Redis to listen only on the
  68 # IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
  69 # will only be able to accept client connections from the same host that it is
  70 # running on).
  71 #
  72 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
  73 # 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 that
  78 # 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 the
  83 #    "bind" directive.
  84 # 2) No password is configured.
  85 #
  86 # The server only accepts connections from clients connecting from the
  87 # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
  88 # sockets.
  89 #
  90 # By default protected mode is enabled. You should disable it only if
  91 # you are sure you want clients from other hosts to connect to Redis
  92 # even if no authentication is configured, nor a specific set of interfaces
  93 # 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 force
 302 # Redis to log on the standard output. Note that if you use standard
 303 # output for logging but daemonize, logs will be sent to /dev/null
 304 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 attached
 707 # replicas in different ways. For example the "INFO replication" section
 708 # offers this information, which is used, among other tools, by
 709 # Redis Sentinel in order to discover replica instances.
 710 # Another place where this info is available is in the output of the
 711 # "ROLE" command of a master.
 712 #
 713 # The listed IP address and port normally reported by a replica is
 714 # obtained in the following way:
 715 #
 716 #   IP: The address is auto detected by checking the peer address
 717 #   of the socket used by the replica to connect with the master.
 718 #
 719 #   Port: The port is communicated by the replica during the replication
 720 #   handshake, and is normally the port that the replica is using to
 721 #   listen for connections.
 722 #
 723 # However when port forwarding or Network Address Translation (NAT) is
 724 # used, the replica may actually be reachable via different IP and port
 725 # pairs. The following two options can be used by a replica in order to
 726 # report to its master a specific set of IP and port, so that both INFO
 727 # and ROLE will report those values.
 728 #
 729 # There is no need to use both the options if you need to override just
 730 # the port or the IP address.
 731 #
 732 # replica-announce-ip 5.5.5.5
 733 # 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 of
 461 # 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 to
 469 #    stop accepting writes if it appears to be not connected with at least
 470 #    a given number of replicas.
 471 # 2) Redis replicas are able to perform a partial resynchronization with the
 472 #    master if the replication link is lost for a relatively small amount of
 473 #    time. You may want to configure the replication backlog size (see the next
 474 #    sections of this file) with a sensible value depending on your needs.
 475 # 3) Replication is automatic and does not need user intervention. After a
 476 #    network partition replicas automatically try to reconnect to masters
 477 #    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.128
 733 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-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: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 of
 461 # 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 to
 469 #    stop accepting writes if it appears to be not connected with at least
 470 #    a given number of replicas.
 471 # 2) Redis replicas are able to perform a partial resynchronization with the
 472 #    master if the replication link is lost for a relatively small amount of
 473 #    time. You may want to configure the replication backlog size (see the next
 474 #    sections of this file) with a sensible value depending on your needs.
 475 # 3) Replication is automatic and does not need user intervention. After a
 476 #    network partition replicas automatically try to reconnect to masters
 477 #    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.128
 733 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.coloradmin.cn/o/1340991.html

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

相关文章

Elasticsearch8.x结合OpenAI CLIP模型实现图搜图及文搜图功能

前言 在当今大数据时代&#xff0c;搜索引擎已经是许多应用的核心组件之一&#xff0c;近年随着大模型以及AI技术&#xff08;如&#xff1a;自然语言处理NLP&#xff09;的流行&#xff0c;这些技术的结合将会创造出更多的应用场景&#xff0c;比如&#xff1a;电商商品搜索、…

实习知识整理12:点击购物车渲染出购物车中的商品并实现在购物车界面对商品价格和数量的相关操作

1. 点击购物车渲染出购物车商品界面 通过userId从购物车表中查找商品的相关信息 前端&#xff1a;需要向后端传递userId 后端&#xff1a; CartMapper.java CartMapper.xml CartService.java 接口 CartServiceImpl.java 实现类 CartController.java cartIndex.html页面 …

第二十一章Java网络通信

网络通信这一章 基本分为三个部分 网络基础概念和TCP,UDP这三个部分主要如下&#xff1a; 计算机网络实现了堕胎计算机间的互联&#xff0c;使得它们彼此之间能够进行数据交流。网络应用程序就是再已连接的不同计算机上运行的程序&#xff0c;这些程序借助于网络协议&#x…

【Unity动画系统】Unity动画系统Animation详解,参数细节你是否弄清?

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;Uni…

电子学会C/C++编程等级考试2023年03月(七级)真题解析

C/C++等级考试(1~8级)全部真题・点这里 第1题:走出迷宫 当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单。 假设你已经得到了一个n*m的迷宫的图纸,请你找出从起点到出口的最短路。 时间限制:1000 内存限制…

docker学习笔记03-持久化存储

1.docker架构 2.docker持续化存储-数据卷 //以后台方式运行容器 (推荐) docker run -d -v 宿主机目录/文件的绝对路径:容器内目录/文件的绝对路径[:rw/ro] -p 主机端口:容器端口 --name容器名称 镜像ID/镜像名称[:版本号]执行下面命令 docker run -p 1122:3306 --name mysql99…

脆皮大学生“拯救”方案——智慧高校智能视频监控系统的建设

随着“脆皮大学生”的网络热梗爆火&#xff0c;大学生日常监管问题也浮出水面。虽然高校大学生作为成年人&#xff0c;可以对自己的日常行为进行自我约束&#xff0c;但由于大学生涉世未深&#xff0c;缺乏独立生活经验&#xff0c;社会关系简单&#xff0c;在校期间&#xff0…

嵌入式Linux:提升VMware虚拟机运行速度的方法

使用虚拟机运行Linux操作系统通常会比在物理机上直接安装系统的运行效率更低&#xff0c;本篇博文将介绍如何优化虚拟机的设置&#xff0c;进而提升虚拟机性能体验。 第1步&#xff1a;选择VMware菜单&#xff1a;编辑–>首选项–>更新&#xff0c;将”启动时检查产品更新…

为什么IDEA建议去掉StringBuilder,而要使用“+”拼接字符串

在字符串拼接时应该都见过下面这种提示&#xff1a; 大家普遍认知中&#xff0c;字符串拼接要用StringBuilder&#xff0c;那为什么idea会建议你是用呢&#xff0c;那到底StringBuilder和有什么具体区别呢&#xff0c;我们一起来探究一下。 普通拼接 普通的几个字符串拼接成一…

积极拥抱信创,思迈特软件与麒麟软件NeoCertify完成认证

近日&#xff0c;思迈特软件与麒麟软件有限公司进行了联合测试&#xff0c;并顺利完成产品兼容性测试。经评测&#xff0c;思迈特软件一站式大数据分析平台&#xff08;Smartbi Insight V11&#xff09;与银河麒麟高级服务器操作系统&#xff08;飞腾版&#xff09;V10、&#…

代码随想录算法训练营第三十天|332.重新安排行程、51. N皇后 、37. 解数独

332.重新安排行程 题目链接&#xff1a;力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 文档讲解&#xff1a;代码随想录 C代码&#xff1a; class Solution { public: unordered_map<string, map<string, int>> targets;bool backtrack…

【Seata源码学习 】篇四 TM事务管理器是如何开启全局事务

TM发送 单个或批量 消息 以发送GlobalBeginRequest消息为例 TM在执行拦截器链路前将向TC发送GlobalBeginRequest 消息 io.seata.tm.api.DefaultGlobalTransaction#begin(int, java.lang.String) Overridepublic String begin(String applicationId, String transactionServi…

一款降压型开关模式转换器解决方案

一、基本概述 TX4145 是一款降压型开关模式转换器。TX4145 在 6-60V 宽输入电源范围内实现不同峰值输出电流&#xff0c;并且具有出色的线电压和负载调整率。 TX4145 采用 PWM 电流模工作模式&#xff0c;环路易于稳定并提供快速的瞬态响应。 TX4145 外部提供 FS 脚&#xf…

[每周一更]-(第44期):GIT版本控制之忽略文件

基础概念 在 Git 中&#xff0c;可以通过 .gitignore 文件来指定不需要纳入版本控制的文件或文件夹&#xff0c;这些被忽略的文件或文件夹不会被提交到仓库中。 在项目根目录下创建一个名为 .gitignore 的文件&#xff0c;并在其中列出需要忽略的文件或文件夹。一些常见的示例…

java设计模式学习之【中介者模式】

文章目录 引言中介者模式简介定义与用途实现方式 使用场景优势与劣势在Spring框架中的应用聊天室示例代码地址 引言 想象一下一座忙碌的机场&#xff0c;各种飞机需要起飞、降落&#xff0c;而不同的飞行活动之间必须互不干扰。如果没有一个统一的控制系统&#xff0c;这将是一…

Vue ThreeJs实现银河系行星运动

预览 可通过右上角调整参数&#xff0c;进行光影练习 代码 <template><div id"body"></div> </template> <script>import * as THREE from three import { OrbitControls } from three/examples/jsm/controls/OrbitControls import …

FontsTest.java

package fonts;import java.awt.Font; import java.awt.GraphicsEnvironment;/*** Font测试* * 不同字体在不同操作系统是不一样的&#xff0c;更新* * linux&#xff1a; https://blog.csdn.net/spencer_tseng/article/details/135232675windows&#xff1a; https://blog.cs…

48道Linux面试题

本博客将汇总 Linux 面试中常见的题目&#xff0c;并提供详细的解答。 文章目录 1、绝对路径用什么[符号表](https://so.csdn.net/so/search?q符号表&spm1001.2101.3001.7020)示&#xff1f;当前目录、上层目录用什么表示&#xff1f;主目录用什么表示? 切换目录用什么命…

活动回顾 (下) | 机器学习系统趋势研判,大咖金句汇总

作者&#xff1a;三羊、李宝珠、李玮栋、Yudi、xixi 编辑&#xff1a;李宝珠 在大模型时代的浪潮中&#xff0c;机器学习系统正经历着前所未有的变革。模型规模的急剧膨胀&#xff0c;让我们见证了 AI 能力的巨大提升&#xff0c;然而这种提升不仅为各个领域带来了新的机遇&…

Java版企业电子招标采购系统源码——鸿鹄电子招投标系统的技术特点

在数字化时代&#xff0c;采购管理也正经历着前所未有的变革。全过程数字化采购管理成为了企业追求高效、透明和规范的关键。该系统通过Spring Cloud、Spring Boot2、Mybatis等先进技术&#xff0c;打造了从供应商管理到采购招投标、采购合同、采购执行的全过程数字化管理。通过…