0
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 5 years have passed since last update.

【GCP】インスタンスのMySQLからGCPのSQLに接続する

Posted at

AWSでもかなり前にやったけど、ほぼ覚えてません。
今後のためにどうやって使うのか試してみました。

インスタンスにMySQLをインストール。
apacheはいれたけど、PHPはめんどくさいので入れなかった。

GCPでSQLを作成するとき、インスタンスで設定したリージョン、ゾーンと合わせる必要がある。
GCEの画面から確認できるので、あらかじめ確認しておく。

承認したIPアドレスからのみSQLへの接続を許可するため、
接続>新しいネットワークで設定する。
保存を押せば完了。

ローカルのターミナルにて、インスタンスにログイン→以下のコマンドで接続できる。
ipアドレス部分は作成したgcp側のSQLのもの。

===
[@instance-1 ~]$ mysql -h ...** -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 124
Server version: 5.7.25-google-log (Google)

Copyright (c) 2000, 2020, 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>
===

なお、PHPコード上からDBに接続する際はホスト名にCloud SQLのIPアドレスを設定すれば接続することができます。

【所感】
・AWSで作った時よりも簡単でスムーズにいけたような気がした。
(その当時、サーバー構築の知識が皆無だったので、あまりあてになりませんが。。。)
・サンプルデータなどを使って挙動を確かめたほうが良いと思う。

【参考URL】
http://yebisupress.dac.co.jp/2018/08/23/publish_website_with_google-cloud-platform/

0
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
0
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?