1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Cloud SQLでのSQLServerの接続方法【GCPコンソール】

Posted at

メモ :bookmark_tabs:

実行環境

  • Windows10
  • コマンドプロンプト

1. インスタンスの作成

請求先アカウントを設定後、[インスタンスを作成]をクリック

[SQLServerを選択]をクリック

Compute Engine APIが有効になっていない場合、[APIを有効にする]をクリック

image.png

インスタンスの情報を入力

インスタンスIDは、小文字、数字、ハイフンのみ使用できます

最初に使用する構成の選択には、今回はテストなので「開発環境」を選択

リージョンとゾーンの可用性の選択
リージョンは変更できないので使用する地域に近い場所に設定する

インタンスのカスタマイズ

インスタンスのカスタマイズを完了後、[インスタンスを作成]をクリック

インスタンス作成には時間が掛かるので、ここで一息
:tea:

インスタンスの作成が完了すると、以下のようにインスタンスの画面が確認できる

image.png

2. インスタンスに接続する

  • Cloud SQL Auth Proxyは、IAM権限を用いて接続を認証するため、最も安全な接続方法
  • 受信接続をリッスンしてSSL/TLSでラップし、Cloud SQLインスタンスに渡す中間サーバーとして機能する

image.png
Cloud SQL Auth Proxy についてより引用

Cloud SQL Auth Proxyを利用するには、Cloud SQL Admin APIを有効にする必要がある

image.png

ここからGcloud CLIを使用するため、インストール

インストール後に確認

gcloud --version

Google Cloud SDK 467.0.0
bq 2.0.101
core 2024.02.29
gcloud-crc32c 1.0.0
gsutil 5.27

GCPの操作には、ログイン後、プロジェクトの選択が必要となる

gcloud auth login

gcloud config set project PROJECT_ID

gcloud components listコマンドでインストールされているツールを確認する

gcloud components list

Your current Google Cloud CLI version is: 467.0.0
The latest available version is: 467.0.0

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                    Components                                                   │
├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────────┬───────────┤
│     Status    │                         Name                         │              ID              │    Size   │
├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────────┼───────────┤
│ Not Installed │ App Engine Go Extensions                             │ app-engine-go                │   4.8 MiB │
│ Not Installed │ Appctl                                               │ appctl                       │  18.7 MiB │
│ Not Installed │ Artifact Registry Go Module Package Helper           │ package-go-module            │   < 1 MiB │
│ Not Installed │ Cloud Bigtable Command Line Tool                     │ cbt                          │  16.6 MiB │
│ Not Installed │ Cloud Bigtable Emulator                              │ bigtable                     │   7.1 MiB │
│ Not Installed │ Cloud Datastore Emulator                             │ cloud-datastore-emulator     │  36.2 MiB │
│ Not Installed │ Cloud Firestore Emulator                             │ cloud-firestore-emulator     │  44.4 MiB │
│ Not Installed │ Cloud Pub/Sub Emulator                               │ pubsub-emulator              │  63.3 MiB │
│ Not Installed │ Cloud Run Proxy                                      │ cloud-run-proxy              │  11.9 MiB │
│ Not Installed │ Cloud SQL Proxy                                      │ cloud_sql_proxy              │   7.4 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr        │   1.8 MiB │
│ Not Installed │ Log Streaming                                        │ log-streaming                │  12.4 MiB │
│ Not Installed │ Minikube                                             │ minikube                     │  35.5 MiB │
│ Not Installed │ Skaffold                                             │ skaffold                     │  23.9 MiB │
│ Not Installed │ Terraform Tools                                      │ terraform-tools              │  66.2 MiB │
│ Not Installed │ anthos-auth                                          │ anthos-auth                  │  22.0 MiB │
│ Not Installed │ config-connector                                     │ config-connector             │  56.9 MiB │
│ Not Installed │ enterprise-certificate-proxy                         │ enterprise-certificate-proxy │   6.5 MiB │
│ Not Installed │ gcloud Alpha Commands                                │ alpha                        │   < 1 MiB │
│ Not Installed │ gcloud Beta Commands                                 │ beta                         │   < 1 MiB │
│ Not Installed │ gcloud app Java Extensions                           │ app-engine-java              │ 125.9 MiB │
│ Not Installed │ gcloud app PHP Extensions                            │ app-engine-php               │  19.1 MiB │
│ Not Installed │ gcloud app Python Extensions                         │ app-engine-python            │   8.4 MiB │
│ Not Installed │ gcloud app Python Extensions (Extra Libraries)       │ app-engine-python-extras     │  31.5 MiB │
│ Not Installed │ gke-gcloud-auth-plugin                               │ gke-gcloud-auth-plugin       │   8.1 MiB │
│ Not Installed │ kubectl                                              │ kubectl                      │   < 1 MiB │
│ Not Installed │ kubectl-oidc                                         │ kubectl-oidc                 │  22.0 MiB │
│ Not Installed │ pkg                                                  │ pkg                          │           │
│ Installed     │ BigQuery Command Line Tool                           │ bq                           │   1.6 MiB │
│ Installed     │ Cloud Storage Command Line Tool                      │ gsutil                       │  11.3 MiB │
│ Installed     │ Google Cloud CLI Core Libraries                      │ core                         │  22.8 MiB │
│ Installed     │ Google Cloud CRC32C Hash Tool                        │ gcloud-crc32c                │   1.3 MiB │
└───────────────┴──────────────────────────────────────────────────────┴──────────────────────────────┴───────────┘
To install or remove components at your current SDK version [467.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [467.0.0], run:
  $ gcloud components update

Cloud SQL Proxy がインストールされていなければ、
gcloud components install {components_name}を実行

gcloud components install cloud_sql_proxy

インストール完了

Your current Google Cloud CLI version is: 467.0.0
Installing components from version: 467.0.0

┌─────────────────────────────────────────────────────────────────────────┐
│                   These components will be installed.                   │
├─────────────────────────────┬─────────────────────┬─────────────────────┤
│             Name            │       Version       │         Size        │
├─────────────────────────────┼─────────────────────┼─────────────────────┤
│ Cloud SQL Proxy             │              1.27.0 │             7.4 MiB │
└─────────────────────────────┴─────────────────────┴─────────────────────┘

For the latest full release notes, please visit:
  https://cloud.google.com/sdk/release_notes

Do you want to continue (Y/n)?  y

╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area                             ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud SQL Proxy                              ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud SQL Proxy                              ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...done.

Update done!

インストール後に確認

cloud_sql_proxy --version

Cloud SQL Auth proxy: 1.27.0+windows.amd64

接続名を指定してcloud-sql-proxyを実行

.\cloud-sql-proxy.exe INSTANCE_CONNECTION_NAME
躓きポイント :ghost: 公式ドキュメントの`.\cloud-sql-proxy.exe`はLinuxベースの環境での方法なので実行できない :skull: 実行ファイルを直接指定するか、環境変数などを利用する
"C:\Users\xxx\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin\cloud_sql_proxy.exe" -instances=trans-run-277920:asia-northeast1:gcp-express2022=tcp:3306

指定したポートが間違っている場合、以下のようなエラーとなった :skull:

"C:\Users\xxx\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin\cloud_sql_proxy.exe" -instances=trans-run-277920:asia-northeast1:gcp-express2022=tcp:1433

2024/03/08 06:03:47 listen tcp 127.0.0.1:1433: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

インスタンス名の指定方法が間違っている場合、以下のようなエラーとなった :skull:

"C:\Users\xxx\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin\cloud_sql_proxy.exe" -instances=trans-run-277920:asia-northeast1:gcp-express2022

2024/03/08 06:00:09 errors parsing config:
        invalid "trans-run-277920:asia-northeast1:gcp-express2022": unsupported network: unix

接続に成功すると、以下のようなメッセージが表示される

2024/03/08 06:04:37 Listening on 127.0.0.1:3306 for trans-run-277920:asia-northeast1:gcp-express2022
2024/03/08 06:04:37 Ready for new connections
2024/03/08 06:04:38 Generated RSA key in 131.2661ms

SQLServer Management Stduio からも接続できました :wink:

image.png

Reference

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?