Skip to content

Commit a17af2c

Browse files
author
shaobolin
committed
feat: 解决方案migrate-database-to-cloud tf文件完成
1 parent 5ab7b50 commit a17af2c

File tree

5 files changed

+309
-0
lines changed

5 files changed

+309
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## Introduction
2+
<!-- DOCS_DESCRIPTION_CN -->
3+
本示例用于实现解决方案[自建数据库迁移到云数据库](https://www.aliyun.com/solution/tech-solution/mysql-rds), 涉及到专有网络(VPC)、交换机(VSwitch)、云服务器(ECS)、云数据库(RDS)MySQL版等资源的部署。
4+
<!-- DOCS_DESCRIPTION_CN -->
5+
6+
<!-- DOCS_DESCRIPTION_EN -->
7+
This example is used to implement solution [Migrate Self-Built Database to Cloud Database](https://www.aliyun.com/solution/tech-solution/mysql-rds), which involves the creation and deployment of resources such as Virtual Private Cloud (VPC), VSwitch, Elastic Compute Service (ECS), ApsaraDB RDS.
8+
<!-- DOCS_DESCRIPTION_EN -->
9+
10+
<!-- BEGIN_TF_DOCS -->
11+
## Providers
12+
13+
| Name | Version |
14+
|------|---------|
15+
| <a name="provider_alicloud"></a> [alicloud](#provider\_alicloud) | n/a |
16+
17+
## Modules
18+
19+
No modules.
20+
21+
## Resources
22+
23+
| Name | Type |
24+
|------|------|
25+
| [alicloud_db_database.wordpress_db](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_database) | resource |
26+
| [alicloud_db_instance.database](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_instance) | resource |
27+
| [alicloud_ecs_command.wordpress_install_command](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ecs_command) | resource |
28+
| [alicloud_ecs_invocation.wordpress_install_invocation](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ecs_invocation) | resource |
29+
| [alicloud_instance.web_server](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/instance) | resource |
30+
| [alicloud_rds_account.db_user](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/rds_account) | resource |
31+
| [alicloud_security_group.security_group](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group) | resource |
32+
| [alicloud_security_group_rule.security_group_ingress](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group_rule) | resource |
33+
| [alicloud_vpc.vpc](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource |
34+
| [alicloud_vswitch.vswitch](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
35+
| [alicloud_db_instance_classes.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/db_instance_classes) | data source |
36+
| [alicloud_db_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/db_zones) | data source |
37+
| [alicloud_images.instance_image](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/images) | data source |
38+
| [alicloud_instance_types.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/instance_types) | data source |
39+
40+
## Inputs
41+
42+
| Name | Description | Type | Default | Required |
43+
|------|-------------|------|---------|:--------:|
44+
| <a name="input_db_instance_engine_and_version"></a> [db\_instance\_engine\_and\_version](#input\_db\_instance\_engine\_and\_version) | 引擎类型及版本,数据库引擎类型及版本,默认为MySQL 8.0。 | `string` | `"MySQL 8.0"` | no |
45+
| <a name="input_db_password"></a> [db\_password](#input\_db\_password) | RDS数据库密码,长度8-30,必须包含三项(大写字母、小写字母、数字、特殊符号)。 | `string` | n/a | yes |
46+
| <a name="input_db_user_name"></a> [db\_user\_name](#input\_db\_user\_name) | RDS数据库账号,由2到16个小写字母组成,下划线。必须以字母开头,以字母数字字符结尾。 | `string` | `"dbuser"` | no |
47+
| <a name="input_ecs_instance_password"></a> [ecs\_instance\_password](#input\_ecs\_instance\_password) | 实例密码,服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、特殊符号)。 | `string` | n/a | yes |
48+
| <a name="input_region"></a> [region](#input\_region) | 地域 | `string` | `"cn-hangzhou"` | no |
49+
<!-- END_TF_DOCS -->
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
data "alicloud_db_zones" "default" {
2+
engine = "MySQL"
3+
engine_version = "8.0"
4+
instance_charge_type = "PostPaid"
5+
category = "Basic"
6+
db_instance_storage_type = "cloud_essd"
7+
}
8+
9+
# 查询实例实例规格
10+
data "alicloud_instance_types" "default" {
11+
availability_zone = data.alicloud_db_zones.default.zones.0.id
12+
system_disk_category = "cloud_essd"
13+
}
14+
15+
data "alicloud_db_instance_classes" "default" {
16+
zone_id = data.alicloud_db_zones.default.zones.0.id
17+
engine = "MySQL"
18+
engine_version = "8.0"
19+
category = "Basic"
20+
instance_charge_type = "PostPaid"
21+
db_instance_storage_type = "cloud_essd"
22+
}
23+
24+
25+
# VPC
26+
resource "alicloud_vpc" "vpc" {
27+
vpc_name = "database-migration-test"
28+
cidr_block = "192.168.0.0/16"
29+
}
30+
31+
# VSwitch
32+
resource "alicloud_vswitch" "vswitch" {
33+
vpc_id = alicloud_vpc.vpc.id
34+
cidr_block = "192.168.0.0/24"
35+
zone_id = data.alicloud_db_zones.default.zones.0.id
36+
vswitch_name = "database-migration-vswitch"
37+
}
38+
39+
# Security Group
40+
resource "alicloud_security_group" "security_group" {
41+
security_group_name = "SG-DTS-GROUP-20220101"
42+
vpc_id = alicloud_vpc.vpc.id
43+
}
44+
45+
# Security Group Rule - Allow all traffic
46+
resource "alicloud_security_group_rule" "security_group_ingress" {
47+
security_group_id = alicloud_security_group.security_group.id
48+
type = "ingress"
49+
ip_protocol = "all"
50+
port_range = "-1/-1"
51+
cidr_ip = "0.0.0.0/0"
52+
}
53+
54+
# Data source for ECS image
55+
data "alicloud_images" "instance_image" {
56+
name_regex = "^aliyun_3_x64_20G_alibase_*"
57+
most_recent = true
58+
owners = "system"
59+
instance_type = data.alicloud_instance_types.default.instance_types[0].id
60+
}
61+
62+
# ECS Instance (WebServer)
63+
resource "alicloud_instance" "web_server" {
64+
instance_name = "database-migration-webserver"
65+
image_id = data.alicloud_images.instance_image.images[0].id
66+
instance_type = data.alicloud_instance_types.default.instance_types[0].id
67+
security_groups = [alicloud_security_group.security_group.id]
68+
vswitch_id = alicloud_vswitch.vswitch.id
69+
system_disk_category = "cloud_essd"
70+
internet_max_bandwidth_out = 80
71+
password = var.ecs_instance_password
72+
instance_charge_type = "PostPaid"
73+
}
74+
75+
# RDS Instance (Database)
76+
resource "alicloud_db_instance" "database" {
77+
engine = "MySQL"
78+
engine_version = "8.0"
79+
instance_type = data.alicloud_db_instance_classes.default.instance_classes.0.instance_class
80+
instance_storage = 20
81+
vpc_id = alicloud_vpc.vpc.id
82+
vswitch_id = alicloud_vswitch.vswitch.id
83+
security_group_ids = [alicloud_security_group.security_group.id]
84+
security_ips = [alicloud_instance.web_server.private_ip]
85+
zone_id = data.alicloud_db_zones.default.zones.0.id
86+
instance_charge_type = "Postpaid"
87+
category = "Basic"
88+
}
89+
90+
# RDS Database
91+
resource "alicloud_db_database" "wordpress_db" {
92+
instance_id = alicloud_db_instance.database.id
93+
name = "wordpressdb"
94+
character_set = "utf8mb4"
95+
description = "WordPress database for migration test"
96+
}
97+
98+
# RDS Account
99+
resource "alicloud_rds_account" "db_user" {
100+
db_instance_id = alicloud_db_instance.database.id
101+
account_name = var.db_user_name
102+
account_password = var.db_password
103+
account_type = "Super"
104+
account_description = "Database user for WordPress"
105+
}
106+
107+
# Local script for WordPress installation
108+
locals {
109+
wordpress_install_script = <<-SHELL
110+
#!/bin/sh
111+
DatabaseUser='wordpressuser'
112+
DatabasePwd='password'
113+
DatabaseName='wordpressdb'
114+
DatabaseHost='localhost'
115+
yum update -y
116+
yum install -y unzip zip
117+
yum install -y mysql-server
118+
systemctl start mysqld
119+
systemctl enable mysqld
120+
mysql -e "CREATE DATABASE wordpressdb;"
121+
mysql -e "CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';"
122+
mysql -e "GRANT ALL PRIVILEGES ON wordpressdb.* TO 'wordpressuser'@'localhost';"
123+
mysql -e "FLUSH PRIVILEGES;"
124+
mysql -e "CREATE USER dtssync1 IDENTIFIED BY 'P@ssw0rd';"
125+
mysql -e "GRANT ALL ON *.* TO 'dtssync1'@'%';"
126+
mysql -e "FLUSH PRIVILEGES;"
127+
mysql -e "SET GLOBAL binlog_format = 'ROW';"
128+
yum install -y nginx
129+
systemctl start nginx
130+
systemctl enable nginx
131+
yum install -y php php-fpm php-mysqlnd
132+
systemctl start php-fpm
133+
systemctl enable php-fpm
134+
cd /usr/share/nginx/html
135+
wget https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20240726/hhvpuw/wordpress-6.6.1.tar
136+
tar -xvf wordpress-6.6.1.tar
137+
cp -R wordpress/* .
138+
rm -R wordpress
139+
cp wp-config-sample.php wp-config.php
140+
sed -i "s/database_name_here/$DatabaseName/" wp-config.php
141+
sed -i "s/username_here/$DatabaseUser/" wp-config.php
142+
sed -i "s/password_here/$DatabasePwd/" wp-config.php
143+
sed -i "s/localhost/$DatabaseHost/" wp-config.php
144+
systemctl restart nginx
145+
systemctl restart php-fpm
146+
SHELL
147+
}
148+
149+
# ECS Command for WordPress installation
150+
resource "alicloud_ecs_command" "wordpress_install_command" {
151+
name = "wordpress-install-command"
152+
description = "Install WordPress and MySQL on ECS instance"
153+
enable_parameter = false
154+
type = "RunShellScript"
155+
command_content = base64encode(local.wordpress_install_script)
156+
timeout = 3600
157+
working_dir = "/root"
158+
}
159+
160+
# Execute command on ECS instance
161+
resource "alicloud_ecs_invocation" "wordpress_install_invocation" {
162+
instance_id = [alicloud_instance.web_server.id]
163+
command_id = alicloud_ecs_command.wordpress_install_command.id
164+
165+
depends_on = [
166+
alicloud_security_group_rule.security_group_ingress
167+
]
168+
169+
timeouts {
170+
create = "60m"
171+
}
172+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ECS Instance Login Information
2+
output "ecs_instance_user" {
3+
description = "Username and password for logging in to ECS instance"
4+
value = "USERNAME: root ; PASSWORD: ${var.ecs_instance_password}"
5+
sensitive = true
6+
}
7+
8+
# WordPress URL
9+
output "ecs_word_press_url" {
10+
description = "WordPress default address"
11+
value = "http://${alicloud_instance.web_server.public_ip}"
12+
}
13+
14+
# RDS Internal Connection Address
15+
output "rds_internal_address" {
16+
description = "RDS internal network address"
17+
value = alicloud_db_instance.database.connection_string
18+
}
19+
20+
# RDS User Information for DTS
21+
output "rds_user_dts" {
22+
description = "RDS username and password for connecting to DTS"
23+
value = "USERNAME: ${var.db_user_name} PASSWORD: ${var.db_password}"
24+
sensitive = true
25+
}
26+
27+
# WordPress Database User for DTS
28+
output "wp_user_for_dts" {
29+
description = "ECS-hosted database username and password for connecting to DTS"
30+
value = "USERNAME: dtssync1 ; PASSWORD: P@ssw0rd"
31+
sensitive = true
32+
}
33+
34+
# WordPress Database User for SQL
35+
output "wp_user_for_sql" {
36+
description = "ECS-hosted database username and password for executing SQL"
37+
value = "USERNAME: wordpressuser ; PASSWORD: password"
38+
sensitive = true
39+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "alicloud" {
2+
region = var.region
3+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
variable "region" {
2+
description = "地域"
3+
type = string
4+
default = "cn-hangzhou"
5+
}
6+
7+
8+
variable "db_instance_engine_and_version" {
9+
type = string
10+
description = "引擎类型及版本,数据库引擎类型及版本,默认为MySQL 8.0。"
11+
validation {
12+
condition = contains(["MySQL 8.0"], var.db_instance_engine_and_version)
13+
error_message = "数据库引擎类型及版本必须为MySQL 8.0。"
14+
}
15+
default = "MySQL 8.0"
16+
}
17+
18+
variable "db_password" {
19+
type = string
20+
description = "RDS数据库密码,长度8-30,必须包含三项(大写字母、小写字母、数字、特殊符号)。"
21+
sensitive = true
22+
validation {
23+
condition = length(var.db_password) >= 8 && length(var.db_password) <= 30
24+
error_message = "密码长度必须在8-30之间。"
25+
}
26+
}
27+
28+
variable "db_user_name" {
29+
type = string
30+
description = "RDS数据库账号,由2到16个小写字母组成,下划线。必须以字母开头,以字母数字字符结尾。"
31+
validation {
32+
condition = can(regex("^[a-z][a-z0-9_]{0,14}[a-z0-9]$", var.db_user_name)) && length(var.db_user_name) >= 2 && length(var.db_user_name) <= 16
33+
error_message = "由2到16个小写字母组成,下划线。必须以字母开头,以字母数字字符结尾。"
34+
}
35+
default = "dbuser"
36+
}
37+
38+
variable "ecs_instance_password" {
39+
type = string
40+
description = "实例密码,服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、特殊符号)。"
41+
sensitive = true
42+
validation {
43+
condition = length(var.ecs_instance_password) >= 8 && length(var.ecs_instance_password) <= 30
44+
error_message = "密码长度必须在8-30之间。"
45+
}
46+
}

0 commit comments

Comments
 (0)