当前位置:网站首页>Explain in detail the pitfalls encountered in DTS due to the time zone problems of timestamp and datetime in MySQL
Explain in detail the pitfalls encountered in DTS due to the time zone problems of timestamp and datetime in MySQL
2022-04-23 11:06:00 【liming89】
Source of the article : Learn through http://www.bdgxy.com/
MySQL How to represent the current time in ?
Actually , There are many ways of expression , The summary is as follows :
Data Type | “Zero” Value |
---|---|
DATE |
'0000-00-00' |
TIME |
'00:00:00' |
DATETIME |
'0000-00-00 00:00:00' |
TIMESTAMP |
'0000-00-00 00:00:00' |
YEAR |
0000 |
datetime and timestamp Both types are used to represent YYYY-MM-DD HH:MM:SS This kind of data in the format of year, month, day, hour, minute and second , But there are some differences between the two .
Conclusion
- timestamp What's actually stored is 1970-01-01 00:00:00 UTC The number of seconds so far accounts for 4 byte ( When the time precision is milliseconds and nanoseconds, it will occupy more bytes ), Therefore, it is equivalent to the time with time zone , By setting the time zone of the session , It will be automatically converted to the set time zone
- datetime Stored is the formatted string, similar to '2021-12-05 13:27:53.957033', Do not carry time zone information , stay UTC and CST The results of time zone query are consistent , For example, in CST Time zone write '2021-12-05 13:27:53.957033', But in UTC The time zone query still finds '2021-12-05 13:27:53.957033', If there is no time zone conversion , It's equivalent to directly CST Time mapped to UTC Time , But actually UTC Time ratio CST Time is slow 8 Hours
verification
Environmental preparation , In short, there is a table with timestamp Fields and datetime Field , And the current server is CST The time zone
mysql> show create table test_time\G;
*************************** 1. row ***************************
Table: test_time
Create Table: CREATE TABLE `test_time` (
`id` int NOT NULL AUTO_INCREMENT,
`ts` timestamp(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
`dt` datetime(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
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.01 sec)
Insert a piece of data , At present CST In time zone ts and dt The result is the same
mysql> select * from test_time;
Empty set (0.00 sec)
mysql> insert into test_time() values();
Query OK, 1 row affected (0.00 sec)
mysql> select * from test_time;
±—±---------------------------±---------------------------+
| id | ts | dt |
±—±---------------------------±---------------------------+
| 3 | 2021-12-05 15:04:13.293949 | 2021-12-05 15:04:13.293949 |
±—±---------------------------±---------------------------+
1 row in set (0.00 sec)
Set the time zone of the session to UTC Check the time zone again ,ts As a result of CST Time zone changed to UTC The result of time zone query is slower than before 8 Hours , because dt No time zone information , The result remains unchanged.
mysql> set time_zone='+00:00';
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test_time;
±—±---------------------------±---------------------------+
| id | ts | dt |
±—±---------------------------±---------------------------+
| 3 | 2021-12-05 07:04:13.293949 | 2021-12-05 15:04:13.293949 |
±—±---------------------------±---------------------------+
1 row in set (0.01 sec)
From just now on insert Produced binlog There is also a reflection of ,ts stay binlog Stored as timestamp in ( from 1970-01-01 00:00:00 UTC The number of seconds so far ) It's equivalent to taking UTC Time zone information ,dt No time zone information , The result is a formatted string 2021-12-05 15:04:13.293949, Focus on the penultimate 4 The first 5 That's ok ,@2=1638687853.293949 Express ts Value of field , @3='2021-12-05 15:04:13.293949' Express dt Value of field
[mysql %] mysqlbinlog -v --base64-output=decode-rows ./mysqlbin.000012
... ...
SET @@SESSION.GTID_NEXT= '1cf4493a-dafd-11eb-944c-4016af29c14c:1416767'/*!*/;
# at 14220
#211205 15:04:13 server id 1 end_log_pos 14308 CRC32 0x1fd913a3 Query thread_id=137 exec_time=0 error_code=0
SET TIMESTAMP=1638687853.293949/*!*/;
BEGIN
/*!*/;
# at 14308
#211205 15:04:13 server id 1 end_log_pos 14368 CRC32 0xbb8937fb Table_map: `testa`.`test_time` mapped to number 121
# at 14368
#211205 15:04:13 server id 1 end_log_pos 14423 CRC32 0x2e0a3baa Write_rows: table id 121 flags: STMT_END_F
### INSERT INTO `testa`.`test_time`
### SET
### @1=3
### @2=1638687853.293949
### @3='2021-12-05 15:04:13.293949'
# at 14423
#211205 15:04:13 server id 1 end_log_pos 14454 CRC32 0x68cee280 Xid = 1416
COMMIT/*!*/;
pit
- If you're doing DTS When related projects , Use parsing MySQL binlog Open source tools for , for example github.com/go-mysql-org/go-mysql, If the parseTime=true Will timestamp The type field resolves to Local Time , take datetime Type resolution as UTC Time , It can also be configured as false What you get is a string (timestamp Time converted to session time zone ,datetime Namely binlog The original string ) Self analysis , If parseTime=true And not used UTC Time inserted datetime Field , In theory, the time is not correct , Equivalent to directly CST Of 2021-12-05 15:04:13.293949 Convert to UTC Of 2021-12-05 15:04:13.293949, In fact, it should be converted to UTC Of 2021-12-05 07:04:13.293949 That's right.
- If you happen to have time.Now() Acquired Local Time and datetime Type field comparison scenario , Pay attention to the time zone , Or remove all the time from the time zone , Convert to a formatted string and compare
- because datetime There is no time zone information in itself , Except for conversion UTC Time , There is no better choice , So it's very boring !
Here is a detailed explanation of MySQL in timestamp and datetime Time zone problems lead to DTS The article on the pit encountered is introduced here , More about MySQL timestamp and datetime Please search for the content of the tutorial www.piaodoo.com Previous articles or continue to browse the relevant articles below. I hope you can support rookie tutorials in the future www.piaodoo.com!
版权声明
本文为[liming89]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231102124670.html
边栏推荐
- Visual solutions to common problems (VIII) mathematical formulas
- An interesting interview question
- Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
- Using El popconfirm and El backtop does not take effect
- Visualization Road (11) detailed explanation of Matplotlib color
- Chapter 1 of technical Xiaobai (express yourself)
- Is the pointer symbol of C language close to variable type or variable name?
- The songbird document editor will be open source: starting with but not limited to markdown
- Special members and magic methods
- MySQL对数据表已有表进行分区表的实现
猜你喜欢
关于JUC三大常用辅助类
Go interface usage
26. 删除有序数组中的重复项
Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
Mysql8.0安装指南
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
Cygwin 中的 rename 用法
More reliable model art than deep learning
升级cpolar内网穿透能获得的功能
随机推荐
软件测试人员,如何优秀的提Bug?
web三大组件(Servlet,Filter,Listener)
Excel · VBA array bubble sorting function
VM set up static virtual machine
@valid,@Validated 的学习笔记
我的创作纪念日
详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
闹钟场景识别
Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)
Manjaro installation and configuration (vscode, wechat, beautification, input method)
学习 Go 语言 0x07:《Go 语言之旅》中 Stringer 练习题代码
Data analysis learning (I) data analysis and numpy Foundation
主流手机分辨率与尺寸
妊娠箱和分娩箱的区别
MIT:用无监督为世界上每个像素都打上标签!人类:再也不用为1小时视频花800个小时了
Common parameters of ffmpeg command line
第六站神京门户-------手机号码的转换
CUMCM 2021-B:乙醇偶合制备C4烯烃(2)
Mba-day5 Mathematics - application problems - engineering problems
How to bind a process to a specified CPU