如何在 Spring 引导配置中设置正确的 MySQL JDBC 时区

How to set correct MySQL JDBC timezone in Spring Boot configuration

数据库:

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

Spring 开机:2.1.1.RELEASE

错误:

2019-01-01 15:56:25.849 ERROR 39957 --- [  restartedMain] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
> :bootRun
java.sql.SQLException: The server time zone value 'AEDT' 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.

我的属性文件的相关部分:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/avmaint-local?useSSL=false&serverTimezone=UTC
spring.datasource.username=#####
spring.datasource.password=########
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

我觉得奇怪的是,错误表明正在使用的时区是 AEDT,但我在 spring.datasource.url 中指定了 UTC。 Hikari 在初始化时会读取其他内容吗?

看起来很像 Hikari 忽略了数据库中的服务器时区设置 url 以支持使用我自己机器的时区,恰好是 'AEDT'(澳大利亚墨尔本)- 这是不需要的行为。我希望 Hikari 忽略我自己机器的时区。有谁知道如何让它做到这一点?

设置 useLegacyDatetimeCode false 并设置 ServerTimezone。

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/avmaint-local?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false

useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false 添加到您的连接字符串:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/avmaint-local?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

感谢您的回答,但我已经找到了解决方案。

正如我所怀疑的那样,Hikari 忽略了你在数据源 URL 中输入的任何内容(很抱歉,伙计们,你在其中输入什么并不重要),本质上,它从 [=38] 读取时区设置=] 本身,即无论您在发出命令时看到什么结果:

SELECT @@GLOBAL.time_zone;

在 MySQL。在我的例子中,结果是“SYSTEM”,这是我的本地机器设置的。这是 AEDT,MySQL 驱动程序不支持它,因此我例外。

运行 AWS 中的同一个查询产生了值“UTC”,这是受支持的(实际上是我想要的)。

因此,我必须在本地 MySQL 服务器中设置时区。

首先,我必须从主机 (Mac OS X) 将可用时区加载到 MySQL。我必须找出 zoneinfo 文件在哪里(在我的例子中是 /usr/share/zoneinfo),然后找出“mysql_tzinfo_to_sql”实用程序在哪里(MySQL 安装的 bin 目录)并使用它加载我本地机器支持的时区。在 Mac OS X 中,我结束了 运行 命令:

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

然后在 MySQL 我可以 运行 命令:

SET GLOBAL time_zone = UTC;

这个一个有效的时区,并且与基于云的实例同步。

我认为这对很多使用 MySQL 和 Spring Boot 的人来说是一个真正的陷阱。它会在人们处于支持的时区时工作,但如果您的开发机器应该切换到不受支持的时区,它会非常神秘地中断,我很惊讶它没有在任何地方记录。 MySQL Connector/J 的源代码很明显,但你不会知道它。

也许是因为 MySQL 是 5 年前的事了,我是一个老化石,而且,好吧,离开我的草坪吧!

我在 application.properties 下的数据库 url 中添加了以下变量:

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

和 Spring 启动现在运行正常。

这对我有用。我在 db URL 中设置变量 application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/db_name?serverTimezone=America/Los_Angeles

设置 useLegacyDatetimeCode false 对我有用。谢谢..

spring.datasource.url: jdbc:mysql://localhost:3306/employee_directory? 
useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false

跟随 Shaun,这对我来说已经足够了: spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/DEMOS?serverTimezone=UTC

另一种方法是在文件application.properties中放入下一行:

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

它作为选项出现在:https://www.baeldung.com/mysql-jdbc-timezone-spring-boot

使用 jdk 14,我必须对我的 application.properties

执行此操作
spring.jpa.properties.hibername.jdbc.time_zone=US/Michigan

编辑:格式化

我 运行 遇到了同样的问题,但添加了

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

在你的架构名称为我解决了这个问题之后。应该看起来像这样

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

我目前正在使用 MySQL v8,这对我来说效果很好。

我正在使用 MySQL 8 并通过注入 user.timezone 属性.[=11= 的值解决了这个问题]

在我的项目中使用 Java 8。

appliction.properties中的相关设置:

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

使用 2.3.4.RELEASE 作为父项目。

使用的依赖项:

  • 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,

世界银行::

我尝试了3种方法。

  1. TimeZone.setDefault(TimeZone.getTimeZone("Asia/Colombo")); //在Spring初始化器
  2. spring.jpa.properties.hibernate.jdbc.time_zone=Asia/Colombo // 在applications.properties
  3. -Duser.timezone=Asia/Colombo / / 在 POM 文件中设置 属性

我也尝试通过 JDBC URL;

设置属性
  • useLegacyDatetimeCode=false
  • serverTimezone=Asia/Colombo

以上都不适合我。我注意到最不可能的嫌疑人 Jackson 覆盖了我的时区。

spring.jackson.time-zone=Asia/Colombo (The Answer)

将它包含在你的身上application.properties(它本身就足够了)

在 Spring 启动时测试 - 2.1.5.RELEASE,MYSQL8.