ShardingSphere-Proxy5搭建使用
Apache ShardingSphere下的一个产品,定位为透明化的数据库代理端,与mycat类似,所有的分片都由其完成。
ShardingSphere-Proxy5下载安装
官网提供三种安装方式,这里主要记录两种
二进制包安装
- 官网下载二进制包apache-shardingsphere-5.2.0-shardingsphere-proxy-bin.tar.gz
- 下载MySQL驱动mysql-connector-java-8.0.22.jar(根据所使用的mysql下载对应版本)
- 将MySQl驱动放至shardingsphere-proxy解压目录中的
ext-lib
目录 - 修改配置conf/server.yaml
server.yaml是与shardingsphere-proxy服务相关的配置,这里主要配置下权限
rules:
- !AUTHORITY
users: # 配置连接shardingsphere-proxy的用户
- root@127.0.0.1:root
- sharding@:sharding
provider: # 授权模式
type: ALL_PERMITTED # 不用授权,获取所有权限
- 启动shardingsphere-proxy
# windows
# 指定端口号和配置文件目录, 默认端口为3307
bin/start.bat ${proxy_port} ${proxy_conf_directory}
出现以上信息代表部署成功
6. 连接测试
连接方式与mysql差不多,可以用mysql命令行连接,也可以用navicat连接。
Docker 方式安装
# 拉取镜像
docker pull apache/shardingsphere-proxy
# 启动临时容器
docker run -d --name tmp --entrypoint=bash apache/shardingsphere-proxy:5.2.0
# 配置文件拷贝
docker cp tmp:/opt/shardingsphere-proxy/conf /mnt/data/shardingsphere-proxy/
#删除临时容器
docker rm tmp
# 注意镜像的扩张依赖需要放在ext-lib目录,不能直接覆盖lib目录
docker run --name shardingsphere-proxy -d -v /mnt/data/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf -v /mnt/data/shardingsphere-proxy/ext-lib:/opt/shardingsphere-proxy/ext-lib -e PORT=3307 -p 33307:3307 apache/shardingsphere-proxy:5.2.0
docker run --name shardingsphere-proxy -d -v /mnt/data/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf -e PORT=3307 -p 33307:3307 apache/shardingsphere-proxy:5.2.0
ShardingSphere-Proxy5分库分表
创建数据源
修改config-sharding.yaml文件添加数据源配置
databaseName: sharding_db ##逻辑库。配置多个逻辑库时需要创建其他的配置文件,并且配置文件格式需为config-sharding***.yaml
dataSources: ## 数据源,连接真实物理库,注意物理库必须有相应的库存在,负责proxy无法启动。
ds_0:
url: jdbc:mysql://127.0.0.1:13307/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: sunday
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
ds_1:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: sunday
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
配置分片规则
继续修改config-sharding.yaml文件添加数据分片规则
databaseName: sharding_db
dataSources: ## 数据源,连接真实物理库,注意物理库必须有相应的库存在,负责proxy无法启动。
ds_0:
url: jdbc:mysql://127.0.0.1:13307/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: sunday
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
ds_1:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: sunday
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
## 分片规则配置
rules:
- !SHARDING
tables:
t_order: # 分片表
actualDataNodes: ds_${0..1}.t_order${0..1}
databaseStrategy: # 分库规则
standard: # 标准类型分片,目前官方有四种分片类型
shardingColumn: user_id
shardingAlgorithmName: alg_mod # 算法名称
tableStrategy: # 分表规则
standard:
shardingColumn: order_no
shardingAlgorithmName: alg_hash_mod # 算法名称,具体使用哪一种算法下面会根据算法名称配置
keyGenerateStr