1.问题
最近需要升级下项目,我之前版本是1.5.6
,升级到最新的2.2.x
时,发现会报以下错误:
2019-11-21 20:36:42.998 WARN 224 --- [ restartedMain] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2019-11-21 20:36:47.104 ERROR 224 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2.问题分析
问题很显然是数据库驱动找不到了,经过查阅资料,这是Spring Boot的更新日志:
Upgrade to Commons Pool2 2.6.1 #15946
Upgrade to Lettuce 5.1.4.RELEASE #15945
Upgrade to Lombok 1.18.6 #15928
Upgrade to Pooled Jms Version 1.0.4 #15927
Upgrade to Liquibase 3.6.3 #15926
Upgrade to Mimepull 1.9.11 #15925
Upgrade to Infinispan 9.4.6.Final #15924
Upgrade to Groovy 2.5.6 #15923
Upgrade to Tomcat 9.0.16 #15922
Upgrade to Log4j2 2.11.2 #15921
Upgrade to Artemis 2.6.4 #15920
Upgrade to Byte Buddy 1.9.10 #15919
Upgrade to Mysql 8.0.15 #15918
日志中将Mysql
更新到了8.0.15,新版中的Mysql引擎进行了升级,所以需要改动一下配置文件,主要是修改以下几个地方
spring.datasource.driver-class-name
spring.datasource.url
3.解决方案
将spring.datasource.driver-class-name
属性替换为com.mysql.cj.jdbc.Driver
将spring.datasource.url
属性值替换为jdbc:mysql://localhost:3306/finger?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
注意这里加了个对于时区的设置,默认是UTC
,但是与我们国家的时差相差8个小时,这里我将它设置为Asia/Shanghai
,也可设置成Asia/HongKong
下面我贴下新版本的完整配置参数:
spring.datasource.url=jdbc:mysql://localhost:3306/finger?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.maximum-pool-size=100
spring.datasource.hikari.max-lifetime=27000000
spring.datasource.hikari.minimum-idle=20
4.总结
主要问题在于新版本的数据库引擎进行了升级,所以需要对新版本的数据库引擎进行适配,下面是官方的更新日志地址