如何使用 AWS DMS 任务迁移非 UTC 时区的 MySQL 数据库?

3 分钟阅读
0

我有一个位于非 UTC 时区的源和目标 MySQL 实例。我想使用 AWS Database Migration Service (AWS DMS) 任务迁移数据库。

简短描述

如果您的源和目标 MySQL 实例使用非 UTC 时区,则您的时间戳数据可能会不一致。在内部,MySQL 将时间戳列存储为 UTC。但是,当您选择日期时,MySQL 会自动将时间戳列转换为当前会话的时区。

例如,您有一个以美国/太平洋时区运行的源和目标 MySQL 数据库。当 AWS 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 网站上的 The DATE, DATETIME, and TIMESTAMP types

AWS 官方
AWS 官方已更新 1 个月前