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

Mysql configuration method without problems

Today, when using springboot to operate the database, I suddenly reported an error
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.

This error is because the dependency package of mysql connection is a high version:
when configuring datasource.url, you cannot simply configure it like this:

#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

But to add some information to spring.datasource.driverClassName and spring.datasource.url

Solution:

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

Or as follows, you can add ?serverTimezone=UTCit directly

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

Or modify the mysql dependency package to a lower version

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

Summary
When using the mysql driver in the project, if it is a lower version 5.xx, you can continue to use the com.mysql.jdbc.Driverdriver, and the higher version driver (like 6.0) should use the new driver com.mysql.cj.jdbc.Driver. IDEA has a setting, as shown in the figure below, here will be advanced Set the driver version, then the driver we configured in application.properties will be invalid. I was also caught up in this pit, so the following is a classification set of high and low versions.
Insert picture description here

100% correct configuration

  1. First of all, idea chooses the version of Mysql configured in advance. Here we set a high and one low version commonly used. You can choose one at first, because you need to configure it later.Insert picture description here

  2. High version---8.0.20
    Pull down on the left frame, select Mysql, and start configuring the jar package path that the high version depends on. Generally, we choose our own local maven warehouse. By default, idea is called in the local warehouse in .m2. Remember to apply at the end.
    Insert picture description here

  3. Low version---5. 1. 6
    Also drop down on the left frame, select another low version of Mysql, here is the named Mysql 5.1, start to configure the jar package path that the low version depends on, generally choose our own maven local Warehouse, idea is called by default in the local warehouse in .m2. Remember to apply at the end.
    Insert picture description here

  4. IDEA configuration may say so, and now we can choose the version they need to switch the level at which the first step, but we also need a configuration file: application.properties;
    Because IDEA default configuration of the drive, so we can no longer configure spring.datasource.driverClassName, Of course, can also be configured, but the default is invalid, can be used as a description

    #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
    
    

Guess you like

Origin blog.csdn.net/weixin_44505194/article/details/106674103