mysql8.4+mysql router读写分离

以下为容器环境内搭建
准备工作:

拉取镜像:

镜像版本
mysql8.4
container-registry.oracle.com/mysql/community-router8.4

下载mysql_shell
mysql-shell-9.0.1-linux-glibc2.17-x86-64bit.tar.gz
下载地址: https://downloads.mysql.com/archives/shell/

参考
这里对这篇文章进行扩展, 容器环境及一些异常情况的处理

  1. 准备mysql配置文件
    注意点: sql-mode与老版本(sql_mode)不一样, binlog-ignore-db=mysql去掉

mysql_m1: master.cnf

[mysqld]
server-id=100gtid_mode=on
enforce-gtid-consistency=onexplicit_defaults_for_timestamp=true
lower_case_table_names=1
sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION#binlog-ignore-db=mysql
max_allowed_packet=128M

mysql_s1: 配置文件(master.cnf)

[mysqld]
server-id=200gtid_mode=on
enforce-gtid-consistency=onexplicit_defaults_for_timestamp=true
lower_case_table_names=1
sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION#binlog-ignore-db=mysql
max_allowed_packet=128M
# 以下容器需要在同一网段内, 否则创建
services:mysql_m1:image: mysql:8.4volumes:- ${PWD}/mysql-shell-9.0.1-linux-glibc2.17-x86-64bit:/mysql-shell- ${PWD}/mysql_m1/data:/var/lib/mysql- ${PWD}/mysql_m1/conf/master.conf:/etc/mysql/conf.d/master.cnfports :- "3306:3306"environment:- MYSQL_ROOT_PASSWORD=rootmysql_s1:image: mysql:8.4volumes:- ${PWD}/mysql_s1/data:/var/lib/mysql- ${PWD}/mysql_s1/conf/master.conf:/etc/mysql/conf.d/master.cnfports :- "3306:3306"environment:- MYSQL_ROOT_PASSWORD=root
  1. 通过docker-compose方式启动以上两个mysql容器,进入容器初始化ReplicaSet
    进入mysql shell所在目录,
    ./mysql_shell/bin/mysqlsh
./mysqlsh mysql://root@localhost:3310
Please provide the password for 'root@localhost:3310': ****
Save password for 'root@localhost:3310'? [Y]es/[N]o/Ne[v]er (default No): n
MySQL Shell 9.0.1Copyright (c) 2016, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.Type '\help' or '\?' for help; '\quit' to exit.
Creating a Classic session to 'root@localhost:3310'
Fetching global names for auto-completion... Press ^C to stop.
Your MySQL connection id is 8
Server version: 8.4.3 MySQL Community Server - GPL
No default schema selected; type \use <schema> to set one.MySQL  localhost:3310 ssl  SQL > MySQL  localhost:3310 ssl  SQL > \js
Switching to JavaScript mode...MySQL  localhost:3310 ssl  JS >

创建用户并给权限

create user rw_shell@'%' identified by 'xxx';
这里还需要设置权限(参考文章开头的链接)
MySQL  localhost:3310 ssl  JS > dba.configureReplicaSetInstance('rw_shell@localhost:3311', {"clusterAdmin": "rw_shell@'%'"})
// 输入登陆密码
创建ReplicaSet 报错: MySQL  localhost:3310 ssl  JS > rs = dba.createReplicaSet("prd_op_service")
A new replicaset with instance '4a64c4d2402e:3306' will be created.* Checking MySQL instance at 4a64c4d2402e:3306This instance reports its own address as 4a64c4d2402e:3306
4a64c4d2402e:3306: Instance configuration is suitable.
Dba.createReplicaSet: 4a64c4d2402e:3306: instance has binlog filters configured, but they are not supported in InnoDB ReplicaSets. (MYSQLSH 51150)报错: 
mysql_m1_1       [ERROR] [MY-013797] [Server] Option --authentication-policy is set to an invalid value. Please check if the specified authentication plugins are valid.
mysql_m1_1       [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
mysql_m1_1       [ERROR] [MY-010119] [Server] Aborting
mysql_m1_1       [System] [MY-015018] [Server] MySQL Server Initialization - end.
mysql_cluster_mysql_m1_1 exited with code 1

master.cnf配置文件里:
authentication_policy=mysql_native_password // 去掉
binlog-ignore-db=mysql // 去掉
sql_mode=xxx // 改为 => sql-mode 修改sql模式以支持full_group_by

  1. 创建ReplicaSet
 MySQL  localhost:3310 ssl  JS > var rs = dba.createReplicaSet("prd_op_service")
A new replicaset with instance '513842479f48:3306' will be created.* Checking MySQL instance at 513842479f48:3306This instance reports its own address as 513842479f48:3306
513842479f48:3306: Instance configuration is suitable.* Checking connectivity and SSL configuration...
* Updating metadata...ReplicaSet object successfully created for 513842479f48:3306.
Use rs.addInstance() to add more asynchronously replicated instances to this replicaset and rs.status() to check its status.MySQL  localhost  JS > rs.status()
{"replicaSet": {"name": "prd_op_service", "primary": "940d1a52cbdf:3306", "status": "AVAILABLE", "statusText": "All instances available.", "topology": {"940d1a52cbdf:3306": {"address": "940d1a52cbdf:3306", "instanceRole": "PRIMARY", "mode": "R/W", "status": "ONLINE"}}, "type": "ASYNC"}
}
  1. 增加从节点, addInstance后,需要去手动重启从节点
 MySQL  localhost  JS > rs.addInstance('mysql_s1:3306')Adding instance to the replicaset...* Performing validation checksThis instance reports its own address as 54ef15541a78:3306
54ef15541a78:3306: Instance configuration is suitable.* Checking async replication topology...* Checking connectivity and SSL configuration...* Checking transaction state of the instance...WARNING: A GTID set check of the MySQL instance at '54ef15541a78:3306' determined that it contains transactions that do not originate from the replicaset, which must be discarded before it can join the replicaset.Instance '54ef15541a78:3306' has the following errant GTIDs that do not exist in the replicaset:6ce93fa1-a31e-11ef-8917-0242ac190002:1-7WARNING: Discarding these extra GTID events can either be done manually or by completely overwriting the state of '54ef15541a78:3306' with a physical snapshot from an existing replicaset member. To use this method by default, set the 'recoveryMethod' option to 'clone'.Having extra GTID events is not expected, and it is recommended to investigate this further and ensure that the data can be removed prior to choosing the clone recovery method.Please select a recovery method [C]lone/[A]bort (default Abort): c
* Updating topology
Monitoring Clone based state recovery of the new member. Press ^C to abort the operation.
Clone based state recovery is now in progress.NOTE: A server restart is expected to happen as part of the clone process. If the
server does not support the RESTART command or does not come back after a
while, you may need to manually start it back.* Waiting for clone to finish...
NOTE: 54ef15541a78:3306 is being cloned from 940d1a52cbdf:3306
** Stage DROP DATA: Completed
** Clone Transfer  FILE COPY  ============================================================    0%  In ProgressPAGE COPY  ============================================================    0%  Not StartedREDO COPY  ============================================================    0%  Not StartedNOTE: 54ef15541a78:3306 is shutting down...* Waiting for server restart... timeout 
WARNING: Clone process appears to have finished and tried to restart the MySQL server, but it has not yet started back up.Please make sure the MySQL server at '54ef15541a78:3306' is properly restarted. The operation will be reverted, but you may retry adding the instance after restarting it. 
ERROR: Error adding instance to replicaset: MYSQLSH 51156: Timeout waiting for server to restart
Reverting topology changes...
ERROR: Error while reverting replication changes: MySQL Error 2013: Lost connection to MySQL server during queryChanges successfully reverted.
ERROR: 54ef15541a78:3306 could not be added to the replicaset
ReplicaSet.addInstance: Timeout waiting for server to restart (MYSQLSH 51156)MySQL  localhost  JS > rs.status();
{"replicaSet": {"name": "prd_op_service", "primary": "940d1a52cbdf:3306", "status": "AVAILABLE", "statusText": "All instances available.", "topology": {"54ef15541a78:3306": {"address": "54ef15541a78:3306", "instanceRole": "SECONDARY", "mode": "R/O", "replication": {"applierStatus": "APPLIED_ALL", "applierThreadState": "Waiting for an event from Coordinator", "applierWorkerThreads": 4, "receiverStatus": "ON", "receiverThreadState": "Waiting for source to send event", "replicationLag": null, "replicationSsl": "TLS_AES_128_GCM_SHA256 TLSv1.3", "replicationSslMode": "REQUIRED"}, "status": "ONLINE"}, "940d1a52cbdf:3306": {"address": "940d1a52cbdf:3306", "instanceRole": "PRIMARY", "mode": "R/W", "status": "ONLINE"}}, "type": "ASYNC"}
}
  1. 启动路由节点
    参考 https://dev.mysql.com/doc/mysql-router/8.4/en/mysql-router-installation-docker.html
    访问6450端口,数据查询正常(连接从节点),开启事务访问正常(进入主节点)
测试路由是否生效./mysqlsh -u rw_shell -P 6450 -pMySQL  localhost:6450 ssl  SQL > select @@hostname;
+--------------+
| @@hostname   |
+--------------+
| 54ef15541a78 | // 从节点
+--------------+
1 row in set (0.0011 sec)
Statement ID: 3736MySQL  localhost:6450 ssl  SQL > begin; // 事务开始
Query OK, 0 rows affected (0.0036 sec)
Statement ID: 6895MySQL  localhost:6450 ssl  ★  SQL > select @@hostname;
+--------------+
| @@hostname   |
+--------------+
| 940d1a52cbdf | // 主节点
+--------------+
1 row in set (0.0006 sec)
Statement ID: 6967MySQL  localhost:6450 ssl  ★  SQL > commit; // 提交
Query OK, 0 rows affected (0.0006 sec)
Statement ID: 7407MySQL  localhost:6450 ssl  SQL > select @@hostname;
+--------------+
| @@hostname   |
+--------------+
| 54ef15541a78 | // 从节点
+--------------+
1 row in set (0.0011 sec)
Statement ID: 4408
  1. 启动应用服务连接mysql_router

连接mysql_router:6450端口报错:

Caused by: java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:448)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)

连接url加上allowPublicKeyRetrieval参数

Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1001)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)
…略
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 7
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)

连接url加上trackSessionState参数 参考
最终
jdbc:mysql://xxx:6450/xxx?allowPublicKeyRetrieval=true&trackSessionState=true

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

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

相关文章

STM32电源管理—实现低功耗

注&#xff1a; 本文是学习野火的指南针开发板过程的学习笔记&#xff0c;可能有误&#xff0c;详细请看B站野火官方配套视频教程&#xff08;这个教程真的讲的很详细&#xff0c;请给官方三连吧&#xff09; 在响应绿色发展的同时&#xff0c;在很多应用场合中都对电子设备的功…

【FPGA开发】AXI-Stream总线协议解读

文章目录 AXI-Stream概述协议中一些定义字节定义流的定义 数据流类别字节流连续对齐流连续不对齐流稀疏流 协议的信号信号列表 文章为个人理解整理&#xff0c;如有错误&#xff0c;欢迎指正&#xff01; 参考文献 ARM官方手册 《IHI0051B》 AXI-Stream概述 协议中一些定义 A…

LogViewer NLog, Log4Net, Log4j 文本日志可视化

LogViewer 下载 示例&#xff1a;NLog文本日志可视化软件&#xff0c;并且能够实时监听输出最新的日志 nlog.config 通过udp方式传输给LogViewer (udp://ip:port) <?xml version"1.0" encoding"utf-8" ?> <nlog xmlns"http://www.nlog-…

ES-针对某个字段去重后-获取某个字段值的所有值

针对上面表的数据&#xff0c;现在想根据age分组&#xff0c;并获取每个分组后的name有哪些(去重后)。 select age, GROUP_CONCAT(DISTINCT(name)) from testtable group by age ; 结果&#xff1a; 如果想要增加排序&#xff1a; SELECT age, GROUP_CONCAT(DISTINCT name)…

LabVIEW三针自动校准系统

基于LabVIEW的智能三针自动校准系统采用非接触式激光测径仪对标准三针进行精确测量。系统通过LabVIEW软件平台与硬件设备的协同工作&#xff0c;实现了数据自动采集、处理及报告生成&#xff0c;大幅提高了校准精度与效率&#xff0c;并有效降低了人为操作误差。 一、项目背景…

流程图图解@RequestBody @RequestPart @RequestParam @ModelAttribute

RequestBody 只能用一次&#xff0c;因为只有一个请求体 #mermaid-svg-8WZfkzl0GPvOiNj3 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-8WZfkzl0GPvOiNj3 .error-icon{fill:#552222;}#mermaid-svg-8WZfkzl0GPvOiNj…

【机器学习】机器学习中用到的高等数学知识-7.信息论 (Information Theory)

熵 (Entropy)&#xff1a;用于评估信息的随机性&#xff0c;常用于决策树和聚类算法。交叉熵 (Cross-Entropy)&#xff1a;用于衡量两个概率分布之间的差异&#xff0c;在分类问题中常用。 信息论作为处理信息量和信息传输的数学理论&#xff0c;在机器学习中具有广泛的应用。…

猎板PCB罗杰斯板材的应用案例

以下是几个猎板 PCB 与罗杰斯板材结合的具体案例&#xff1a; 案例一&#xff1a;5G 通信基站天线 PCB 在 5G 通信基站的天线系统中&#xff0c;对高频信号的传输和处理要求极高。猎板 PCB 采用罗杰斯板材&#xff0c;凭借其稳定的低介电常数&#xff08;如 RO4003C 板材&…

解读InnoDB数据库索引页与数据行的紧密关联

目录 一、快速走进索引页结构 &#xff08;一&#xff09;整体展示说明 &#xff08;二&#xff09;内容说明 File Header&#xff08;文件头部&#xff09; Page Header&#xff08;页面头部&#xff09; Infimum Supremum&#xff08;最小记录和最大记录&#xff09; …

Excel如何把两列数据合并成一列,4种方法

Excel如何把两列数据合并成一列,4种方法 参考链接:https://baijiahao.baidu.com/s?id=1786337572531105925&wfr=spider&for=pc 在Excel中,有时候需要把两列或者多列数据合并到一列中,下面介绍4种常见方法,并且提示一些使用注意事项,总有一种方法符合你的要求:…

uniApp项目运行到鸿蒙手机,应用图标一直是H,应用名一直是HBuilder问题

项目运行到鸿蒙手机&#xff0c;应用图标一直是H,应用名一直是HBuilder问题 应用运行到鸿蒙手机和鸿蒙模拟器&#xff0c;应用图标一直是H,应用名一直是HBuilder&#xff0c;在自动生成的harmony-configs文件夹下也没有配置的文件&#xff0c; 这时候需要你将DevEco Studio 下…

uniapp 自定义加载组件,全屏加载,局部加载 (微信小程序)

效果图 全屏加载 页面加载使用 局部加载 列表加载里面使用 使用gif html <template><view><view class"" v-if"typeFullScreen"><view class"loading" v-if"show"><view class""><i…

鸿蒙实战:使用隐式Want启动Ability

文章目录 1. 实战概述2. 实现步骤2.1 创建鸿蒙应用项目2.2 修改Index.ets代码2.3 创建LuzhouAbility2.4 创建Luzhou页面2.5 设置模块配置文件 3. 测试效果4. 实战总结 1. 实战概述 本次鸿蒙应用实战&#xff0c;先创建项目“ImplicitWantStartAbility”&#xff0c;接着修改In…

NLP论文速读(多伦多大学)|利用人类偏好校准来调整机器翻译的元指标

论文速读|MetaMetrics-MT: Tuning Meta-Metrics for Machine Translation via Human Preference Calibration 论文信息&#xff1a; 简介&#xff1a; 本文的背景是机器翻译&#xff08;MT&#xff09;任务的评估。在机器翻译领域&#xff0c;由于不同场景和语言对的需求差异&a…

AJAX学习(24.11.1-24.11.14)(包含HTTP协议)

AJAX学习&#xff08;24.11.1-11.14) 来源&#xff1a; 传智 | 高校学习平台-首页 传智播课&#xff1a;黑马程序员 1.服务器和客户端 1.服务器&#xff1a;存放和对外提供资源的电脑。 2.客户端&#xff08;用户&#xff09;&#xff1a;获取和消费资源的电脑。&#xff0…

好用的js组件库

lodash https://www.lodashjs.com/https://www.lodashjs.com/ uuid 用于生成随机数&#xff0c;常用于生成id标识 GitHub - uuidjs/uuid: Generate RFC-compliant UUIDs in JavaScripthttps://github.com/uuidjs/uuid dayjs 常用于时间的处理 安装 | Day.js中文网 (fenxi…

基于麒麟服务器操作系统V10版本,部署Nginx服务、MySql服务搭建PHP环境,实现静态网站平台的搭建。

一、环境准备 关闭防火墙。 查看当前防火墙的状态 systemctl status firewalld Copy 如果防火墙的状态参数是inactive,则防火墙为关闭状态。 如果防火墙的状态参数是active,则防火墙为开启状态。 关闭防火墙。 如果您想临时关闭防火墙,需要运行以下命令: systemctl…

[JavaWeb] 尚硅谷JavaWeb课程笔记

1 Tomcat服务器 Tomcat目录结构 bin&#xff1a;该目录下存放的是二进制可执行文件&#xff0c;如果是安装版&#xff0c;那么这个目录下会有两个exe文件&#xff1a;tomcat10.exe、tomcat10w.exe&#xff0c;前者是在控制台下启动Tomcat&#xff0c;后者是弹出GUI窗口启动To…

SRP 实现 Cook-Torrance BRDF

写的很乱&#xff01; BRDF&#xff08;Bidirectional Reflectance Distribution Function&#xff09;全称双向反射分布函数。辐射量单位非常多&#xff0c;这里为方便直观理解&#xff0c;会用非常不严谨的光照强度来解释说明。 BRDF光照模型&#xff0c;上反射率公式&#…

向量数据库FAISS之五:原理(LSH、PQ、HNSW、IVF)

1.Locality Sensitive Hashing (LSH) 使用 Shingling MinHashing 进行查找 左侧是字典&#xff0c;右侧是 LSH。目的是把足够相似的索引放在同一个桶内。 LSH 有很多的版本&#xff0c;很灵活&#xff0c;这里先介绍第一个版本&#xff0c;也是原始版本 Shingling one-hot …