This gist describes how to use the mysql client, installed on a Compute Engine instance, to connect to Google Cloud SQL.
If you are connecting to a First Generation instance of Google Cloud SQL, you must use an IPv4 address to connect. If you are connecting to a Second Generation instance, you can also connect using the Cloud SQL Proxy or the proxy Docker image.
For information about using the Cloud SQL Proxy, see Connecting mysql Client Using the Cloud SQL Proxy. For information about using the Cloud SQL Proxy Docker image, see Connecting mysql Client Using the Cloud SQL Proxy Docker Image. For information about connecting using IP addresses, see Configuring access for IP connections.
Before you can connect to your Cloud SQL instance, you must have a default database user (root) on the instance.
This task does not include instructions for setting up your Compute Engine instance. If you need help with creating and configuring a Compute Engine instance, see the Compute Engine documentation.
sudo apt-get update
sudo apt-get install mysql-client
Install the Cloud SQL Proxy on the Compute Engine instance.
cd /usr/local/bin
sudo wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
sudo chmod +x cloud_sql_proxy
Some possible proxy invocation could be done like this :
- Using Cloud SDK authentication:
cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306
- Using a service account and explicit instance specification (recommended for production environments):
cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306 -credential_file=<PATH_TO_KEY_FILE>
Now you can connection with :
mysql -u <USERNAME> -p --host 127.0.0.1
- Copy above gist to
/etc/systemd/system/cloud-sql-proxy.service
- run
sudo systemctl daemon-reload
- and
sudo systemctl start cloud-sql-proxy
You can now see you service running with sudo systemctl status cloud-sql-proxy.service
I'm using CentOS in my Compute Engine, had follow your instructions and I got the following error:
Failed to start cloud-sql-proxy.service: Unit not found.