下载安装

下载Sharding-Proxy最新的安装包

添加MySQL的Driver

下载mysql-connector-java-5.1.48.jar,将其放到Sharding-Proxy的lib文件夹里

修改配置文件

conf文件夹下,具体修改后内容如下:

配置账号密码

server.yaml

  1. authentication:
  2. users:
  3. root:
  4. password: root
  5. # sharding:
  6. # password: sharding
  7. # authorizedSchemas: sharding_db
  8. #
  9. #props:
  10. # max.connections.size.per.query: 1
  11. # acceptor.size: 16 # The default value is available processors count * 2.
  12. # executor.size: 16 # Infinite by default.
  13. # proxy.frontend.flush.threshold: 128 # The default value is 128.
  14. # # LOCAL: Proxy will run with LOCAL transaction.
  15. # # XA: Proxy will run with XA transaction.
  16. # # BASE: Proxy will run with B.A.S.E transaction.
  17. # proxy.transaction.type: LOCAL
  18. # proxy.opentracing.enabled: false
  19. # query.with.cipher.column: true
  20. # sql.show: false

配置读写分离

config-master_slave.yaml

  1. schemaName: master_slave_db
  2. dataSources:
  3. master_ds:
  4. url: jdbc:mysql://localhost:3306/demo_1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
  5. username: root
  6. password: 654321@qq.com
  7. connectionTimeoutMilliseconds: 30000
  8. idleTimeoutMilliseconds: 60000
  9. maxLifetimeMilliseconds: 1800000
  10. maxPoolSize: 50
  11. slave_ds_0:
  12. url: jdbc:mysql://localhost:3308/demo_1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
  13. username: root
  14. password: 654321@qq.com
  15. connectionTimeoutMilliseconds: 30000
  16. idleTimeoutMilliseconds: 60000
  17. maxLifetimeMilliseconds: 1800000
  18. maxPoolSize: 50
  19. masterSlaveRule:
  20. name: ms_ds
  21. masterDataSourceName: master_ds
  22. slaveDataSourceNames:
  23. - slave_ds_0

注意:实现读写分离的前提是配置的数据库首先要实现主从复制的功能,否则读写分离不能实现

配置数据分片

config-sharding.yaml

  1. schemaName: sharding_db
  2. dataSources:
  3. ds_0:
  4. url: jdbc:mysql://localhost:3306/sharding_master_1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
  5. username: root
  6. password: 654321@qq.com
  7. connectionTimeoutMilliseconds: 30000
  8. idleTimeoutMilliseconds: 60000
  9. maxLifetimeMilliseconds: 1800000
  10. maxPoolSize: 50
  11. ds_1:
  12. url: jdbc:mysql://localhost:3306/sharding_master_2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
  13. username: root
  14. password: 654321@qq.com
  15. connectionTimeoutMilliseconds: 30000
  16. idleTimeoutMilliseconds: 60000
  17. maxLifetimeMilliseconds: 1800000
  18. maxPoolSize: 50
  19. shardingRule:
  20. tables:
  21. t_user:
  22. actualDataNodes: ds_0.user
  23. t_student:
  24. actualDataNodes: ds_1.student
  25. bindingTables:
  26. - t_user,t_student
  27. defaultTableStrategy:
  28. none:

运行

命令行运行bin里面的start.bat,默认端口号是3307,可以在start.bat后面带上其他端口号

  1. D:\sharding-proxy\bin>start.bat 3308

运行结果如下:
image.png

image.png