使用的是 MySQL 5.7

在使用 SSM 框架时,连接数据库出现错误

The server time zone value ‘xxxxx’(这里乱码了) 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

按照 MySQL 文档中描述,服务器默认使用操作系统的时区,这里出现乱码可能因为我的系统是使用 GBK 编码的原因,然后 MySQL 无法正确识别。不管如何,明确指定时区总是好的。
我需要配置 server 或 JDBC 驱动的 tome zone。

1. server

先查看下 server 的 time zone

这里写图片描述

设置 time_zone 的几种方式

  • 使用命令

mysql> SET GLOBAL time_zone = timezone;

或者

mysql> SET time_zone = timezone;

区别是前者是全局设置,针对所有连接,后者只针对当前设置的客户端。
命令行的设置当 MySQL 服务重启的时候就会失效。

或者在启动服务器时增加选项 --default-time-zone=timezone

  • 使用配置文件

需要重启服务器都能使用设置好的 time zone,则可以在配置文件中,my.ini 或 my.cnf 中的 [mysqld] 部分增加 default-time-zone='timezone'

关于 time zone 的格式

  • 'SYSTEM' 表示 server 的 time zone 跟随系统
  • 使用 UTC 偏移量,如 '+10:00''-6:00'
  • 使用时区名,如 'Europe/Helsinki', 'US/Eastern', 'MET' 等,注意只有在 MySQL 中有时区信息表并且有相应信息数据的时候才能使用时区名

参考官方文档

2. JDBC driver

在 url 中增加 serverTimezone 属性,如

jdbc:mysql://localhost:3306/demo?serverTimezone=Hongkong

Logo

更多推荐