https://shardingsphere.apache.org/index_zh.html
https://shardingsphere.apache.org/document/legacy/4.x/document/cn/overview/
1、下载安装 Sharding-Proxy
4.0.0版本 链接: https://pan.baidu.com/s/1dJJVziURzEzrSp0v9liqjQ 提取码: fdf9
以下内容以4.0.0版本说明
4.0.0版本 ①将 mysql-connector-java-5.1.47.jar复制到 /mydata/apache-shardingsphere-incubating-4.0.0-sharding-proxy-bin/lib 目录下
链接: https://pan.baidu.com/s/12_Ghmo4Pms5NMCmsdAfs5A 提取码: eg3v
server.yaml 服务配置
authentication:
users:
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: true
config-sharding.yaml 分库分表配置
######################################################################################################
#
# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
#
######################################################################################################
schemaName: sharding_db
#
dataSources:
ds_0:
url: jdbc:mysql://10.206.0.14:3307/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: zan123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
ds_1:
url: jdbc:mysql://10.206.0.14:3307/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: zan123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
#
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_id
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item_${0..1}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_item_id
bindingTables:
- t_order,t_order_item
defaultDatabaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
defaultTableStrategy:
none:
config-master_slave_0.yaml 读写分离配置
######################################################################################################
#
# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
#
######################################################################################################
schemaName: sharding_db_0
dataSources:
master_0_ds:
url: jdbc:mysql://10.206.0.14:3307/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: zan123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_0:
url: jdbc:mysql://10.206.0.14:3317/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: zan123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
masterSlaveRule:
name: ms_ds_0
masterDataSourceName: master_0_ds
slaveDataSourceNames:
- slave_ds_0
loadBalanceAlgorithmType: ROUND_ROBIN
config-master_slave_1.yaml 读写分离配置
######################################################################################################
#
# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
#
######################################################################################################
schemaName: sharding_db_1
dataSources:
master_1_ds:
url: jdbc:mysql://10.206.0.14:3307/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: zan123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_1:
url: jdbc:mysql://10.206.0.14:3317/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: zan123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
masterSlaveRule:
name: ms_ds_1
masterDataSourceName: master_1_ds
slaveDataSourceNames:
- slave_ds_1
loadBalanceAlgorithmType: ROUND_ROBIN
注意:在只用navicat连接时 必须使用指定版本,否则会连接失败