是什么導(dǎo)致了,寫入MySQL庫表時(shí)間不正確?—— 官網(wǎng)也有Bug!
圖片
在實(shí)際的工作場(chǎng)景中有時(shí)候就是一個(gè)小小的問題,就可能引發(fā)出一個(gè)大大的bug。而且工作這么多年,看到的線上事故,往往也都是這些小的細(xì)節(jié)問題,所以學(xué)習(xí)這些具有實(shí)際經(jīng)驗(yàn)的細(xì)節(jié)非常重要。
有些事故隱藏的很深!
其實(shí)很多時(shí)候事故也不是一開始就有的,而是隨著需求的迭代,達(dá)到某一個(gè)條件后觸達(dá)到事故的發(fā)生條件了才出現(xiàn)的。就像 MySQL 的時(shí)區(qū)配置問題,它既有不同版本 JDBC 連接引擎的不同,又有數(shù)據(jù)庫設(shè)置的時(shí)區(qū),還有服務(wù)端設(shè)置的時(shí)區(qū),還包括在使用數(shù)據(jù)庫配置時(shí)指定的時(shí)區(qū)。這些條件綜合發(fā)生時(shí)才會(huì)出現(xiàn)事故。
接下來,小傅哥就給大家分享下為啥是 8.0.22 版本才會(huì)引發(fā)時(shí)區(qū)錯(cuò)誤問題。
一、問題場(chǎng)景
這是一條很普通的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>
接下來在執(zhí)行插入SQL語句;
圖片
- 原本直接使用數(shù)據(jù)庫語句 now() 的并沒有問題,而改為由程序透?jìng)鞯臅r(shí)間 createTime 后,日期時(shí)間發(fā)生了錯(cuò)誤。差了8個(gè)小時(shí)。
- 通常一般我們操作數(shù)據(jù)庫的時(shí)候,寫入的時(shí)間,往往都是 now()。但有時(shí)候比如要外部透?jìng)饔脩粝聠螘r(shí)間做本系統(tǒng)做一個(gè)返利活動(dòng),在什么時(shí)間內(nèi)才返利,要記錄時(shí)間。這個(gè)時(shí)候發(fā)現(xiàn)寫入數(shù)據(jù)庫的時(shí)間就不對(duì)了。
- 因?yàn)樵灸愕南到y(tǒng)都是走的數(shù)據(jù)庫時(shí)間,現(xiàn)在突然多了一個(gè)來自系統(tǒng)的透?jìng)鲿r(shí)間,那么你可能是注意不到的。另外由于本機(jī)的開發(fā)環(huán)境與服務(wù)器配置不一樣,所以最終直至上線開始跑數(shù)據(jù)了,才發(fā)現(xiàn)問題。這個(gè)就是一般出現(xiàn)事故的原因。
二、排查配置
1. mysql jdbc 版本
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
- 8.0.22 版本,官網(wǎng)提示有bug;https://dev.mysql.com/doc/relnotes/connector-j/en/news-8-0-23.html
2. 鏈接參數(shù)配置
jdbc:mysql://127.0.0.1:3306/road-map?useUnicode=true&characterEncoding=utf8&autoRecnotallow=true&zeroDateTimeBehavior=convertToNull&useSSL=true
- 注意首次沒有配置時(shí)區(qū)。配置時(shí)區(qū)需要增加參數(shù);&serverTimeznotallow=Asia/Shanghai
3. mysql time-zone 配置
show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
- 命令修改時(shí)區(qū);SET time_zone = 'SYSTEM';
- 命令修改時(shí)區(qū);SET time_zone = '+8:00';
- 注意CST配置,不是中國(guó)時(shí)區(qū)。默認(rèn)是美國(guó)中部時(shí)間。
美國(guó)中部時(shí)間 Central Standard Time (USA) UTC-05:00 或 UTC-06:00
澳大利亞中部時(shí)間 Central Standard Time (Australia) UTC+09:30
中國(guó)標(biāo)準(zhǔn)時(shí) China Standard Time UTC+08:00
古巴標(biāo)準(zhǔn)時(shí) Cuba Standard Time UTC-04:00
4. linux 服務(wù)器時(shí)間
[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
命令修改時(shí)區(qū);sudo timedatectl set-timezone Asia/Shanghai
命令修改時(shí)區(qū);sudo timedatectl set-timezone America/New_York
三、源碼問題 - MySQL JDBC
1. 8.0.22 版本問題
在 8.0.0 ~ 8.0.22 版本中,如果未配置時(shí)區(qū),serverTimeznotallow=Asia/Shanghai 則會(huì)取服務(wù)端時(shí)區(qū),所以如果服務(wù)端配置的是 CST 時(shí)區(qū),則會(huì)有問題。調(diào)試源碼;
com.mysql.cj.protocol.a.NativeProtocol#configureTimezone
圖片
- 在 8.0.22 版本中,獲取時(shí)區(qū)的方法,如果本地為配置 jdbc 時(shí)區(qū),則會(huì)獲取服務(wù)端時(shí)區(qū)。也就是 CST 美國(guó)中部時(shí)間。
- 所以,如果你要使用的是 8.0.22 就必須指定時(shí)區(qū)。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 版本以后,如果未配置時(shí)區(qū),調(diào)整為獲取客戶端時(shí)區(qū)。
<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 虛擬機(jī)默認(rèn)時(shí)區(qū)。這個(gè)方法也是 Java 本身代碼的方法。
- 你可以通過 Java Main 函數(shù)執(zhí)行 System.*out*.println("Default Time Zone: " + TimeZone.getDefault().getID()); 獲取默認(rèn)時(shí)區(qū)。打印結(jié)果為 Default Time Zone: Asia/Shanghai
3. 官網(wǎng)說明
地址: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 升級(jí)到 8.0 后,保存和檢索 DATETIME 和 TIMESTAMP 值的結(jié)果有時(shí)會(huì)有所不同。這是因?yàn)椋m然 Connector/J 5.1 默認(rèn)不保留時(shí)間點(diǎn),但 Connector/J 8.0.22 及更早版本嘗試通過在將時(shí)間戳的值發(fā)送到服務(wù)器之前將其轉(zhuǎn)換為服務(wù)器的會(huì)話時(shí)區(qū)來保留時(shí)間點(diǎn)。在此版本中,引入了用于控制時(shí)區(qū)轉(zhuǎn)換的新機(jī)制 - 有關(guān)詳細(xì)信息,請(qǐng)參閱保留時(shí)間點(diǎn)。在這種新機(jī)制下,通過設(shè)置連接屬性 retainInstants=false 來保留 Connector/J 5.1 在這方面的默認(rèn)行為。(錯(cuò)誤 #30962953、錯(cuò)誤 #98695、錯(cuò)誤 #30573281、錯(cuò)誤 #95644)
四、綜上總結(jié)
在使用MySQL的時(shí)候,確保服務(wù)器時(shí)區(qū)、MySQL時(shí)區(qū)、Java應(yīng)用鏈接MySQL JDBC的參數(shù)配置,都指定到具體的時(shí)區(qū)上。MySQL JDBC 使用 8.0.23+ 版本,不要使用 8.0.0 ~ 8.0.22 版本,尤其是5.1升級(jí)要升級(jí)到 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