SpringBoot数据库配置问题解析

Posted licky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot数据库配置问题解析相关的知识,希望对你有一定的参考价值。

mysql服务端和客户端不一致

服务可以正常启动,但数据库操作就一直等待中。异常日志返回如下:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 3 milliseconds ago.  The last packet sent successfully to the server was 3 milliseconds ago.

Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors

这里的原因是mysql版本问题导致的,我本地MySQL服务端的版本是:

mysql  Ver 8.0.22 for osx10.16 on x86_64 (Homebrew)

但maven配置MySQL用的却是5.7的客户端,就会导致链接MySQL数据库异常,一直处于等待中。

解决方法
更改maven中MySQL依赖包的版本,然后重新编译就可以了。

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.18</version>
</dependency>

MySQL连接配置问题

服务根本启动不起来,异常日志如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'org.springframework.cloud.autoconfigure.RefreshAutoConfiguration$JpaInvokerConfiguration\': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker\': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'dataSource\' defined in class path resource [cn/lalaframework/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: dynamic-datasource Please check the setting of primary
    
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker\': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'dataSource\' defined in class path resource [cn/lalaframework/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: dynamic-datasource Please check the setting of primary
    

意思大致是数据库连接的主配置获取不到,指定master主配置就可以了。

错误配置:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/cp_mcv_monitor?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root

正确配置:

spring.datasource.dynamic.primary=master
spring.datasource.dynamic.datasource.master.url=jdbc:mysql://localhost:3306/cp_mcv_monitor?characterEncoding=utf8&useSSL=true
spring.datasource.dynamic.datasource.master.username=root
spring.datasource.dynamic.datasource.master.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

以上是关于SpringBoot数据库配置问题解析的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot:thymeleaf 没有正确渲染片段

SpringBoot数据库配置问题解析

SpringBoot 源码解析 ----- Spring Boot的核心能力 - 自动配置源码解析

Relay.js 没有正确解析组合片段

(转)Spring Boot干货系列:默认日志logback配置解析

Springboot :BeanDefinitionStoreException: 解析配置类失败