MySQL5.6 (5.7-8) 基于shardingsphere5.1.1 Sharding-Proxy模式读写分离

本文基于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:
      - root@%:123456  # 启动该服务的用户密码
      - sharding@: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
  1. 配置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

 

  1. 启动服务
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
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值