跳至內容

如何使用 AWS DMS 任務遷移非 UTC 時區的 MySQL 資料庫?

3 分的閱讀內容
0

我有一個位於非 UTC 時區的來源和目標 MySQL 執行個體。我想要使​​用 AWS Database Migration Service (AWS DMS) 任務來遷移資料庫。

簡短描述

如果您的來源 MySQL 執行個體和目標 MySQL 執行個體使用的是非 UTC 時區,則您的時間戳記資料可能會不一致。在內部,MySQL 會將時間戳記欄儲存為 UTC。但是,當您選擇日期時,MySQL 會自動將時間戳記欄轉換為目前工作階段的時區。

例如,您有一個來源和目標 MySQL 資料庫是以美國/太平洋地區執行。當 DMS 任務擷取資料並將其作為來源中的 UTC 套用到目標時,資料會變得不一致。

若要解決此問題,請使用來源端點中的 ServerTimezone 端點設定。將值設定為 MySQL 資料庫的時區。

ServerTimezone 端點設定範例:

ServerTimezone=US/Pacific

解決方法

在來源端點沒有設定 ServerTimezone 的情況下進行遷移

下列範例來源資料庫未在來源端點中包含 ServerTimezone 設定:

mysql>  SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| US/Pacific         | US/Pacific          |
+--------------------+---------------------+

mysql> create  table test_tz ( id int primary key,t_date timestamp);
Query OK, 0 rows affected (0.28 sec)

mysql> insert into test_tz values (10, now());
Query OK, 1 row affected (0.31 sec)

mysql> select * from test_tz;
+----+---------------------+
| id | t_date              |
+----+---------------------+
| 10 | 2022-06-27 20:50:29 |
+----+---------------------+
1 row in set (0.25 sec)

以下是建立 DMS 任務、遷移資料且任務完全載入資料後的來源資料庫範例:

mysql>  SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| US/Pacific         | US/Pacific          |
+--------------------+---------------------+
1 row in set (0.30 sec)

mysql> select * from test_tz;
+----+---------------------+
| id | t_date              |
+----+---------------------+
| 10 | 2022-06-28 03:50:29 |    
+----+---------------------+
1 row in set (0.25 sec)

時間戳記採用 UTC 時間。

在來源資料庫中執行插入:

mysql>  insert into test_tz values (11, now());Query OK, 1 row affected (0.38 sec)

mysql> select * from test_tz;
+----+---------------------+
| id | t_date              |
+----+---------------------+
| 10 | 2022-06-27 20:50:29 |
| 11 | 2022-06-27 21:10:13 |
+----+---------------------+
2 rows in set (0.24 sec)

目標範例:

mysql> select * from test_tz;
+----+---------------------+
| id | t_date              |
+----+---------------------+    
| 10 | 2022-06-28 03:50:29 |
| 11 | 2022-06-28 04:10:13 |    
+----+---------------------+
2 rows in set (0.25 sec)

時間戳記仍為 UTC。

使用來源端點中的 ServerTimezone 設定進行遷移

以下範例是當您在來源端點中使用 ServerTimezone 設定時,完全載入的來源和目標。

來源範例:

mysql> select * from test_tz;
+----+---------------------+
| id | t_date              |
+----+---------------------+
| 10 | 2022-06-27 20:50:29 |
| 11 | 2022-06-27 21:10:13 |
+----+---------------------+

目標範例:

mysql> select * from test_tz;
+----+---------------------+
| id | t_date              |
+----+---------------------+
| 10 | 2022-06-27 20:50:29 |
| 11 | 2022-06-27 21:10:13 |
+----+---------------------+

CDC 期間的資料

當您為使用非 UTC 時區的來源 MySQL 執行個體複製時間戳記資料時,請在變更資料擷取 (CDC) 期間使用 ServerTimezone 設定。

來源範例:

mysql> insert into test_tz values (12, current_time());
Query OK, 1 row affected (0.38 sec)
mysql>
mysql>
mysql> select * from test_tz;
+----+---------------------+
| id | t_date |
+----+---------------------+
| 10 | 2022-06-27 20:50:29 |
| 11 | 2022-06-27 21:10:13 |
| 12 | 2022-06-27 21:12:06 |
+----+---------------------+
3 rows in set (0.25 sec)

目標範例:

mysql> select * from test_tz;
+----+---------------------+
| id | t_date |
+----+---------------------+
| 10 | 2022-06-27 20:50:29 |
| 11 | 2022-06-27 21:10:13 |
| 12 | 2022-06-27 21:12:06 |
+----+---------------------+
3 rows in set (0.25 sec)

相關資訊

MySQL 網站上的 DATE、DATETIME 和 TIMESTAMP 類型

AWS 官方已更新 10 個月前