1.查询系统支持当前的时区设置:
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.12 |
+-----------+
1 row in set (0.00 sec)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)
2.修改时区设置:
方法1:写入到配置文件需要重启mysql实例:
[mysqld]
default-time-zone=timezone
修改为
default-time-zone = '+8:00'
方法2:在线修改和查看:
mysql> set time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | +08:00 |
+------------------+--------+
2 rows in set (0.01 sec)
--通过now() 查看:
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | +08:00 |
+------------------+--------+
2 rows in set (0.01 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2018-08-28 16:08:38 |
+---------------------+
1 row in set (0.00 sec)
mysql> set time_zone='+0:00';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | +00:00 |
+------------------+--------+
2 rows in set (0.01 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2018-08-28 08:09:12 |
+---------------------+
1 row in set (0.00 sec)
--自MySQL 8.0 GA版本开始支持将参数写入并且持久化:
mysql> set persist time_zone='+0:00';
--针对application 可以设置:
jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&autoReconnect=true&serverTimezone=UTC
--结论:
mysql> set time_zone='+8:00'; 和time_zone='system'是等同的,默认为系统时间。