Skip to content

Commit 3320e8c

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

File tree

5 files changed

+313
-0
lines changed

5 files changed

+313
-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: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
## here are the updates to merge:
2+
3+
4+
5+
data "alicloud_db_zones" "default" {
6+
engine = "MySQL"
7+
engine_version = "8.0"
8+
instance_charge_type = "PostPaid"
9+
category = "HighAvailability"
10+
db_instance_storage_type = "cloud_essd"
11+
}
12+
13+
# 查询实例实例规格
14+
data "alicloud_instance_types" "default" {
15+
availability_zone = data.alicloud_db_zones.default.zones.0.id
16+
system_disk_category = "cloud_essd"
17+
}
18+
19+
data "alicloud_db_instance_classes" "default" {
20+
zone_id = data.alicloud_db_zones.default.zones.0.id
21+
engine = "MySQL"
22+
engine_version = "8.0"
23+
category = "HighAvailability"
24+
instance_charge_type = "PostPaid"
25+
db_instance_storage_type = "cloud_essd"
26+
}
27+
28+
29+
# VPC
30+
resource "alicloud_vpc" "vpc" {
31+
vpc_name = "database-migration-test"
32+
cidr_block = "192.168.0.0/16"
33+
}
34+
35+
# VSwitch
36+
resource "alicloud_vswitch" "vswitch" {
37+
vpc_id = alicloud_vpc.vpc.id
38+
cidr_block = "192.168.0.0/24"
39+
zone_id = data.alicloud_db_zones.default.zones.0.id
40+
vswitch_name = "database-migration-vswitch"
41+
}
42+
43+
# Security Group
44+
resource "alicloud_security_group" "security_group" {
45+
security_group_name = "SG-DTS-GROUP-20220101"
46+
vpc_id = alicloud_vpc.vpc.id
47+
}
48+
49+
# Security Group Rule - Allow all traffic
50+
resource "alicloud_security_group_rule" "security_group_ingress" {
51+
security_group_id = alicloud_security_group.security_group.id
52+
type = "ingress"
53+
ip_protocol = "all"
54+
port_range = "-1/-1"
55+
cidr_ip = "0.0.0.0/0"
56+
}
57+
58+
# Data source for ECS image
59+
data "alicloud_images" "instance_image" {
60+
name_regex = "^aliyun_3_x64_20G_alibase_*"
61+
most_recent = true
62+
owners = "system"
63+
instance_type = data.alicloud_instance_types.default.instance_types[0].id
64+
}
65+
66+
# ECS Instance (WebServer)
67+
resource "alicloud_instance" "web_server" {
68+
instance_name = "database-migration-webserver"
69+
image_id = data.alicloud_images.instance_image.images[0].id
70+
instance_type = data.alicloud_instance_types.default.instance_types[0].id
71+
security_groups = [alicloud_security_group.security_group.id]
72+
vswitch_id = alicloud_vswitch.vswitch.id
73+
system_disk_category = "cloud_essd"
74+
internet_max_bandwidth_out = 80
75+
password = var.ecs_instance_password
76+
instance_charge_type = "PostPaid"
77+
}
78+
79+
# RDS Instance (Database)
80+
resource "alicloud_db_instance" "database" {
81+
engine = "MySQL"
82+
engine_version = "8.0"
83+
instance_type = data.alicloud_db_instance_classes.default.instance_classes.0.instance_class
84+
instance_storage = 20
85+
vpc_id = alicloud_vpc.vpc.id
86+
vswitch_id = alicloud_vswitch.vswitch.id
87+
security_group_ids = [alicloud_security_group.security_group.id]
88+
security_ips = [alicloud_instance.web_server.private_ip]
89+
zone_id = data.alicloud_db_zones.default.zones.0.id
90+
instance_charge_type = "Postpaid"
91+
category = "Basic"
92+
}
93+
94+
# RDS Database
95+
resource "alicloud_db_database" "wordpress_db" {
96+
instance_id = alicloud_db_instance.database.id
97+
name = "wordpressdb"
98+
character_set = "utf8mb4"
99+
description = "WordPress database for migration test"
100+
}
101+
102+
# RDS Account
103+
resource "alicloud_rds_account" "db_user" {
104+
db_instance_id = alicloud_db_instance.database.id
105+
account_name = var.db_user_name
106+
account_password = var.db_password
107+
account_type = "Super"
108+
account_description = "Database user for WordPress"
109+
}
110+
111+
# Local script for WordPress installation
112+
locals {
113+
wordpress_install_script = <<-SHELL
114+
#!/bin/sh
115+
DatabaseUser='wordpressuser'
116+
DatabasePwd='password'
117+
DatabaseName='wordpressdb'
118+
DatabaseHost='localhost'
119+
yum update -y
120+
yum install -y unzip zip
121+
yum install -y mysql-server
122+
systemctl start mysqld
123+
systemctl enable mysqld
124+
mysql -e "CREATE DATABASE wordpressdb;"
125+
mysql -e "CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'password';"
126+
mysql -e "GRANT ALL PRIVILEGES ON wordpressdb.* TO 'wordpressuser'@'localhost';"
127+
mysql -e "FLUSH PRIVILEGES;"
128+
mysql -e "CREATE USER dtssync1 IDENTIFIED BY 'P@ssw0rd';"
129+
mysql -e "GRANT ALL ON *.* TO 'dtssync1'@'%';"
130+
mysql -e "FLUSH PRIVILEGES;"
131+
mysql -e "SET GLOBAL binlog_format = 'ROW';"
132+
yum install -y nginx
133+
systemctl start nginx
134+
systemctl enable nginx
135+
yum install -y php php-fpm php-mysqlnd
136+
systemctl start php-fpm
137+
systemctl enable php-fpm
138+
cd /usr/share/nginx/html
139+
wget https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20240726/hhvpuw/wordpress-6.6.1.tar
140+
tar -xvf wordpress-6.6.1.tar
141+
cp -R wordpress/* .
142+
rm -R wordpress
143+
cp wp-config-sample.php wp-config.php
144+
sed -i "s/database_name_here/$DatabaseName/" wp-config.php
145+
sed -i "s/username_here/$DatabaseUser/" wp-config.php
146+
sed -i "s/password_here/$DatabasePwd/" wp-config.php
147+
sed -i "s/localhost/$DatabaseHost/" wp-config.php
148+
systemctl restart nginx
149+
systemctl restart php-fpm
150+
SHELL
151+
}
152+
153+
# ECS Command for WordPress installation
154+
resource "alicloud_ecs_command" "wordpress_install_command" {
155+
name = "wordpress-install-command"
156+
description = "Install WordPress and MySQL on ECS instance"
157+
enable_parameter = false
158+
type = "RunShellScript"
159+
command_content = base64encode(local.wordpress_install_script)
160+
timeout = 3600
161+
working_dir = "/root"
162+
}
163+
164+
# Execute command on ECS instance
165+
resource "alicloud_ecs_invocation" "wordpress_install_invocation" {
166+
instance_id = [alicloud_instance.web_server.id]
167+
command_id = alicloud_ecs_command.wordpress_install_command.id
168+
169+
depends_on = [
170+
alicloud_security_group_rule.security_group_ingress
171+
]
172+
173+
timeouts {
174+
create = "60m"
175+
}
176+
}
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)