Error reporting details
When using mybatis plus multiple data sources, the startup message cannot find the master data source
com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException: dynamic-datasource can not find primary datasource
at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.determinePrimaryDataSource(DynamicRoutingDataSource.java:91) ~[dynamic-datasource-spring-boot-starter-3.5.1.jar:3.5.1]
at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.getDataSource(DynamicRoutingDataSource.java:120) ~[dynamic-datasource-spring-boot-starter-3.5.1.jar:3.5.1]
at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.determineDataSource(DynamicRoutingDataSource.java:78) ~[dynamic-datasource-spring-boot-starter-3.5.1.jar:3.5.1]
at com.baomidou.dynamic.datasource.ds.AbstractRoutingDataSource.getConnection(AbstractRoutingDataSource.java:48) ~[dynamic-datasource-spring-boot-starter-3.5.1.jar:3.5.1]
......
Solution:
① The dependency of multiple data sources is introduced, but multiple data sources are not used
<!--This is the dependent version I use-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!--document-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>${version}</version>
</dependency>
Multi data source usage: use @ds to switch data sources.
@DS can be annotated on methods or classes, and there is a proximity principle that annotations on methods take precedence over annotations on classes.
annotation | result |
---|---|
no @DS | Default data source |
@DS(“databaseName”) | databaseName can be a group name or the name of a specific library |
② Multiple data sources are used but the main data source is not specified
spring: datasource: dynamic: primary: master # Set the default data source or data source group, the default value is master strict: false #Strictly match the datasource, default false. true does not match the specified datasource throw an exception, false uses the default datasource datasource: master: url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver # This configuration can be omitted for SPI support since 3.2.0 slave_1: url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic username: root password: 123456 driver-class-name: com.mysql.jdbc. slave_2: url: ENC(xxxxxx) # Built-in encryption, please check the detailed documentation for use username: ENC(xxxxxxxxxx) password: ENC(xxxxxxxxxx) driver-class-name: com.mysql.jdbc. #...... omit #The above will configure a default library master, a group slave with two sub-banks slave_1,slave_2
③ Check carefully if there is any alignment in the configuration
# Correct format spring: datasource: dynamic: strict: false primary: one datasource: one: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/demo?allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false username: root password: 123456 two: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/demo1?allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false username: root password: 123456
Read More:
- [Solved] Description: Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded
- [Solved] ERROR com.alibaba.druid.pool.DruidDataSource – init datasource error, url jdbcmysql
- [Solved] error {dataSource-1} init error java.sql.SQLException: com.mysql.cj.jdbc.Driver
- [Solved] init datasource error, url: jdbc:mysql://lcoalhost:3306/test com.mysql.cj.jdbc.exceptions.Com
- [Solved] MySQL Error: Can‘t find record in ‘order_form‘
- MYSQL Error: Can‘t find error-message file [How to Solve]
- [305]MYSQL 1062 error: duplicate entry ‘…’ for key ‘primary
- Solve MySQL table can not query, modify, delete and other operations and appear stuck
- [Solved] public key is not available client side (option serverRsaPublicKeyFile not set)
- How to Solve Error: Rsa Public Key not Find
- [Solved] Canal Error: Could not find first log file name in binary log index file
- RECEIVED ERROR PACKET: ERRNO = 1236, SQLSTATE = HY000 ERRMSG = COULD NOT FIND FIRST LOG FILE NAME IN BINARY LOG INDEX FILE
- Can mybatis prevent SQL injection
- [Solved] Hive Find Error: FAILED: RuntimeException java.lang.RuntimeException: cannot find field
- MYSQL Enter password:ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:3306‘
- MySql Install Error: Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘
- [Solved] mysqldump: Got error: 1556: “You can‘t use locks with log tables.“ when using LOCK TABLES
- ERROR 1075 (42000) Incorrect table definition; there can be only one auto column and it must be defi
- [Solved] MYSQL Command Execute Error: Can ‘t connect to local MySQL server through socket ‘/tmp/mysql.sock ‘(2) “
- How to Solve can‘t connect to local mysql server through socket ‘/var/lib/mysql/mysql.sock‘