Ricky 叩叮俱樂部

在開始之前,先來杯長島冰茶吧~

0%

Cloud SQL+SSH

前言

原本好一陣子的專案都在AWS上部署,沒想到目前手頭的專案客戶強烈要求要架設在GCP上

並且是採GCE、Cloud SQL、Cloud Storage分離的方式

因此這邊就來談談如何在本機及GCE上連線至Cloud SQL

在開始之前

但在操作以下步驟之前,你必須先符合以下條件:

  1. 擁有GCP帳號並在專案下建立GCE、Cloud SQL

  2. 於本機上安裝Cloud SDK

Cloud SDK

在安裝完Cloud SDK後,確認一下可以在終端機上執行

1
2
3
4
5
6
7
8
9
10
$ gcloud -v

Google Cloud SDK 381.0.0
beta 2022.04.08
bq 2.0.74
core 2022.04.08
gsutil 5.9
Updates are available for some Google Cloud CLI components. To install them,
please run:
$ gcloud components update

接著登入帳號,此時會跳出登入頁填寫輸入帳號密碼

1
2
3
4
5
$ gcloud auth login

You are now logged in as [xxx@gmail.com].
Your current project is [storied-deck-xxx]. You can change this setting by running:
$ gcloud config set project PROJECT_ID

這邊也可以看到在本機上登入過的帳號

1
2
3
4
5
6
7
8
9
$ gcloud auth list

Credentialed Accounts
ACTIVE ACCOUNT
abc@gmail.com
* xxx@gmail.com

To set the active account, run:
$ gcloud config set account `ACCOUNT`

再來設定專案

1
2
3
$ gcloud config set project <PROJECT_ID>

Updated property [core/project].

還有時區

1
2
3
$ gcloud config set compute/zone <ZONE_NAME>

Updated property [compute/zone].

最後則是地區

1
2
3
gcloud config set compute/region <REGION_NAME>

Updated property [compute/region].

完成後可以透過configurations查看目前的配置

1
$ gcloud config configurations list

Gcloud

只要完成上述的設定,就可以很輕鬆的使用Cloud SDK連線至資料庫,並且是SSL!

1
2
3
4
5
$ gcloud sql connect <INSTANCE_ID> --user=root

Allowlisting your IP for incoming connection for 5 minutes...

Connecting to database with SQL user [root].Enter password:

GCP會先將你的IP加入白名單後詢問密碼即可登入

接著馬上就可以看到熟悉的MySQL Command Line

1
2
3
4
5
6
7
8
9
10
11
12
13
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3773
Server version: 8.0.26-google (Google)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Cloud SQL Proxy

使用Cloud SDK很方便,但更多時候我們想讓專案或是資料庫管理工具(ex. DataGrip)連至Cloud SQL,而Cloud SQL預設是不允許外部連線的,這時候我們可以由Google自家提供的cloud_sql_proxy來辦到這件事

我們可以在本機上的任何地方(ex. ~/Downloads)將cloud_sql_proxy下載下來

設定cloud_sql_proxy權限

1
$ chmod +x cloud_sql_proxy

將本機Port連接至Cloud SQL

1
2
3
$ ./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:<PORT>

當出現 Ready for new connections 就代表連線成功

可將tcp設為33061避免與本機3306衝突

這時我們另開一個終端機並使用設好的PORT登入MySQL

1
$ mysql -u root -p -P 33061 -h 127.0.0.1

此時神奇的地方出現了,本機上的33061就代表SQL Cloud,而你可以將此PORT用於專案及資料庫管理工具上!

1
2
3
4
5
6
7
8
9
10
11
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1008
Server version: 8.0.26-google (Google)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Google Compute Engine

而除了本機連線外,VM上的連線也是不可獲缺的,首先至Google Cloud的編輯頁中點選連線設定,於已授權網路點選新增網路,輸入名稱及IP後即可在主機上連線

Cloud_SQL_ip.png