Aurora MySQLのスナップショットを使用してTiDB Serverlessにデータを移行してみた

2024.03.07

ゲームソリューション部の えがわ です。

Aurora MySQLのスナップショットを使用してTiDB Serverlessにデータを移行してみました。

AWS DMSを使用する場合はこちら

環境

  • TiDB Serverless (v7.1.1)
  • EC2(Amazon Linux 2023) 踏み台として使用します。
  • Aurora MySQL(8.0.mysql_aurora.3.04.1)

注意点

以下の説明は本筋ではないため割愛しています。

  • IAMロールやKMS
  • スキーマやスナップショット保存用のバケットの作成

事前準備

Aurora MySQL(移行元)のデータ

移行元のAuroraには以下のデータを追加しておきます。

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    product_name VARCHAR(255) NOT NULL,
    quantity INT NOT NULL,
    order_date DATE NOT NULL,
    FOREIGN KEY (user_id) REFERENCES users(id)
);
INSERT INTO users (name, email) VALUES
('Alice', 'alice@example.com'),
('Bob', 'bob@example.com'),
('Charlie', 'charlie@example.com'),
('David', 'david@example.com'),
('Eve', 'eve@example.com'),
('Frank', 'frank@example.com'),
('Grace', 'grace@example.com'),
('Hannah', 'hannah@example.com'),
('Ivy', 'ivy@example.com'),
('Jack', 'jack@example.com');

INSERT INTO orders (user_id, product_name, quantity, order_date) VALUES
(1, 'Laptop', 1, '2023-01-15'),
(2, 'Mouse', 2, '2023-01-16'),
(1, 'Keyboard', 1, '2023-01-17'),
(3, 'Monitor', 1, '2023-01-18'),
(4, 'USB Cable', 3, '2023-01-19'),
(3, 'Webcam', 1, '2023-01-20'),
(5, 'Headphones', 1, '2023-01-21'),
(6, 'Desk Lamp', 1, '2023-01-22'),
(7, 'Notebook', 5, '2023-01-23'),
(8, 'Pen', 10, '2023-01-24');

文字セットの照合順序の統一(必要な方のみ)

TiDB Serverless(v7.1.1)のデフォルトの照合順序はutf8mb4_general_ciとなっています。
v7.4utf8mb4_0900_ai_ciに変更されています。

今回はAurora側の照合順序を変更します。

文字セットの照合順序をutf8mb4_general_ciに変更します。

MySQL [tidb_migrate]> ALTER DATABASE tidb_migrate CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
Query OK, 1 row affected (0.005 sec)

各テーブルの照合順序をutf8mb4_general_ciに変更します。
吐き出されたSQLを実行することで変更が可能です。

MySQL [tidb_migrate]> SELECT CONCAT('ALTER TABLE `', table_name, '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;') AS _sql FROM information_schema.TABLES WHERE table_schema = 'tidb_migrate';
+-----------------------------------------------------------------------------------+
| _sql                                                                              |
+-----------------------------------------------------------------------------------+
| ALTER TABLE `orders` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; |
| ALTER TABLE `users` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;  |
+-----------------------------------------------------------------------------------+
2 rows in set (0.001 sec)

tiupの設定

踏み台サーバーでtiupの設定をします。

tiupをインストール

curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
source ~/.bash_profile

DumplingとLightningをインストール

dumplingtidb-lightningをインストールします。

tiup install dumpling tidb-lightning
$ tiup tidb-lightning -V
Starting component tidb-lightning: /home/ssm-user/.tiup/components/tidb-lightning/v7.6.0/tidb-lightning -V
Release Version: v7.6.0
Git Commit Hash: 52794d985ba6325d75a714d4eaa0838d59425eb6
Git Branch: heads/refs/tags/v7.6.0
Go Version: go1.21.5
UTC Build Time: 2024-01-22 14:15:25
Race Enabled: false
$ tiup dumpling -V
Starting component dumpling: /home/ssm-user/.tiup/components/dumpling/v7.6.0/dumpling -V
Release version: v7.6.0
Git commit hash: 52794d985ba6325d75a714d4eaa0838d59425eb6
Git branch:      heads/refs/tags/v7.6.0
Build timestamp: 2024-01-22 02:14:07Z
Go version:      go version go1.21.5 linux/amd64

DDLのエクスポート

Auroraのスナップショットには DDLが含まれていないため、Dumplingを使用してAuroraからスキーマをエクスポートする必要があります。
tiupコマンドでDumplingを実行します。

$ tiup dumpling --host ${host} --port 3306 --user admin --password ${password} --database tidb_migrate --consistency none --no-data --output 's3://${my-bucket}/schema-backup'
Starting component dumpling: /home/ssm-user/.tiup/components/dumpling/v7.6.0/dumpling --host {host} --port 3306 --user admin --password ${password} --database tidb_migrate --consistency none --no-data --output s3://my-bucket/schema-backup
Release version: v7.6.0
Git commit hash: 52794d985ba6325d75a714d4eaa0838d59425eb6
Git branch:      heads/refs/tags/v7.6.0
Build timestamp: 2024-01-22 02:14:07Z
Go version:      go version go1.21.5 linux/amd64

中略...

[2024/03/06 02:22:26.218 +00:00] [INFO] [main.go:82] ["dump data successfully, dumpling will exit now"]

DDLが指定したS3バケットにエクスポートされました。

吐き出したDDLをTiDBにインポート

インポートを行うにはLightningを使用します。
Lightningを実行するために設定ファイルを作成します。
今回は以下の設定で行います。

tidb-lightning-schema.toml

[tidb]
host = ${host}
port = ${port}
user = ${user}
password = ${password}
tls = "cluster"

[tikv-importer]
backend = "tidb"

[mydumper]
data-source-dir = "s3://${my-bucket}/schema-backup"

[security]
ca-path = "/etc/pki/tls/certs/ca-bundle.crt"

TiDBの接続情報はTiDBのコンソール画面から確認できます。

設定ファイルを指定しLightningを実行します。

$ tiup tidb-lightning -config tidb-lightning-schema.toml
Starting component tidb-lightning: /home/ssm-user/.tiup/components/tidb-lightning/v7.6.0/tidb-lightning -config tidb-lightning-schema.toml

+---+----------------------------------------------+-------------+--------+
| # | CHECK ITEM                                   | TYPE        | PASSED |
+---+----------------------------------------------+-------------+--------+
| 1 | Source data files size is proper             | performance | true   |
+---+----------------------------------------------+-------------+--------+
| 2 | the checkpoints are valid                    | critical    | true   |
+---+----------------------------------------------+-------------+--------+
| 3 | Cluster version check passed                 | critical    | true   |
+---+----------------------------------------------+-------------+--------+
| 4 | Lightning has the correct storage permission | critical    | true   |
+---+----------------------------------------------+-------------+--------+

tidb lightning exit successfully

スキーマのインポートが完了しました。

スナップショットからTiDBに復元

スキーマのインポートと同様にLightningを使用します。
先ほどとは別で設定ファイルを作成します。
data-source-dirにスナップショットの保存先を指定します。

tidb-lightning-data.toml

[tidb]
host = ${host}
port = ${port}
user = ${user}
password = ${password}
tls = "cluster"

[tikv-importer]
backend = "tidb"

[mydumper]
data-source-dir = "s3://${my-bucket}/sql-backup/tidb-migrate-ss/"

[[mydumper.files]]
pattern = '(?i)^(?:[^/]*/)*([a-z0-9_]+)\.([a-z0-9_]+)/(?:[^/]*/)*(?:[a-z0-9\-_.]+\.(parquet))$'
schema = '$1'
table = '$2'
type = '$3'

[security]
ca-path = "/etc/pki/tls/certs/ca-bundle.crt"
$ tiup tidb-lightning -config tidb-lightning-data.toml
+---+----------------------------------------------+-------------+--------+
| # | CHECK ITEM                                   | TYPE        | PASSED |
+---+----------------------------------------------+-------------+--------+
| 1 | Source data files size is proper             | performance | true   |
+---+----------------------------------------------+-------------+--------+
| 2 | the checkpoints are valid                    | critical    | true   |
+---+----------------------------------------------+-------------+--------+
| 3 | Cluster version check passed                 | critical    | true   |
+---+----------------------------------------------+-------------+--------+
| 4 | Lightning has the correct storage permission | critical    | true   |
+---+----------------------------------------------+-------------+--------+

tidb lightning exit successfully

移行が完了したので、データを確認してみます。

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 744567
Server version: 5.7.28-TiDB-Serverless TiDB Server (Apache License 2.0) Community Edition, MySQL 5.7 compatible

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [tidb_migrate]> select * from users;
+----+---------+---------------------+---------------------+
| id | name    | email               | created_at          |
+----+---------+---------------------+---------------------+
|  1 | Alice   | alice@example.com   | 2024-03-06 01:34:15 |
|  2 | Bob     | bob@example.com     | 2024-03-06 01:34:15 |
|  3 | Charlie | charlie@example.com | 2024-03-06 01:34:15 |
|  4 | David   | david@example.com   | 2024-03-06 01:34:15 |
|  5 | Eve     | eve@example.com     | 2024-03-06 01:34:15 |
|  6 | Frank   | frank@example.com   | 2024-03-06 01:34:15 |
|  7 | Grace   | grace@example.com   | 2024-03-06 01:34:15 |
|  8 | Hannah  | hannah@example.com  | 2024-03-06 01:34:15 |
|  9 | Ivy     | ivy@example.com     | 2024-03-06 01:34:15 |
| 10 | Jack    | jack@example.com    | 2024-03-06 01:34:15 |
+----+---------+---------------------+---------------------+
10 rows in set (0.008 sec)

MySQL [tidb_migrate]> select  * from orders;
+----+---------+--------------+----------+------------+
| id | user_id | product_name | quantity | order_date |
+----+---------+--------------+----------+------------+
|  1 |       1 | Laptop       |        1 | 2023-01-15 |
|  2 |       2 | Mouse        |        2 | 2023-01-16 |
|  3 |       1 | Keyboard     |        1 | 2023-01-17 |
|  4 |       3 | Monitor      |        1 | 2023-01-18 |
|  5 |       4 | USB Cable    |        3 | 2023-01-19 |
|  6 |       3 | Webcam       |        1 | 2023-01-20 |
|  7 |       5 | Headphones   |        1 | 2023-01-21 |
|  8 |       6 | Desk Lamp    |        1 | 2023-01-22 |
|  9 |       7 | Notebook     |        5 | 2023-01-23 |
| 10 |       8 | Pen          |       10 | 2023-01-24 |
+----+---------+--------------+----------+------------+
10 rows in set (0.004 sec)

無事に移行が完了しています!

最後に

DumplingとLightningを使用してAurora MySQLからTiDB Serverlessにデータを移行してみました。
2テーブル、数十行のレコードとはいえ、簡単かつ迅速に移行を行うことができました。
S3バケット名のタイポで3時間詰まっていたことは秘密