遇到一个问题
在这里插入图片描述
关键点:
You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

翻译一下:
CST 2022 WARN:不建议在没有服务器身份验证的情况下建立SSL连接。 根据MySQL 5.5.45+、5.6.26+和5.7.6+的要求,如果没有设置显式选项,默认必须建立SSL连接。 为了符合不使用SSL的现有应用程序,verifyServerCertificate属性被设置为’false’。 您需要通过设置useSSL=false显式禁用SSL,或者设置useSSL=true并为服务器证书验证提供信任存储区。

解决:
找到数据库和项目连接的配置文件
application-prod.properties

spring.datasource.url=jdbc:mysql://localhost:3306/life_vest?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false

//原来:
//spring.datasource.url=jdbc:mysql://127.0.0.1:3306/life_vest?useUnicode=true&characterEncoding=utf-8

试错:
直接添加&useSSL=false,没有效果

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/life_vest?useUnicode=true&characterEncoding=utf-8&useSSL=false
或者
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/life_vest?useSSL=false&useUnicode=true&characterEncoding=utf-8

查到的资料

第一种处理办法
jdbcUrl=jdbc:mysql://localhost:3306/zht?useUnicode=true&characterEncoding=utf-8&useSSL=false

1.先将mysql-connector-java的版本改到5.5.45之下
2.再将jdbcUrl改成如下

jdbcUrl=jdbc:mysql://localhost:3306/zht?useUnicode=true&characterEncoding=utf-8
第二种处理办法

如果你觉得更喜欢5.5.45以后的版本,那么需要将jdbc.properties里jdbcUrl换成新的:

jdbc:mysql://localhost:3306/zht?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&zeroDateTimeBehavior=convertToNull

参数名称 | 参数说明
user | 数据库用户名(用于连接数据库)
password | 用户密码(用于连接数据库)
useUnicode | 是否使用Unicode字符集,如果参数characterEncoding | 设置为gb2312或gbk,本参数值必须设置为true
characterEncoding | 当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk
autoReconnect | 当数据库连接异常中断时,是否自动重新连接?
autoReconnectForPools | 是否使用针对数据库连接池的重连策略
failOverReadOnly | 自动重连成功后,连接是否设置为只读?
maxReconnects | autoReconnect设置为true时,重试连接的次数
initialTimeout | autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒
connectTimeout | 和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本
socketTimeout | socket操作(读写)超时,单位:毫秒。 0表示永不超时

附录:

完整application-prod.properties
阿里云 配置springboot与mysql数据库

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/life_vest?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false
//数据库名称
spring.datasource.username=root
//数据库密码
spring.datasource.password=
//指定启动连接池时,初始建立的连接数量
spring.datasource.initialSize=5  
//指定必须保持连接的最小值(For DBCP and Tomcat connection pools)
spring.datasource.minIdle=5 
//指定连接池中最大的活跃连接数.
spring.datasource.maxActive=20  
//指定连接池等待连接返回的最大等待时间,毫秒单位.
spring.datasource.maxWait=60000  
//指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔
spring.datasource.timeBetweenEvictionRunsMillis=60000  
//指定一个空闲连接最少空闲多久后可被清除.
spring.datasource.minEvictableIdleTimeMillis=300000  
//指定获取连接时连接校验的sql查询语句.
spring.datasource.validationQuery=SELECT 1 FROM DUAL  
//当连接空闲时,是否执行连接测试.
spring.datasource.testWhileIdle=true  
//当从连接池借用连接时,是否测试该连接.
spring.datasource.testOnBorrow=false  
//在连接归还到连接池时是否测试该连接.
spring.datasource.testOnReturn=false  
//指定是否池化statements.
spring.datasource.poolPreparedStatements=true  
//
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20  
//
spring.datasource.filters=stat,wall,log4j  
//在使用DBCP connection pool时指定要配置的属性
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

本地的话可以改成

jdbc:mysql://192.16.30.40:3306/life_vest

参考:
mysql 建立SSL连接问题,设置useSSL=false显式禁用SSL,或者设置useSSL=true

springboot配置mysql数据库spring.datasource.url报错的解决

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐