Cloud SpannerのエミュレータにDBツールでローカルから接続する(DBeaver)
はじめに
- Cloud Spanner使いたいけれどお高いので、なるべく使いたくない。
- そんな時はローカルに立てたエミュレータでOKじゃない?
- SpannerってCLIじゃないと接続できないの?
- DBeaverで接続できるらしい
- DBeaverだとエミュレータに接続できてないっぽい? 👈いまここ
ということでDBeaverからCloud Spannerのエミュレータに接続したいと思います。
事前準備
Cloud SDK をインストール
gcloud の更新
gcloud components update
エミュレータの起動
gcloud emulators spanner start Executing: docker run -p 127.0.0.1:9010:9010 -p 127.0.0.1:9020:9020 gcr.io/cloud-spanner-emulator/emulator:1.2.0 [cloud-spanner-emulator] Unable to find image 'gcr.io/cloud-spanner-emulator/emulator:1.2.0' locally [cloud-spanner-emulator] 1.2.0: Pulling from cloud-spanner-emulator/emulator ・・・ [cloud-spanner-emulator] Status: Downloaded newer image for gcr.io/cloud-spanner-emulator/emulator:1.2.0 [cloud-spanner-emulator] 2021/08/18 11:27:52 gateway.go:140: Cloud Spanner emulator running. [cloud-spanner-emulator] 2021/08/18 11:27:52 gateway.go:141: REST server listening at 0.0.0.0:9020 [cloud-spanner-emulator] 2021/08/18 11:27:52 gateway.go:142: gRPC server listening at 0.0.0.0:9010 # これで起動完了
DBeaverのインストール
割愛
接続タイプの選択
Cloud Spannerを選択
接続情報の設定
プロジェクト、インスタンス、データベースをそれぞれ設定
例)test-project、test-instance、test-database
private keyは未設定でOK
ドライバの設定
[ドライバのプロパティ]タブを開く
初回はドライバのインストールが必要なのでダウンロード
終わると下記の画面になるので、[ドライバのプロパティ]の[autoConfigEmulator]の値をtrueに変更
テスト接続してエラーが出なければOK
参考サイト
Cloud Spanner エミュレータの使用 | Google Cloud
Use DBeaver with Cloud Spanner and Cloud Spanner Emulator | Google Cloud - Community
おまけ
テーブル作成
・SQLエディタを開く
インスタンス名右クリック>SQLエディタ>SQLエディタ
・作成するCREATE文を入力して実行
・自動的には画面に表示されないので、作成したスキーマを選択してF5
※スキーマ名右クリックで[更新]でも可
無事作成できています。
おまけ2
・gcloudでエミュレータを参照したい場合は以下のコマンドを実行します。
エミュレータの構成を作成して有効にする
gcloud config configurations create emulator gcloud config set auth/disable_credentials true gcloud config set project your-project-id gcloud config set api_endpoint_overrides/spanner http://localhost:9020/
・構成の確認
gcloud config configurations list NAME IS_ACTIVE ACCOUNT PROJECT COMPUTE_DEFAULT_ZONE COMPUTE_DEFAULT_REGION default False アカウント プロジェクト名 asia-northeast1-b asia-northeast1 emulator True your-project-id
emulatorの[IS_ACTIVE]がTrueになっているのでemulator側の構成が有効になっているのがわかります。
・インスタンスの作成
gcloud spanner instances create test-instance2 ` --config=emulator-config --description="Test Instance" --nodes=1
test-instance2が作られ、接続もできます。
・エミュレータとデフォルトの設定切り替え
gcloud config configurations activate [emulator | default]