sharding-jdbc sql show
时间: 2023-07-07 07:29:10 浏览: 57
Sh-JDBC 是一个开源的分布式数据库中间件,它提供了轻量级 Java 框架的形式,实现了对关系型数据库的分库分表、读写分离和分布式事务等功能。如果您想要查看 Sharding-JDBC SQL 执行情况,可以通过配置打开 SQL 显示功能,具体方法如下:
1. 在 Sharding-JDBC 配置文件中添加以下配置:
```
props:
sql.show: true
```
2. 重启应用程序,此时应用程序会输出 SQL 执行情况。
注意:开启 SQL 显示功能可能会暴露数据库敏感信息,建议仅在开发和测试环境中使用。在生产环境中,应该关闭 SQL 显示功能。
相关问题
sharding-jdbc读写分离yml
以下是一个示例的Sharding-JDBC的读写分离配置文件(YAML格式):
```yaml
spring:
shardingsphere:
datasource:
names: ds_master, ds_slave1, ds_slave2
ds_master:
url: jdbc:mysql://localhost:3306/master_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
type: com.zaxxer.hikari.HikariDataSource
ds_slave1:
url: jdbc:mysql://localhost:3306/slave1_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
type: com.zaxxer.hikari.HikariDataSource
ds_slave2:
url: jdbc:mysql://localhost:3306/slave2_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
type: com.zaxxer.hikari.HikariDataSource
masterslave:
name: ms_ds
master-data-source-name: ds_master
slave-data-source-names: ds_slave1, ds_slave2
props:
sql.show: true
```
这里配置了3个数据源,分别为主库和两个从库。`ds_master` 是主库,`ds_slave1` 和 `ds_slave2` 是从库。`masterslave` 配置项指定了主从关系,将主库指定为 `ds_master` ,从库指定为 `ds_slave1` 和 `ds_slave2` 。
shardingsphere-jdbc的yaml的配置规则
ShardingSphere-JDBC 的 YAML 配置规则如下:
1. 数据源配置
```yaml
dataSources:
ds0:
url: jdbc:mysql://localhost:3306/db0?serverTimezone=UTC
username: root
password: root
driverClassName: com.mysql.jdbc.Driver
ds1:
url: jdbc:mysql://localhost:3306/db1?serverTimezone=UTC
username: root
password: root
driverClassName: com.mysql.jdbc.Driver
```
2. 分片规则配置
```yaml
shardingRule:
tables:
user:
actualDataNodes: ds$->{0..1}.user$->{0..1}
tableStrategy:
standard:
shardingColumn: user_id
preciseAlgorithmClassName: com.xxx.PreciseShardingAlgorithm
rangeAlgorithmClassName: com.xxx.RangeShardingAlgorithm
shardingAlgorithmClassName: com.xxx.ComplexKeysShardingAlgorithm
keyGenerateStrategy:
column: user_id
type: SNOWFLAKE
```
3. 数据库和表的默认分片策略
```yaml
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
preciseAlgorithmClassName: com.xxx.PreciseShardingAlgorithm
rangeAlgorithmClassName: com.xxx.RangeShardingAlgorithm
shardingAlgorithmClassName: com.xxx.ComplexKeysShardingAlgorithm
defaultTableStrategy:
none:
```
4. 数据源和表的映射关系配置
```yaml
props:
sql:
show: true
# 从库负载均衡策略
executor.size: 20
executor.keepAliveSeconds: 300
executor.blockQueueSize: 1000
executor.threadFactory: net.sourceforge.jtds.jdbc.ThreadFactoryImpl
slave.load.balance.strategy.type: ROUND_ROBIN
slave.load.balance.algorithm.type: HASH
# 执行超时时间
max.timeout.milliseconds: 2000
# 是否打印 SQL
sql.show: true
# 是否打印 SQL 时输出原始 SQL
sql.simple: false
# 是否开启动态表路由
dynamic.table: true
# 是否允许表规则不存在,如果不存在则直接路由到主库
allow.table.without.rule: true
# 是否允许无法路由的 SQL 走主库
allow.broadcast.without.sharding: true
```
以上就是 ShardingSphere-JDBC 的 YAML 配置规则。