Java MySQL JDBC 驱动程序 5.1.33 - 时区问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26515700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:40:52  来源:igfitidea点击:

MySQL JDBC Driver 5.1.33 - Time Zone Issue

javamysqltomcatjdbctimezone

提问by bluecollarcoder

Some background:

一些背景:

I have a Java 1.6 webapp running on Tomcat 7. The database is MySQL 5.5. Previously, I was using Mysql JDBC driver 5.1.23 to connect to the DB. Everything worked. I recently upgraded to Mysql JDBC driver 5.1.33. After the upgrade, Tomcat would throw this error when starting the app.

我有一个在 Tomcat 7 上运行的 Java 1.6 webapp。数据库是 MySQL 5.5。以前,我使用Mysql JDBC 驱动程序5.1.23 连接到数据库。一切正常。我最近升级到了 Mysql JDBC 驱动程序 5.1.33。升级后,Tomcat 启动应用程序时会抛出此错误。

WARNING: Unexpected exception resolving reference
java.sql.SQLException: The server timezone value 'UTC' is unrecognized or represents more than one timezone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc timezone value if you want to utilize timezone support.

Why is this happening?

为什么会这样?

采纳答案by bluecollarcoder

Apparently, to get version 5.1.33 of MySQL JDBC driver to work with UTC time zone, one has to specify the serverTimezoneexplicitly in the connection string.

显然,要让 MySQL JDBC 驱动程序的 5.1.33 版使用 UTC 时区,必须serverTimezone在连接字符串中明确指定。

jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

回答by antgar9

This is a bug in mysql-connector-java from version 5.1.33 to 5.1.37. I've reported it here: http://bugs.mysql.com/bug.php?id=79343

这是 mysql-connector-java 从 5.1.33 到 5.1.37 版本中的一个错误。我在这里报告过:http: //bugs.mysql.com/bug.php?id=79343

Edited: This has been corrected from mysql-connector-java 5.1.39

编辑:这已从 mysql-connector-java 5.1.39 更正

It was a typo in TimeUtil class in loadTimeZoneMappings method that raises a NPE locating /com/mysql/jdbc/TimeZoneMapping.properties file. If you look at the code, the file should be located within TimeUtil class loader, not TimeZone:

这是 loadTimeZoneMappings 方法中 TimeUtil 类中的一个错字,它引发了 NPE 定位 /com/mysql/jdbc/TimeZoneMapping.properties 文件。如果您查看代码,该文件应该位于 TimeUtil 类加载器中,而不是 TimeZone:

TimeUtil.class.getResourceAsStream(TIME_ZONE_MAPPINGS_RESOURCE);

The parameter useLegacyDatetimeCodeallows to correct the difference between client and server timezones automatically when using dates. So it helps you precissely not having to specify timezones in each part. Althought using serverTimeZoneparameter is a workaround, and meanwhile the patch is released, you can try better correcting the code by yourself as I did.

参数useLegacyDatetimeCode允许在使用日期时自动更正客户端和服务器时区之间的差异。因此,它可以帮助您精确地不必在每个部分中指定时区。虽然使用serverTimeZone参数是一种解决方法,同时补丁已经发布,你可以像我一样尝试更好地自己更正代码。

  • If it's a standalone application, you can try simply to add a corrected com/mysql/jdbc/TimeUtil class to your code and be careful with jar loading order. This can help: https://owenou.com/2010/07/20/patching-with-class-shadowing-and-maven.html

  • If it's a web application, the easier solution is to create your own mysql-connector-java-5.1.37-patched.jar, substituting the .class directly into the original jar.

  • 如果它是一个独立的应用程序,您可以尝试简单地将一个更正的 com/mysql/jdbc/TimeUtil 类添加到您的代码中,并注意 jar 加载顺序。这可以帮助:https: //owenou.com/2010/07/20/patching-with-class-shadowing-and-maven.html

  • 如果是Web应用程序,更简单的解决方案是创建自己的mysql-connector-java-5.1.37-patched.jar,将.class直接替换到原始jar中。

回答by Gabriel Amazonas Mesquita

If you are using Maven, you can just set another MySQL connector version (I had the same error, so i changed from 6.0.2 to 5.1.39) in pom.xml:

如果您使用的是 Maven,则可以在以下位置设置另一个 MySQL 连接器版本(我遇到了同样的错误,所以我从 6.0.2 更改为 5.1.39)pom.xml

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>

As reported in another answers, this issue has been fixed in versions 6.0.3 or above, so you can use the updated version:

如另一个答案中所述,此问题已在 6.0.3 或更高版本中修复,因此您可以使用更新版本:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.3</version>
</dependency>

Maven will automatically re-build your project after you save the pom.xmlfile.

保存pom.xml文件后,Maven 将自动重新构建您的项目。

回答by Alireza Alallah

The connection string should be set like this:

连接字符串应该这样设置:

jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

If you are defining the connection in an xmlfile (such as persistence.xml, standalone-full.xml, etc..), instead of &you should use &amp;or use a CDATAblock.

如果要定义在连接xml文件(如persistence.xmlstandalone-full.xml等..),而不是&你应该使用&amp;或使用CDATA块。

回答by Dmitriy Rud

I've solved this problem by configuring MySQL.

我已经通过配置 MySQL 解决了这个问题。

SET GLOBAL time_zone = '+3:00';

SET GLOBAL time_zone = '+3:00';

回答by Fortran

  1. I added in mysql config file in section [mysqld]

    default_time_zone='+03:00'
    
  2. And restart mysql server:

    sudo service mysql restart
    
  1. 我在[mysqld]部分添加了 mysql 配置文件

    default_time_zone='+03:00'
    
  2. 并重启mysql服务器:

    sudo service mysql restart
    

Where +03:00my UTC time zone.

哪里+03:00我的 UTC 时区。

Path to config file on my os ubuntu 16.04:

我的 os ubuntu 16.04 上配置文件的路径:

/etc/mysql/mysql.conf.d/mysqld.cnf

WARNING: IF YOUR TIME ZONE HAVE SUMMER AND WINTER TIME. YOU MUST CHANGE UTC IN CONFIG IF CHANGE TIME. TWICE IN YEAR(USUALLY) OR SET CRONTAB WITH SUDO.

警告:如果您的时区有夏季和冬季时间。如果更改时间,您必须在配置中更改 UTC。一年两次(通常)或使用 SUDO 设置 CRONTAB。

My url jdbc connection:

我的 url jdbc 连接:

"jdbc:mysql://localhost/java"

回答by Nikunj Shroff

I solved this issue without any single code change. just goto system time setting and set the time zone. In my case the default time zone was UTC which I changed to my local time zone. After I did restart all services, everything worked for me.

我在没有任何单一代码更改的情况下解决了这个问题。只需转到系统时间设置并设置时区。在我的情况下,默认时区是 UTC,我将其更改为本地时区。在我重新启动所有服务后,一切都对我有用。

回答by Rafael Costa Cavalcante

I solved putting below connection string in the URL

我解决了在 URL 中放置以下连接字符串

jdbc:mysql://localhost:3306/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

回答by GordR

I also was having the exact same problem in LibreOffice Base. So I just specified a non 'daylight savings time zone' in the connection string.
**enter image description here**

我在 LibreOffice Base 中也遇到了完全相同的问题。所以我只是在连接字符串中指定了一个非“夏令时区”。
**在此处输入图片描述**

I tried withoutthe "&serverTimezone=MST" but that failed as well.

我试过没有“&serverTimezone=MST”,但也失败了。

I also tried "&serverTimezone=MDT" and that failed, so for some reason, it doesn't like daylight savings time!

我也试过“&serverTimezone=MDT”但失败了,所以出于某种原因,它不喜欢夏令时!

回答by felice de simone

I have the same problem and i solved it append only "?serverTimezone=UTC" to my string connection.

我有同样的问题,我解决了它只将“?serverTimezone = UTC”附加到我的字符串连接。

#

sinossi my problem:

我的问题:

java.sql.SQLException: The server time zone value 'CEST' 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.

java.sql.SQLException: 服务器时区值“CEST”无法识别或表示多个时区。如果您想利用时区支持,您必须将服务器或 JDBC 驱动程序(通过 serverTimezone 配置属性)配置为使用更具体的时区值。

my dbDriver = com.mysql.jdbc.Driver

my jar = mysql-connector-java-8.0.12.jar

my java = 1.8

my tomcat = Apache Tomcat Version 8.5.32

my MySql server = MySql ver.8.0.12