mysql-connector-java-8.0.12:'Malay Peninsula Standard Time' 服务器时区出错

mysql-connector-java-8.0.12 : getting error for 'Malay Peninsula Standard Time' server time zone

我们最近将 MySQL 从 5.6 升级到 8。因此,根据升级说明,使用 mysql-java-connector(8.0.12) 和新驱动程序 class 名称,即数据库 url 中的 com.mysql.cj.jdbc.Driver。但仅针对此时区出现以下错误 'Malay Peninsula Standard Time'

java.sql.SQLException: Cannot create PoolableConnectionFactory (The server time zone value 'Malay Peninsula Standard Time' 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.) Related cause: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dynamicEntityManagerFactory': FactoryBean which is currently in creation returned null from getObject at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378) ............... .............. Caused by: java.sql.SQLException: The server time zone value 'Malay Peninsula Standard Time' 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. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)

使用以下配置在 tomcat context.xml

中创建连接
<Resource auth="Container" driverClassName="com.mysql.cj.jdbc.Driver" factory="com.grc.common.EncryptedDataSourceFactory" maxActive="100" maxIdle="30" maxWait="10000" minEvictableIdleTimeMillis="30000" minIdle="10" name="jdbc/TestDB" password="0df9f21ee02a561fa39f18f5c7581061" removeAbandoned="true" removeAbandonedTimeout="180" testOnBorrow="true" testOnReturn="false" testWhileIdle="true" timeBetweenEvictionRunsMillis="30000" type="javax.sql.DataSource" url="jdbc:mysql://127.0.0.1:3306/production1?useSSL=false" username="root" validationInterval="30000" validationQuery="SELECT 1"/>

此外,尝试使用如下所示的 serverTimezone 参数,但没有成功。

<Resource auth="Container" driverClassName="com.mysql.cj.jdbc.Driver" factory="com.grc.common.EncryptedDataSourceFactory" maxActive="100" maxIdle="30" maxWait="10000" minEvictableIdleTimeMillis="30000" minIdle="10" name="jdbc/TestDB" password="0df9f21ee02a561fa39f18f5c7581061" removeAbandoned="true" removeAbandonedTimeout="180" testOnBorrow="true" testOnReturn="false" testWhileIdle="true" timeBetweenEvictionRunsMillis="30000" type="javax.sql.DataSource" url="jdbc:mysql://127.0.0.1:3306/production1?useSSL=false&amp;serverTimezone=Malay Peninsula Standard Time" username="root" validationInterval="30000" validationQuery="SELECT 1"/>

我在 OS 级别没有更改任何注册表的情况下解决了自己的问题。这可能会对其他人有所帮助。

出现此错误是因为 'Malay Peninsula Standard Time' 的时区名称在 MySQL 中不可用。因此,按照此 MySQL Documenation, downloaded timezone_2018e_posix_sql.zip from this MySQL Timezones 中的说明并按照说明加载到 MySQL 数据库中。之后在 my.ini 文件中添加以下配置并重新启动服务器。

default_time_zone=Asia/Singapore