是什么導致了,寫入MySQL庫表時間不正確?—— 官網也有Bug!
圖片
在實際的工作場景中有時候就是一個小小的問題,就可能引發出一個大大的bug。而且工作這么多年,看到的線上事故,往往也都是這些小的細節問題,所以學習這些具有實際經驗的細節非常重要。
有些事故隱藏的很深!
其實很多時候事故也不是一開始就有的,而是隨著需求的迭代,達到某一個條件后觸達到事故的發生條件了才出現的。就像 MySQL 的時區配置問題,它既有不同版本 JDBC 連接引擎的不同,又有數據庫設置的時區,還有服務端設置的時區,還包括在使用數據庫配置時指定的時區。這些條件綜合發生時才會出現事故。
接下來,小傅哥就給大家分享下為啥是 8.0.22 版本才會引發時區錯誤問題。
一、問題場景
這是一條很普通的SQL語句;
<insert id="insert" parameterType="cn.bugstack.xfg.dev.tech.infrastructure.po.EmployeePO">
INSERT INTO employee(employee_number, employee_name, employee_level, employee_title, create_time, update_time)
VALUES(#{employeeNumber}, #{employeeName}, #{employeeLevel}, #{employeeTitle}, now(), now())
</insert>修改下這條普通的SQL語句;
<insert id="insert" parameterType="cn.bugstack.xfg.dev.tech.infrastructure.po.EmployeePO">
INSERT INTO employee(employee_number, employee_name, employee_level, employee_title, create_time, update_time)
VALUES(#{employeeNumber}, #{employeeName}, #{employeeLevel}, #{employeeTitle}, #{createTime}, now())
</insert>接下來在執行插入SQL語句;
圖片
- 原本直接使用數據庫語句 now() 的并沒有問題,而改為由程序透傳的時間 createTime 后,日期時間發生了錯誤。差了8個小時。
- 通常一般我們操作數據庫的時候,寫入的時間,往往都是 now()。但有時候比如要外部透傳用戶下單時間做本系統做一個返利活動,在什么時間內才返利,要記錄時間。這個時候發現寫入數據庫的時間就不對了。
- 因為原本你的系統都是走的數據庫時間,現在突然多了一個來自系統的透傳時間,那么你可能是注意不到的。另外由于本機的開發環境與服務器配置不一樣,所以最終直至上線開始跑數據了,才發現問題。這個就是一般出現事故的原因。
二、排查配置
1. mysql jdbc 版本
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>- 8.0.22 版本,官網提示有bug;https://dev.mysql.com/doc/relnotes/connector-j/en/news-8-0-23.html
2. 鏈接參數配置
jdbc:mysql://127.0.0.1:3306/road-map?useUnicode=true&characterEncoding=utf8&autoRecnotallow=true&zeroDateTimeBehavior=convertToNull&useSSL=true- 注意首次沒有配置時區。配置時區需要增加參數;&serverTimeznotallow=Asia/Shanghai
3. mysql time-zone 配置
show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+- 命令修改時區;SET time_zone = 'SYSTEM';
- 命令修改時區;SET time_zone = '+8:00';
- 注意CST配置,不是中國時區。默認是美國中部時間。
美國中部時間 Central Standard Time (USA) UTC-05:00 或 UTC-06:00
澳大利亞中部時間 Central Standard Time (Australia) UTC+09:30
中國標準時 China Standard Time UTC+08:00
古巴標準時 Cuba Standard Time UTC-04:00
4. linux 服務器時間
[root@lavm-aqhgp9nber ~]# timedatectl
Local time: Sat 2024-08-31 13:57:07 CST
Universal time: Sat 2024-08-31 05:57:07 UTC
RTC time: Sat 2024-08-31 05:57:06
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a命令修改時區;sudo timedatectl set-timezone Asia/Shanghai
命令修改時區;sudo timedatectl set-timezone America/New_York
三、源碼問題 - MySQL JDBC
1. 8.0.22 版本問題
在 8.0.0 ~ 8.0.22 版本中,如果未配置時區,serverTimeznotallow=Asia/Shanghai 則會取服務端時區,所以如果服務端配置的是 CST 時區,則會有問題。調試源碼;
com.mysql.cj.protocol.a.NativeProtocol#configureTimezone
圖片
- 在 8.0.22 版本中,獲取時區的方法,如果本地為配置 jdbc 時區,則會獲取服務端時區。也就是 CST 美國中部時間。
- 所以,如果你要使用的是 8.0.22 就必須指定時區。jdbc:mysql://IP:13306/road-map?useUnicode=true&characterEncoding=utf8&autoRecnotallow=true&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimeznotallow=Asia/Shanghai
2. 8.0.23 + 版本
在 8.0.23 版本以后,如果未配置時區,調整為獲取客戶端時區。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>- 切換到 8.0.23 版本。
com.mysql.cj.protocol.a.NativeProtocol#configureTimezone
圖片
- 在使用 8.0.23 TimeZone.getDefault() 注釋 Gets the default TimeZone of the Java virtual machine. 獲取 Java 虛擬機默認時區。這個方法也是 Java 本身代碼的方法。
- 你可以通過 Java Main 函數執行 System.*out*.println("Default Time Zone: " + TimeZone.getDefault().getID()); 獲取默認時區。打印結果為 Default Time Zone: Asia/Shanghai
3. 官網說明
地址:https://dev.mysql.com/doc/relnotes/connector-j/en/news-8-0-23.html
Bugs Fixed
After upgrading from Connector/J 5.1 to 8.0, the results of saving and then retrieving DATETIME and TIMESTAMP values became different sometimes. It was because while Connector/J 5.1 does not preserve a time instant by default, Connector/J 8.0.22 and earlier tried to do so by converting a timestamp to the server's session time zone before sending its value to the server. In this release, new mechanisms for controlling timezone conversion has been introduced—see Preserving Time Instants for details. Under this new mechanism, the default behavior of Connector/J 5.1 in this respect is preserved by setting the connection property preserveInstants=false. (Bug #30962953, Bug #98695, Bug #30573281, Bug #95644)從 Connector/J 5.1 升級到 8.0 后,保存和檢索 DATETIME 和 TIMESTAMP 值的結果有時會有所不同。這是因為,雖然 Connector/J 5.1 默認不保留時間點,但 Connector/J 8.0.22 及更早版本嘗試通過在將時間戳的值發送到服務器之前將其轉換為服務器的會話時區來保留時間點。在此版本中,引入了用于控制時區轉換的新機制 - 有關詳細信息,請參閱保留時間點。在這種新機制下,通過設置連接屬性 retainInstants=false 來保留 Connector/J 5.1 在這方面的默認行為。(錯誤 #30962953、錯誤 #98695、錯誤 #30573281、錯誤 #95644)
四、綜上總結
在使用MySQL的時候,確保服務器時區、MySQL時區、Java應用鏈接MySQL JDBC的參數配置,都指定到具體的時區上。MySQL JDBC 使用 8.0.23+ 版本,不要使用 8.0.0 ~ 8.0.22 版本,尤其是5.1升級要升級到 8.0.23 以及往后的版本。
正確配置;url: jdbc:mysql://127.0.0.1:3306/road-map?useUnicode=true&characterEncoding=utf8&autoRecnotallow=true&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimeznotallow=Asia/Shanghai


























