创建一个云数据库实例

更新时间:

本文介绍如何使用Terraform创建一个云数据库实例。

说明

本教程所含示例代码支持一键运行,您可以直接运行代码。一键运行

前提条件

在开始之前,请您确保完成以下操作:

操作步骤

  1. 定义专有网络、交换机、数据库实例及相关的数据库和账号等资源,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"
    }
  2. 运行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.