0
votes

I have Cloud SQL and Instance in same project of Google Cloud.

I want to connect to Mysql (from Google Cloud SQL) from Instance using command line.

I am connecting using command:

gcloud sql connect cloud-sql-name --user=username

its giving:

 ERROR: (gcloud.sql.connect) There was no instance found  or you are not authorized to connect to it.

How do I make connection.

2

2 Answers

2
votes

You probably missed the authorization of your instance's IP to be able to connect to the Cloud SQL database. This is done for e.g. through the Cloud Console in the Cloud SQL Instances page.

To see the step-by-step guide for connecting from Compute Engine instance to Cloud SQL (with the mysql client) check this docs page.

1
votes

Are you trying to connect to your Cloud SQL instance from your Compute Engine VM instance?

If this is the case, I can suggest to first grant your service account (the Compute Engine default service account or a new one) the corresponding IAM role/permission for Cloud SQL [1].

Then, authenticate/activate [2] your service account within your VM instance command line (it is recommended to generate a JSON key File instead of a P12 key File for your service account):

~$ gcloud auth activate-service-account [ACCOUNT] --key-file=KEY_FILE

To generate a key File for your service account, you can go to your Products and Services menu > IAM & admin > Service accounts. Click on the three-dotted button at the right of the corresponding service account and select "Create key".

Your key File should be stored/uploaded to your VM instance, so that it can use it to activate the service account.

It is possible that you would also need to grant your VM instance access for Cloud SQL within its Cloud API access scopes panel. Go to your Products and Services menu > Compute Engine > VM instances. Select your VM instance and edit it.

Be aware that you will need to stop your VM instance before editing Cloud API access scopes. Go to "Access scopes" > "Set access for each API". Enable and Save.

You will also have to enable the Cloud SQL Admin API. Go to your Products and Services menu > API and services. Search for SQL Admin and Enable it (wait a few minutes)

Start the VM instance and try your Cloud SQL tests (re-activate your service account if necessary).

Your Cloud SQL instance for MySQL creates the "root" user. Just make sure to use this as the username, or any other you have created (if any).

Take into account that since you would be connecting from a Compute Engine VM instance, it is possible that yo will be asked for a MySQL Client.

[1] https://cloud.google.com/sql/docs/mysql/project-access-control

[2] https://cloud.google.com/sdk/gcloud/reference/auth/activate-service-account