SpringBoot整合mysql-8 遇见的bug
问题描述
Maven中引入SpringBoot 2.1.6.RELEASE版本,导致项目运行失败,错误信息如下:
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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.16.jar:8.0.16]
bug出现原因
SpringBoot 2.1.6.RELEASE版本,默认使用的是 mysql-8.0.16 版本,这个版本导入的驱动和低版本不一样,需要修改一下。
解决方案
方案一:使用低版本mysql
Pom.xml
- 使用较低版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
application.yml
spring:
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/mybatis-example?useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver
方案二:修改yml文件
Pom.xml
- 使用较高版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
application.yml
- url 中添加时区属性
- driver 的驱动名称发生变化
spring:
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/mybatis-example?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.cj.jdbc.Driver