AWS DMS 可以做到資料庫的遷移,例如將 A 資料庫以不影響現行服務的狀況下複製資料至 B 目標的資料庫中。且 A 和 B 非必要是同樣的 DB Engine , 例如你可以將 MySQL 遷移至 PostgreSQL 等。
而 DMS 主要的流程是在中間還會有一台機器(EC2) 負責將 source database 的資料複製至 target database。如以下圖的 Replication Instance 就是一台 EC2。
用途:
- 將原本的 db engine 遷移至不同的 db engine
- 資料異地備份
以下範例將採用 2 台 RDS 以及 1 台 replication instance 當作練習
- RDS MySQL 當作 source database
- RDS PostgreSQL 當作 target database
首先可以建立一台 free tier 的 RDS MySQL (db.tg4.micro) 當做 source database
緊接著在建立一台 free tier 的 RDS PostgreSQL (db.tg4.micro) 當作 target database。
都建立完畢後,連線至 source database 建立 source_a 以及 source_b database 以模擬到時僅將某一個 database 轉移至 target database。
建立 source_a 以及 source_b database
create database source_a;
create database source_b
分別在 source_a 以及 source_b 建立 person 資料表以及新增資料至 person table
CREATE TABLE persons (
id INT AUTO_INCREMENT PRIMARY KEY,
last_name VARCHAR(255),
first_name VARCHAR(255),
address VARCHAR(255),
city VARCHAR(255),
meta JSON
);
INSERT INTO persons (last_name, first_name, address, city, meta)
VALUES ('gary', 'garyng', '123 Main St', 'New York', '{"age": 30, "hobbies": ["reading", "traveling"]}');
都處理完畢後開始設定 DMS, 進入 DMS dashboard
注意:以下的前四個步驟 provider 的設定是給 migration project ,但是 migration project 在創建的時候, db 的連線資訊需設定在 secret manager, 假如沒有使用 secret manager 則無法建立 migration project。
- 設定 data provider , 總共要創建兩個 provider, 一個是 source provider 而另外一個為 target provider
1–1 選擇 MySQL 以及選擇 rds 所建立的 rds name
1–2 建立 target data provider (postgresql)
建立完後在 data provider 即可看見所建立的 provider
2. 配置 endpoint (連線資訊), 這邊也需要建立兩個 endpoint , 一個是 source endpoint (MySQL), 另外一個則是 target endpoint (PostgreSQL)
2–1 首先建立 dms-vpc-role iam role。
trust relationship如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSDMSVPCPolicyTemplate",
"Effect": "Allow",
"Principal": {
"Service": "dms.amazonaws.com"
}
}
]
}
policy 如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CreateNetworkInterface",
"Effect": "Allow",
"Action": [
"ec2:CreateNetworkInterface",
"ec2:DeleteNetworkInterface",
"ec2:ModifyNetworkInterfaceAttribute"
],
"Resource": [
"arn:aws:ec2:*:<account id>:network-interface/*",
"arn:aws:ec2:*:<account id>:instance/*",
"arn:aws:ec2:*:<account id>:subnet/*",
"arn:aws:ec2:*:<account id>:security-group/*"
]
},
{
"Sid": "DescribeVPC",
"Effect": "Allow",
"Action": [
"ec2:DescribeAvailabilityZones",
"ec2:DescribeInternetGateways",
"ec2:DescribeSubnets",
"ec2:DescribeVpcs",
"ec2:DescribeSecurityGroups",
"ec2:DescribeDhcpOptions",
"ec2:DescribeNetworkInterfaces"
],
"Resource": "*"
}
]
}
2–2 建立 dms-cloudwatch-logs-role iam role
trust relationship 如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSDMSCloudWatchPolicyTemplate",
"Effect": "Allow",
"Principal": {
"Service": "dms.amazonaws.com"
}
}
]
}
permission policy 如下
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowDescribeOnAllLogGroups",
"Effect": "Allow",
"Action": [
"logs:DescribeLogGroups"
],
"Resource": [
"arn:aws:logs:*:<account id>:log-group:*"
]
},
{
"Sid": "AllowDescribeOfAllLogStreamsOnDmsTasksLogGroup",
"Effect": "Allow",
"Action": [
"logs:DescribeLogStreams"
],
"Resource": [
"arn:aws:logs:*:<account id>:log-group:dms-tasks-*",
"arn:aws:logs:*:<account id>:log-group:dms-serverless-replication-*"
]
},
{
"Sid": "AllowCreationOfDmsLogGroups",
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup"
],
"Resource": [
"arn:aws:logs:*:<account id>:log-group:dms-tasks-*",
"arn:aws:logs:*:<account id>:log-group:dms-serverless-replication-*:log-stream:"
]
},
{
"Sid": "AllowCreationOfDmsLogStream",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream"
],
"Resource": [
"arn:aws:logs:*:<account id>:log-group:dms-tasks-*:log-stream:dms-task-*",
"arn:aws:logs:*:<account id>:log-group:dms-serverless-replication-*:log-stream:dms-serverless-*"
]
},
{
"Sid": "AllowUploadOfLogEventsToDmsLogStream",
"Effect": "Allow",
"Action": [
"logs:PutLogEvents"
],
"Resource": [
"arn:aws:logs:*:<account id>:log-group:dms-tasks-*:log-stream:dms-task-*",
"arn:aws:logs:*:<account id>:log-group:dms-serverless-replication-*:log-stream:dms-serverless-*"
]
}
]
}
iam role 都建立好後建立 source endpoint 以及 target endpoint
注意!! postgresql 的 endpoint 需啟用 ssl mode
3. 建立給 ec2 使用的 instance profile已配置 VPC 等相關網路設定
3–1 設定 subnet group
4. 設定 migration project
4–1 設定 第三步驟設定的 subnet group 以及 instance profile
4–2 指定資料庫的 secret manager
5. 建立 replication instance
這邊因為是演練使用,所以使用 single-az 建立 replication instance
subnet group 選擇前面所建立的 subnet group
都選擇好後點擊 Create
5. 建立 migration task
5–1 選擇前面步驟建立的 replication instance 以及 endpoint
5–2 這邊為了使用 CDC (changed data captured) 功能所以選擇 migrate and replicate
5–3 因為要持續進行 replicate, 所以不要 stop migration task
5–4 設定 table mapping , 目的是要將 source rds 的 source_a database 遷移至 target rds 的 target_a database
transformation rule 設定如下
建立完成後顯示 migration task 正在創建, 在執行 migration 前會先進行 assessment
接著會發現建立失敗,因為 assessment 一些檢查會發生問題。
- 設定 source database mysql 權限
CREATE USER 'dms_user'@'%' IDENTIFIED BY '<password>';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'dms_user'@'%';
GRANT SELECT, RELOAD, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER ON *.* TO 'dms_user'@'%';
2. 設定 target database postgresql 權限
CREATE USER dms_user WITH LOGIN PASSWORD '<password>';
GRANT USAGE ON SCHEMA public TO dms_user;
GRANT CONNECT ON DATABASE target_a to dms_user;
GRANT CREATE ON DATABASE target_a TO dms_user;
GRANT CREATE ON SCHEMA public TO dms_user;
GRANT UPDATE, INSERT, SELECT, DELETE, TRUNCATE ON ALL TABLES IN SCHEMA public TO dms_user;
GRANT rds_superuser TO dms_user; # full load and cdc
3. mysql 的 binlog format 要從 mixed 改為 row
4. 重新執行一次 assessment
此時可以透過在 source database 新增資料,而在 target database 檢查是否有資料進來。
參考資源: