登录
首页 >  数据库 >  MySQL

SpringBoot数据库配置问题解析

来源:SegmentFault

时间:2023-01-12 09:57:10 307浏览 收藏

你在学习数据库相关的知识吗?本文《SpringBoot数据库配置问题解析》,主要介绍的内容就涉及到MySQL、Java、数据库、spring、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依赖包的版本,然后重新编译就可以了。

mysqlmysql-connector-java8.0.18

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数据库配置问题解析》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表