Daylight Saving Time and System Time Zone in MySQL

栏目: IT技术 · 发布时间: 5年前

内容简介:You may end up with a situation where some of your cluster nodes still use the time zone before the switch (e.g. EST) and others use the timezone after the change (e.g. EDT).

Daylight Saving Time and System Time Zone in MySQL March is not only the month when a pandemic closed all borders and people had to stay home on the quarantine; it’s also the month when daylight saving time change happens. For some regions, this is not only a time change but also a switch to a different timezone. For example, New York uses EST during winter and EDT during summer. If you use the system timezone and do not restart the MySQL server or a PXC node after the switch, you may notice that the change was not implemented.

You may end up with a situation where some of your cluster nodes still use the time zone before the switch (e.g. EST) and others use the timezone after the change (e.g. EDT).

$ date
Sun Mar  8 03:03:28 EDT 2020
 
$ ./bin/mysql  -h127.0.0.1 -P3373 -uroot test
...
EDT node> show variables like '%zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | EDT    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)
 
$ ./bin/mysql  -h127.0.0.1 -P3372 -uroot test
...
EST node> show variables like '%zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | EST    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)


Should you worry about it?

Nope!

MySQL initiates the system_time_zone variable when it is started. Even if the variable contains stalled data, all calculations are performed correctly and temporal values already use the new timezone.

To demonstrate this, let’s look at a simple table holding timestamp values:

CREATE TABLE `t1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB

First, let’s check existing data; it is the same no matter which value of the system_time_zone variable uses the node:

EDT node> select * from t1;
+----+---------------------+
| id | ts                  |
+----+---------------------+
|  2 | 2020-03-08 01:03:53 |
|  4 | 2020-03-08 01:03:54 |
|  6 | 2020-03-08 01:03:55 |
+----+---------------------+
3 rows in set (0.00 sec)
 
EST node> select * from t1;
+----+---------------------+
| id | ts                  |
+----+---------------------+
|  2 | 2020-03-08 01:03:53 |
|  4 | 2020-03-08 01:03:54 |
|  6 | 2020-03-08 01:03:55 |
+----+---------------------+
3 rows in set (0.00 sec)

If we add a new row, the valid timestamp will be inserted on both nodes:

EST node> insert into t1 values();
Query OK, 1 row affected (0.01 sec)
 
EST node> select * from t1;
+----+---------------------+
| id | ts                  |
+----+---------------------+
|  2 | 2020-03-08 01:03:53 |
|  4 | 2020-03-08 01:03:54 |
|  6 | 2020-03-08 01:03:55 |
|  8 | 2020-03-08 03:02:22 |
+----+---------------------+
4 rows in set (0.00 sec)
 
EDT node> select * from t1;
+----+---------------------+
| id | ts                  |
+----+---------------------+
|  2 | 2020-03-08 01:03:53 |
|  4 | 2020-03-08 01:03:54 |
|  6 | 2020-03-08 01:03:55 |
|  8 | 2020-03-08 03:02:22 |
+----+---------------------+
4 rows in set (0.00 sec)

As you can see, the row, inserted on the node, started before the timezone change, has the same value on both nodes.

The same happens if we insert a new row on the node, started after the time change:

EDT node> insert into t1 values();
Query OK, 1 row affected (0.01 sec)
 
EDT node> select * from t1;
+----+---------------------+
| id | ts                  |
+----+---------------------+
|  2 | 2020-03-08 01:03:53 |
|  4 | 2020-03-08 01:03:54 |
|  6 | 2020-03-08 01:03:55 |
|  8 | 2020-03-08 03:02:22 |
|  9 | 2020-03-08 03:02:32 |
+----+---------------------+
5 rows in set (0.00 sec)
 
EST node> select * from t1;
+----+---------------------+
| id | ts                  |
+----+---------------------+
|  2 | 2020-03-08 01:03:53 |
|  4 | 2020-03-08 01:03:54 |
|  6 | 2020-03-08 01:03:55 |
|  8 | 2020-03-08 03:02:22 |
|  9 | 2020-03-08 03:02:32 |
+----+---------------------+
5 rows in set (0.00 sec)

Conclusion

If you use time_zone = SYSTEM , you may notice the value of the system_time_zone variable is outdated after the daylight saving time changes happen. But you should not worry about it, because all the calculations will use the updated time.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

网站分析实战

网站分析实战

王彦平 吴盛峰 / 电子工业出版社 / 2013-1 / 59.00元

《网站分析实战:如何以数据驱动决策,提升网站价值》由王彦平、吴盛峰著。目前,越来越多的网站开始重视数据,并期望从中发现新的机会,不管你是做网络营销、互联网产品设计、电子商务运营、个人站点运营维护,我们都希望从数据中寻找有价值的结论,并且指导公司管理层的决策,最终创造更大的网站价值。《网站分析实战:如何以数据驱动决策,提升网站价值》以通俗易懂的方式来讲解网站分析所需掌握的知识,剖析日常工作中遇到的问......一起来看看 《网站分析实战》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具