How to set correct MySQL JDBC timezone in Spring Boot configuration

前端 未结 11 1161
佛祖请我去吃肉
佛祖请我去吃肉 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:03

    Following Shaun it was enough for me with this: spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/DEMOS?serverTimezone=UTC

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

    Set useLegacyDatetimeCode false is working for me. Thanks..

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

    Using jdk 14, I had to do this to my application.properties

    spring.jpa.properties.hibername.jdbc.time_zone=US/Michigan
    

    edit: formatting

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

    I added the following variable in the db url under application.properties:

    spring.datasource.url=jdbc:mysql://localhost:3306/db_name?serverTimezone=GMT-6
    

    and Spring Boot now runs ok.

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

    This worked for me. I set the variable in the db URL as such in application.properties:

    spring.datasource.url=jdbc:mysql://localhost:3306/db_name?serverTimezone=America/Los_Angeles
    
    0 讨论(0)
  • 2020-12-14 02:15

    Thanks for your answers, but I have found the solution.

    As I suspected, Hikari ignores whatever you put in the datasource url (so sorry guys, it doesn't matter what you chuck in there), essentially, it reads the timezone setting from MySQL itself, i.e., whatever the result you see when issuing the command

    SELECT @@GLOBAL.time_zone;

    in MySQL. In my case, the result was "SYSTEM", which is whatever my local machine it set at. This was AEDT, which is not supported by the MySQL driver and hence my exception.

    Running this same query in AWS yielded the value "UTC", which is supported (and, actually what I wanted).

    Therefore, I had to set the timezone in my local MySQL server.

    Firstly, I had to load the available timezones from my host (Mac OS X) into MySQL. I had to find out where the zoneinfo file was (/usr/share/zoneinfo in my case) then find out out where the `mysql_tzinfo_to_sql' utility was (bin directory of the MySQL installation) and use it to load my local machine's supported timezones. In Mac OS X, I ended up running the command:

    /usr/local/mysql/bin/mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

    Then in MySQL I could run the command

    SET GLOBAL time_zone = UTC;

    this is a valid timezone, and is synchronized with the cloud based instances.

    I think this is a real trap for a lot of people using MySQL with Spring Boot. It will work while people are in supported timezones, but if your development machine should switch to an unsupported timezone, it will quite mysteriously break, I'm surprised that it isn't documented anywhere. The source code of the MySQL Connector/J makes it obvious, but you wouldn't know it otherwise.

    Maybe its because MySQL is just so 5 years ago, and I'm an old fossil and, and, well, just get off my lawn!

    0 讨论(0)
提交回复
热议问题