# 在GCP上创建Cloud SQL的三种方式(Console,gcloud,Terraform)

# 1 简介

Cloud SQL 是GCP上的关系型数据库,常用的有三种方式来创建:

(1) 界面操作

(2) 命令行 gcloud

(3) Terraform

在开始之前,可以查看:《初始化一个GCP项目并用gcloud访问操作 (opens new window)》。

# 2 GCP 操作界面

登陆GCP,选择SQL,可以创建MySQL、PostgreSQL和SQL Server:

以PostgreSQL为例,可以选择很多参数:

# 3 gcloud命令

通过命令行创建也很方便,可以自动化。命令如下:

$ gcloud sql instances create pkslow-testdb \
--database-version=POSTGRES_13 \
--region=us-west1 \
--cpu=1 \
--memory=3840MB \
--authorized-networks="0.0.0.0/0" 

Creating Cloud SQL instance...done.                                                                                                                             
Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/pkslow/instances/pkslow-testdb].
NAME           DATABASE_VERSION  LOCATION    TIER              PRIMARY_ADDRESS  PRIVATE_ADDRESS  STATUS
pkslow-testdb  POSTGRES_13       us-west1-a  db-custom-1-3840  34.83.146.214    -                RUNNABLE

authorized-networks 这个参数很重要,它决定了哪些客户端可以连接上数据库。

设置密码:

$ gcloud sql users set-password postgres \
--instance=pkslow-testdb \
--password=pkpass

给实例创建数据库:

$ gcloud sql databases create billing --instance=pkslow-testdb

Creating Cloud SQL database...done.                                                                                                                             
Created database [billing].
instance: pkslow-testdb
name: billing
project: pkslow

创建完成后,就可以在本地连接了:

连上之后,我们来创建一张表和插入一些数据来测试一下:

CREATE TABLE pkslow_person (
                               name varchar not null,
                                 age int not null,
                                 city varchar not null
);

insert into pkslow_person(name, age, city) VALUES ('Larry', 1, 'GZ');
insert into pkslow_person(name, age, city) VALUES ('pkslow', 2, 'SZ');
insert into pkslow_person(name, age, city) VALUES ('Deng', 21, 'SZ');
insert into pkslow_person(name, age, city) VALUES ('Eason', 13, 'BJ');
insert into pkslow_person(name, age, city) VALUES ('JJ', 43, 'SH');
insert into pkslow_person(name, age, city) VALUES ('Mi', 28, 'HK');

查看数据:

# 4 Terraform

当然,更好的方式还是通过Terraform来创建:

provider "google" {
  project     = "pkslow"
}

resource "google_sql_database_instance" "instance" {
  name             = "pkslow-postgresql"
  region           = "us-west1"
  database_version = "POSTGRES_13"
  settings {
    tier = "db-f1-micro"

    ip_configuration {
          ipv4_enabled = "true"

          authorized_networks {
            value           = "0.0.0.0/0"
            name            = "allow-all"
            expiration_time = "2099-01-01T00:00:00.000Z"
          }

        }
  }

  deletion_protection  = "true"
}

resource "google_sql_database" "database" {
  name     = "billing"
  instance = google_sql_database_instance.instance.name
}

resource "google_sql_user" "users" {
  name     = "larry"
  instance = google_sql_database_instance.instance.name
  password = "larry.pass"
}

这段代码为会我们创建三个资源:实例、数据库和用户。

准备好代码好,运行下面命令:

$ terraform init -plugin-dir=${TERRAFORM_PLUGIN}

$ terraform plan

$ terraform apply -auto-approve

Terraform还会自动在Bucket上创建文件来管理Terraform的状态:

创建完成后,检查就有两个数据库实例了:

$ gcloud sql instances list
NAME               DATABASE_VERSION  LOCATION    TIER              PRIMARY_ADDRESS  PRIVATE_ADDRESS  STATUS
pkslow-postgresql  POSTGRES_13       us-west1-a  db-f1-micro       34.127.53.161    -                RUNNABLE
pkslow-testdb      POSTGRES_13       us-west1-a  db-custom-1-3840  34.83.146.214    -                RUNNABLE

本地连接如下:

# 5 代码

代码请查看GitHub: https://github.com/LarryDpk/pkslow-samples


References:

GCP Create PostgreSQL instance (opens new window)

Terraform GCP Cloudsql (opens new window)

上次更新: 2023/8/18 23:39:36