創建一個云數據庫實例
更新時間:
本文介紹如何使用Terraform創建一個云數據庫實例。
說明
本教程所含示例代碼支持一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
在開始之前,請您確保完成以下操作:
使用Terraform,您需要一個阿里云賬號和訪問密鑰(AccessKey)。 請在阿里云控制臺中的AccessKey管理頁面上創建和查看您的AccessKey。
已經安裝并配置了Terraform,具體操作請參見在本地安裝和配置Terraform和在Cloud Shell中使用Terraform。
操作步驟
定義專有網絡、交換機、數據庫實例及相關的數據庫和賬號等資源,terraform.tf文件內容如下。
說明需要為每個Terraform項目創建一個獨立的執行目錄。
說明當前示例代碼支持一鍵運行,您可以直接運行代碼。一鍵運行
data "alicloud_zones" "default" { available_resource_creation = "VSwitch" } resource "alicloud_vpc" "main" { vpc_name = "alicloud" cidr_block = "10.1.0.0/21" } resource "alicloud_vswitch" "main" { vpc_id = alicloud_vpc.main.id cidr_block = "10.1.0.0/24" zone_id = data.alicloud_zones.default.zones[0].id } resource "alicloud_db_instance" "instance" { engine = "MySQL" engine_version = "5.6" instance_type = "rds.mysql.t1.small" instance_storage = "10" vswitch_id = alicloud_vswitch.main.id } resource "alicloud_rds_account" "account" { db_instance_id = alicloud_db_instance.instance.id account_name = "tf_account" account_password = "!Test@123456" } resource "alicloud_db_database" "db" { instance_id = alicloud_db_instance.instance.id name = "tf_database" } resource "alicloud_db_account_privilege" "privilege" { instance_id = alicloud_db_instance.instance.id account_name = alicloud_rds_account.account.account_name db_names = [alicloud_db_database.db.name] } resource "alicloud_db_connection" "connection" { instance_id = alicloud_db_instance.instance.id connection_prefix = "tf-example" }
運行terraform apply開始創建資源。出現類似下面的日志,說明創建成功。
alicloud_vpc.main: Creating... alicloud_vpc.main: Creation complete after 6s [id=vpc-bp1qdtiaztg1f9g5cqv3n] alicloud_vswitch.main: Creating... alicloud_vswitch.main: Creation complete after 5s [id=vsw-bp1i5dkhraoplr0pga7oy] alicloud_db_instance.instance: Creating... alicloud_db_instance.instance: Still creating... [10s elapsed] alicloud_db_instance.instance: Still creating... [20s elapsed] alicloud_db_instance.instance: Still creating... [30s elapsed] alicloud_db_instance.instance: Still creating... [40s elapsed] alicloud_db_instance.instance: Still creating... [50s elapsed] ... alicloud_db_instance.instance: Still creating... [5m0s elapsed] alicloud_db_instance.instance: Creation complete after 5m9s [id=rm-bp1mwp0hs49bgoizk] alicloud_db_connection.connection: Creating... alicloud_db_database.db: Creating... alicloud_db_account.account: Creating... alicloud_db_database.db: Creation complete after 0s [id=rm-bp1mwp0hs49bgoizk:tf_database] alicloud_db_account.account: Creation complete after 2s [id=rm-bp1mwp0hs49bgoizk:tf_account] alicloud_db_account_privilege.privilege: Creating... alicloud_db_connection.connection: Still creating... [10s elapsed] alicloud_db_account_privilege.privilege: Still creating... [10s elapsed] alicloud_db_connection.connection: Still creating... [20s elapsed] alicloud_db_account_privilege.privilege: Still creating... [20s elapsed] alicloud_db_account_privilege.privilege: Creation complete after 22s [id=rm-bp1mwp0hs49bgoizk:tf_account:ReadOnly] alicloud_db_connection.connection: Creation complete after 28s [id=rm-bp1mwp0hs49bgoizk:tf-example] Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
文檔內容是否對您有幫助?