背景知识:

我有一个运行在Tomcat 7上的Java 1.6 web应用程序。数据库为MySQL 5.5。之前,我使用Mysql JDBC驱动程序5.1.23连接到DB。一切工作。我最近升级到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.

为什么会这样?


当前回答

连接字符串为我工作这个

jdbc: mysql: / / localhost / < yourDbName > ?useUnicode = true&useJDBCCompliantTimezoneShift = true&useLegacyDatetimeCode = false&serverTimezone = UTC

我使用Gradle我的构建。Gradle文件是这样的

dependencies {
    // https://mvnrepository.com/artifact/mysql/mysql-connector-java
    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.17'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

其他回答

在应用程序内按位置为spring引导应用程序设置时区。属性文件

spring.datasource.url=jdbc:mysql://localhost:3306/db?serverTimezone=Europe/Berlin

解决了CET / CEST时区的问题。xml使用maven工件

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.22</version>
    </dependency>

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

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

如果你在xml文件(如persistence.xml, standalone-full.xml等)中定义连接,你应该使用&或者使用CDATA块。

同意@bluecollarcoder的答案,但最好使用TimeZone.getDefault().getID();在连接字符串的末尾:

"jdbc:mysql://localhost/db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=" + TimeZone.getDefault().getID();  

在这种情况下,Timezone参数根据本地机器时区自动更新。

在mysql工作台中运行以下sql语句:

设置@@global。Time_zone = '+00:00'; 设置@@session。Time_zone = '+00:00';

使用以下SQL语句检查是否设置了值:

选择@@global。time_zone @@session.time_zone;

my.ini

最后加上这一行:

default-time-zone = '+05:30'

从终端运行此命令

>> sudo mysql -e "SET GLOBAL time_zone = ‘+5:30’;" -u root
>> sudo mysql -e "SELECT @@global.time_zone;" -u root

如果两者都不工作,请尝试使用终端从sqoop使用此请求

>> sqoop list-databases --connect "jdbc:mysql://localhost/employees?serverTimezone=UTC" --username sqoop -P

或者你可以用这个替换你的请求URL

jdbc:mysql://localhost/employees?serverTimezone=UTC