定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。 目前先提供MySQL/PostgreSQL版本,它可以使用任何兼容MySQL/PostgreSQL协议的访问客户端(如:MySQL Command Client, MySQL Workbench, Navicat等)操作数据,对DBA更加友好。下载地址
- 向应用程序完全透明,可直接当做MySQL/PostgreSQL使用。
- 适用于任何兼容MySQL/PostgreSQL协议的的客户端。
下载的文件 lib 下有几个文件不是.jar结尾,需要改成.jar结尾,否则会报找不到类文件
下载的Sharding-Proxy默认是没有mysql驱动的,需要自己找到版本往lib目录下丢
分表
server.yaml 基本信息配置
authentication:
users:
# 账号 root
root:
# 密码 root
password: root
sharding:
password: sharding
# 逻辑库
authorizedSchemas: sharding_db
props:
max.connections.size.per.query: 1
acceptor.size: 16 # The default value is available processors count * 2.
executor.size: 16 # Infinite by default.
proxy.frontend.flush.threshold: 128 # The default value is 128.
# LOCAL: Proxy will run with LOCAL transaction.
# XA: Proxy will run with XA transaction.
# BASE: Proxy will run with B.A.S.E transaction.
proxy.transaction.type: LOCAL
proxy.opentracing.enabled: false
query.with.cipher.column: true
sql.show: false
config-sharding.yaml 分表配置
# 逻辑库
schemaName: sharding_db
#数据源
dataSources:
ds_0:
url: jdbc:mysql://192.168.81.104:3306/test_jdbc?serverTimezone=GMT%2B8
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
#分片规则
shardingRule:
tables:
# 逻辑表名
test_standard:
actualDataNodes: ds_0.test_standard_${1..2}
tableStrategy:
# 规则
inline:
# 注定分片的列
shardingColumn: id
# 注定分片的算法
algorithmExpression: test_standard_${id % 2 + 1}
# 主键生成策略
keyGenerator:
type: SNOWFLAKE
column: id
bindingTables:
- test_standard
defaultTableStrategy:
none:
连接
启动后默认端口 3307,然后可以使用 mysql 连接工具进行连接
对逻辑库创建表等操作,都会映射到物理库中。
分库分表
修改 config-sharding.yaml
# 逻辑库
schemaName: sharding_db
#数据源
dataSources:
ds_0:
url: jdbc:mysql://192.168.81.104:3306/test_jdbc1?serverTimezone=GMT%2B8
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
ds_1:
url: jdbc:mysql://192.168.81.104:3306/test_jdbc2?serverTimezone=GMT%2B8
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
#分片规则
shardingRule:
tables:
# 逻辑表名
test_standard:
actualDataNodes: ds_${0..1}.test_standard_${1..2}
tableStrategy:
# 规则
inline:
# 注定分片的列
shardingColumn: id
# 注定分片的算法
algorithmExpression: test_standard_${id % 2 + 1}
# 主键生成策略
keyGenerator:
type: SNOWFLAKE
column: id
bindingTables:
- test_standard
defaultDatabaseStrategy:
# 分库策略
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
defaultTableStrategy:
none:
创建表只能通过在配置中,把表提前预定义好,才可以创建。否则报错 Cannot find table rule and default data source with logic table
读写分离
修改 config-master_slave.yaml
schemaName: master_slave_db
dataSources:
master_ds:
url: jdbc:mysql://192.168.81.104:3306/user_db?serverTimezone=UTC&useSSL=false
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_0:
url: jdbc:mysql://192.168.81.104:3306/user_db?serverTimezone=UTC&useSSL=false
username: dev_fqr
password: Dev@fqr2021
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
masterSlaveRule:
name: ms_ds
masterDataSourceName: master_ds
slaveDataSourceNames:
- slave_ds_0