乐趣区

关于云计算:在GCP上创建Cloud-SQL的三种方式ConsolegcloudTerraform

1 简介

Cloud SQL 是 GCP 上的关系型数据库,罕用的有三种形式来创立:

(1) 界面操作

(2) 命令行 gcloud

(3) Terraform

在开始之前,能够查看:《初始化一个 GCP 我的项目并用 gcloud 拜访操作》。

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/p…

退出移动版