java.sql.SQLException:The server time zone value xxx is unrecognized

Mysql不出问题的配置方法

今天在使用springboot操作数据库时,突然报错
java.sql.SQLException: 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.

这个错误是因为mysql连接的依赖包是高版本的情况下:
在配置datasource.url时不能简单的这样配:

#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crawler
spring.datasource.username=root
spring.datasource.password=root
        
#JPA Configuration:
spring.jpa.database=MySQL
spring.jpa.show-sql=true

而是要对其中的spring.datasource.driverClassName,以及spring.datasource.url增添一些信息

解决方法:

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crawler?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

或者如下,直接加上?serverTimezone=UTC也行

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crawler?serverTimezone=UTC

或者修改mysql依赖包为低版本

<!--MySQL连接包-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

总结
在项目中使用mysql驱动时,如果是低版本5.xx,可以继续使用com.mysql.jdbc.Driver驱动,高版本驱动(好像是6.0开始)就应当使用新的驱动com.mysql.cj.jdbc.Driver,IDEA有一个设置,如下图,在这里会提前设置好驱动版本,那么我们在application.properties中配置的驱动就会无效,我也是被这个坑了一波,所以下面对高低版本做一个分类集合。
在这里插入图片描述

百分百正确配置

  1. 首先,idea选择Mysql提前配置好的版本,这里我们设置常用的一个高一个低版本,可以先随便选择一个,因为后面还需要配置。在这里插入图片描述

  2. 高版本 - - - 8.0.20
    在左边框下拉,选择Mysql,开始配置高版本所依赖的jar包路径,一般选择我们自己的maven本地仓库,idea默认是在.m2里面的本地仓库中调用。记得最后Apply.
    在这里插入图片描述

  3. 低版本 - - - 5. 1. 6
    同样在左边框下拉,选择Mysql的另一个低版本,我这里是命名的Mysql 5.1,开始配置低版本所依赖的jar包路径,一般选择我们自己的maven本地仓库,idea默认是在.m2里面的本地仓库中调用。记得最后Apply.
    在这里插入图片描述

  4. IDEA中的配置可以说就这样的了,现在我们可以在第一步里面选择切换自己需要的高低版本了,但是我们还需要一个配置文件:application.properties
    由于IDEA默认配置了驱动,所以我们可以不用再配置spring.datasource.driverClassName,当然也可以配置,只是默认无效,可以作为一个说明

    #DB Configuration:高版本 Mysql --- 8.0.20 
    # spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crawler?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=root
    
    #JPA Configuration:
    spring.jpa.database=MySQL
    spring.jpa.show-sql=true
    
    
    #DB Configuration:低版本 Mysql --- 5.1.6
    # spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/crawler
    spring.datasource.username=root
    spring.datasource.password=root
    
    #JPA Configuration:
    spring.jpa.database=MySQL
    spring.jpa.show-sql=true
    
    

猜你喜欢

转载自blog.csdn.net/weixin_44505194/article/details/106674103
今日推荐