springboot集成mybatis,連接mysql時報CannotGetJdbcConnectionException


 如下圖所示異常:

1 com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. 
You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
2 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_102] 3 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_102] 4 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_102] 5 at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_102]

分析過程:

首先:有一條紅色警告。

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

 

按照提示將 spring.datasource.driverClassName 修改為 com.mysql.cj.jdbc.Driver

spring.datasource.driverClassName=com.mysql.jdbc.Driver

 

其次,我的 mysql-connector-java 版本為 8.0.18,在url上需要添加時區信息:

jdbc:mysql://host:port/dbname?characterEncoding=utf8&serverTimezone=UTC

添加后,異常消失。

 

 

 

PS:從6.0.2版本開始,如果mysql server未明確配置時區,則必須在jdbc連接參數中設置serverTimezone
否則,在啟動時報錯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM