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

问题描述 投票:4回答:6

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

错误:

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忽略我自己机器的时区。有谁知道如何做到这一点?

mysql spring-boot hikaricp
6个回答
5
投票

设置useLegacyDatetimeCode假。

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

5
投票

谢谢你的回答,但我找到了解决方案。

正如我所怀疑的那样,Hikari忽略了你在数据源网址中放置的任何内容(很抱歉,你在那里查看的内容并不重要),本质上,它从MySQL本身读取时区设置,即,无论你在发布时看到的结果如何命令

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年前就是这样,而且我是一个古老的化石,而且,好吧,离开我的草坪吧!


2
投票

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

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

和Spring Boot现在运行正常。


2
投票

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

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

1
投票

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

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

0
投票

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
© www.soinside.com 2019 - 2024. All rights reserved.