![](https://hellomyworld.net/wp-content/uploads/2021/07/face_20210703_222637-e1625369239626-300x282.png)
私
Google Cloud Platform で DBインスタンスを作成し、自マシンからアクセスするには?
コンテンツ
データベースインスタンスの作成
好きなデータベースを選択
現在は MySQL、PostgreSQL、SQL Server の三つから選ぶことが可能。
今回はポスグレを使うよ
![](https://hellomyworld.net/wp-content/uploads/2021/09/gcloud_1-1024x255.jpg)
情報の入力
インスタンスIDやアドミンパスワード等、必要な情報を入力。
![](https://hellomyworld.net/wp-content/uploads/2021/09/gcloud_2.jpg)
Public IP アドレスを確認
作成が完了したら、IPアドレスを確認しておこう。
インスタンスの作成が完了するまで数分かかるよ
![](https://hellomyworld.net/wp-content/uploads/2021/09/gcloud_3-1024x897.jpg)
ネットワークの設定
自マシンから接続するために、事前に自マシンの外部IPアドレスを登録しておこう。
外部からの接続を許可したいIPアドレスの登録はこちらから
![](https://hellomyworld.net/wp-content/uploads/2021/09/gcloud_4-808x1024.jpg)
![](https://hellomyworld.net/wp-content/uploads/2021/09/gcloud_5.jpg)
gcloud CLI を自マシンにインストール
インストール
以下のリンクからファイルをダウンロードし、展開してから install.sh を実行する。
-
-
Install the gcloud CLI | Google Cloud CLI Documentation
cloud.google.com
初期設定
gcloud init でタイムゾーンの設定や、ログインの確認などの初期設定をする。
実行例
hellomyworld@MacBook-Air ~ % gcloud init
Welcome! This command will take you through the configuration of gcloud.
Your current configuration has been set to: [default]
You can skip diagnostics next time by using the following flag:
gcloud init --skip-diagnostics
Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).
You must log in to continue. Would you like to log in (Y/n)?
Your browser has been opened to visit:
接続確認
先ほど作成したデータベースに接続することができることを確認。
gcloud sql connect <インスタンス名> --user=postgres
実行例
hellomyworld@MacBook-Air ~ % gcloud sql connect test --user=postgres
Allowlisting your IP for incoming connection for 5 minutes...⠶
Allowlisting your IP for incoming connection for 5 minutes...done.
Connecting to database with SQL user [postgres].Password: