当前位置:网站首页>MySQL5.6 (5.7-8) 基于shardingsphere5.1.1 Sharding-Proxy模式读写分离
MySQL5.6 (5.7-8) 基于shardingsphere5.1.1 Sharding-Proxy模式读写分离
2022-06-23 05:11:00 【Mumunu-】
本文基于mysql5.6 ,实际测试中mysql5.7 ,8 配置一致 ,可以通用
首先搭建mysql主从,主从由mysql自己管理,这个就不赘述了。。
1.准备工作
下载 ShardingSphere-Proxy
官网地址
http://shardingsphere.apache.org/index_zh.html
下载跳转
https://shardingsphere.apache.org/document/current/cn/downloads/
下载MySQL驱动依赖
5版本:
https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar
8版本:
https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar
其他版本:
https://repo1.maven.org/maven2/mysql/mysql-connector-java/
安装java
这个自己按自己喜欢的方式装就行了 java8
将下载好的MySQL驱动移到解压好的中间件文件lib下
进入conf目录
这里就需要配置两个文件:server.yaml,config-readwrite-splitting.yaml
不管使用Sharding-Proxy做什么都需要配置server.yaml文件,他就是一个启动Sharding-Proxy的服务。
配置server.yaml
刚开始该文件全是注释,而我们配置读写分离大概只需要两个节点
rules:
- !AUTHORITY
users:
- [email protected]%:123456 # 启动该服务的用户密码
- [email protected]:123456 # 启动该服务的用户密码 意思就是用这个密码来登录shardingshare生成的模拟mysql
provider:
type: NATIVE
props:
max-connections-size-per-query: 1 # 一次查询请求在每个数据库实例中所能使用的最大连接数。
kernel-executor-size: 16 # 线程池大小 默认值: CPU核数
kernel-acceptor-size: 16 # 用于设置接收客户端请求的工作线程个数,默认为CPU核数*2
proxy-frontend-flush-threshold: 128 # 设置传输数据条数的 IO 刷新阈值
proxy-opentracing-enabled: false #是否开启链路追踪功能,默认为不开启
proxy-hint-enabled: false #是否启用hint算法强制路由 默认false
sql-show: ture #是否打印sql 默认falsefalse
# show-process-list-enabled: false
# Proxy backend query fetch size. A larger value may increase the memory usage of ShardingSphere Proxy.
# The default value is -1, which means set the minimum value for different JDBC drivers.
proxy-backend-query-fetch-size: -1
check-duplicate-table-enabled: false
sql-comment-parse-enabled: false
proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.
# Available options of proxy backend executor suitable: OLAP(default), OLTP. The OLTP option may reduce time cost of writing packets to client, but it may increase the latency of SQL execution
# if client connections are more than proxy-frontend-netty-executor-size, especially executing slow SQL.
proxy-backend-executor-suitable: OLAP
# proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.
# sql-federation-enabled: false
- 配置config-readwrite-splitting.yaml
schemaName: test #给你的连接起个名字
# 逻辑库,相当于独立主机与从机的一个中间库,搭起该服务后就要使用该逻辑库
# 注意修改已经配置好一个逻辑库并启动了,再修改这个逻辑库时就得先暂停该服务再启动。
dataSources:
write_ds:
url: jdbc:mysql://192.168.1.1:3306/test?allowPublicKeyRetrieval=true&useSSL=false
username: root
password: 123456
connectionTimeoutMilliseconds: 30000 # 连接超时时间
idleTimeoutMilliseconds: 60000 # 空闲连接回收超时毫秒数
maxLifetimeMilliseconds: 1800000 # 连接最大存活时间毫秒数
maxPoolSize: 50 # 最大连接数
minPoolSize: 1 # 最小连接数
read_ds_0:
url: jdbc:mysql://192.168.1.2:3306/test?allowPublicKeyRetrieval=true&useSSL=false
username: root
password: 123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
rules:
- !READWRITE_SPLITTING
dataSources:
readwrite_ds:
type: Static
props:
write-data-source-name: write_ds
read-data-source-names: read_ds_0
- 启动服务
bin/start.sh默认端口为3307
停止为:./stop.sh
netstat -nltp 可以查看端口是否起来
没起来可以看日志什么原因
验证
验证读写分离需要现关闭主从复制,先去从机将sql线程关闭,然后重启两个机子的服务。
这里就不贴图只说步骤:
先在主机与从机各自创键同一个库同一个表,并且在主机与从机的两个表各插入不同的数据。
连接Sharding-Proxy服务
mysql -h 服务的ip -P 服务的端口 -p
1
使用show databases;命令查看库时会发现库名与我们配置读写分离的逻辑库是一样的名字,然后我们切进该库然后使用show tables;查看表时自动会有一个跟主机与从机一样的表
查询该表的数据,会与从表的数据一样,当向该表插入数据时,在查询时,发现刚刚插入的数据没有显示
连接 主机的MySQL服务查询 主机的表,发现刚刚插入的数据有显示,那么证明MySQL8基于Sharding-Proxy5搭建读写分离就此成功!
一些部署中遇到的问题
连接失败
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused: connect
STACKTRACE:
java.net.ConnectException: Connection refused: connect
需要先查看相应的MySQL服务是否起来,相应的端口是否有暴露
Public Key Retrieval is not allowed
在config-readwrite-splitting.yaml文件中,配置主机与从机的每个url后面加上allowPublicKeyRetrieval=true
边栏推荐
- Softing dataFEED OPC Suite将西门子PLC数据存储到Oracle数据库中
- Microsoft interview question: creases in origami printing
- Day_ 11 smart communication health project - graphic report and poi Report
- Tencent security 2021 report white paper collection (download attached)
- Index - MySQL
- 开源生态|超实用开源License基础知识扫盲帖(下)
- [vivado] xilinxcedstore introduction
- Jour 13 Projet de santé mentale - chapitre 13
- mongodb 4. X binding multiple IP startup errors
- Illuminate\support\collection de duplication unique list de duplication
猜你喜欢

In the half year summary, it people just want to lie flat

Day_10 传智健康项目-权限控制、图形报表

Day_04 傳智健康項目-預約管理-套餐管理

Day_ 10 smart health project - permission control, graphic report

Day_09 传智健康项目-移动端开发-手机快速登录、权限控制

云盒子联合深信服,为南京一中打造智慧双模教学资源分享平台

把CSMA/CD、Token Bus、Token Ring说清楚

The softing datafeed OPC suite stores Siemens PLC data in an Oracle Database

11、 Realization of textile fabric off shelf function

C# wpf 通过绑定实现控件动态加载
随机推荐
Laravel log channel grouping configuration
A review: neural oscillation and brain stimulation in Alzheimer's disease
Pyqt5 setting window top left Icon
Day_ 08 smart health project - mobile terminal development - physical examination appointment
基于T5L1的小型PLC设计方案
Introduction to JVM principle
Gplearn appears assignment destination is read only
The softing datafeed OPC suite stores Siemens PLC data in an Oracle Database
json转化为proto
QT creator builds osgearth environment (osgqt msvc2017)
解析创客教育中的个性化学习进度
Extend your kubernetes API using the aggregation API
Given a node of a binary tree, return the successor node of the node
How to build a data application system based on overall value for energy enterprises
minio单节点部署 minio分布式部署 傻瓜式部署过程 (一)
Illustration Google V8 18: asynchronous programming (I): how does V8 implement micro tasks?
Day_04 传智健康项目-预约管理-套餐管理
[DaVinci developer topic] -42- how to generate template and header files of APP SWC
又到半年总结时,IT人只想躺平
What is a PDCA cycle? How to integrate PDCA cycle and OKR