How to set correct MySQL JDBC timezone in Spring Boot configuration

前端 未结 11 1162
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 01:28

DB:

$ mysql --version
mysql  Ver 14.14 Distrib 5.6.27, for osx10.10 (x86_64) using  EditLine wrapper

Spring Boot: 2.1.1.RELEASE

The

相关标签:
11条回答
  • 2020-12-14 02:16

    Another way is to put into the file application.properties the next line:

    spring.jpa.properties.hibernate.jdbc.time_zone=UTC
    

    It comes as an option in: https://www.baeldung.com/mysql-jdbc-timezone-spring-boot

    0 讨论(0)
  • 2020-12-14 02:19

    Set useLegacyDatetimeCode false and set ServerTimezone.

    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/avmaint-local?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
    
    0 讨论(0)
  • 2020-12-14 02:19

    I've run into the same issue but adding

    ?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
    

    after your schema name fixed this issue for me. Should look something like this

    spring.datasource.url = jdbc:mysql://localhost:3306/my-schema-name?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
    

    I'm currently using MySQL v8 and this works well for me.

    0 讨论(0)
  • 2020-12-14 02:23

    I'm using MySQL 8 and solved this question by injecting the value of the user.timezone property.

    Using Java 8 in my project.

    Relevant settings in appliction.properties:

    spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
    spring.datasource.url=jdbc:mysql://localhost:3306/mudi?serverTimezone=${user.timezone}
    

    Using 2.3.4.RELEASE as parent project.

    Dependencies used:

    • spring-boot-starter-thymeleaf
    • spring-boot-starter-web
    • spring-boot-devtools
    • spring-boot-starter-test
    • junit-vintage-engine
    • spring-boot-starter-data-jpa
    • mysql-connector-java

    HTH,

    WB::

    0 讨论(0)
  • 2020-12-14 02:23

    Add useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false to your connection string:

    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/avmaint-local?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    
    0 讨论(0)
提交回复
热议问题