使用mysql读写分离前应先配置mysql主从,参见下面文档配置主从

wget https://archive.apache.org/dist/shardingsphere/5.0.0/apache-shardingsphere-5.0.0-shardingsphere-proxy-bin.tar.gz
    tar -zxvf apache-shardingsphere-5.0.0-shardingsphere-proxy-bin.tar.gz
wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar
mv mysql-connector-java-8.0.11.jar apache-shardingsphere-5.0.0-shardingsphere-proxy/lib/

更改server.yaml配置,打开注释并且更改账号密码

rules:
  - !AUTHORITY
    users:
      - root@%:ebaad83fd82b4d2c
      - sharding@:sharding
    provider:
      type: ALL_PRIVILEGES_PERMITTED

添加config-readwrite-splitting.yaml配置如下

schemaName: platform

# 配置物理数据库参数
dataSources:
  # 主库,其中服务器名"master"应该换成数据库真实IP或域名
  write_ds:
    url: jdbc:mysql://127.0.0.1:3306/platform?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8
    username: root
    password: ebaad83fd82b4d2c
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 100
    minPoolSize: 1
  # 从库0,其中服务器名"slave"应该换成数据库真实IP或域名
  read_ds_0:
    url: jdbc:mysql://10.211.55.39/platform?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8
    username: root
    password: ebaad83fd82b4d2c
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 1000
    minPoolSize: 1
    # 从库1
#  read_ds_1:
#    url: jdbc:mysql://slave2/test?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8


# 配置主从规则
rules:
- !READWRITE_SPLITTING
  dataSources:
    pr_ds:
      writeDataSourceName: write_ds
      readDataSourceNames:
        - read_ds_0
#        - read_ds_1

启动项目
./bin/start.sh

最后修改:2022 年 01 月 27 日 02 : 45 PM
如果觉得我的文章对你有用,请随意赞赏