Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ErikSaunier
Created May 30, 2018 12:08
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ErikSaunier/e8ff253acc8d18a57192d53b6850300f to your computer and use it in GitHub Desktop.
Save ErikSaunier/e8ff253acc8d18a57192d53b6850300f to your computer and use it in GitHub Desktop.
How to install Cloud SQL Proxy on Compute Engine instance and make it start on boot with Systemd

How to install Cloud SQL Proxy on Compute Engine instance and make it start on boot with Systemd

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 begin

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.

Install the mysql client on your Computer Engine instance, if it is not already installed.

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

Start Cloud SQL Proxy on system boot with Systemd

  1. Copy above gist to /etc/systemd/system/cloud-sql-proxy.service
  2. run sudo systemctl daemon-reload
  3. and sudo systemctl start cloud-sql-proxy

You can now see you service running with sudo systemctl status cloud-sql-proxy.service

[Unit]
Description=Connecting MySQL Client from Compute Engine using the Cloud SQL Proxy
Documentation=https://cloud.google.com/sql/docs/mysql/connect-compute-engine
Requires=networking.service
After=networking.service
[Service]
WorkingDirectory=/usr/local/bin
ExecStart=/usr/local/bin/cloud_sql_proxy -dir=/var/run/cloud-sql-proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306
Restart=always
StandardOutput=journal
User=root
[Install]
WantedBy=multi-user.target
@ovictoraurelio
Copy link

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.

@ovictoraurelio
Copy link

Sorry, I just found a solution. For CentOS 7:

[Install]
WantedBy=multi-user.target

[Unit]
Description=Google Cloud Compute Engine SQL Proxy
Requires=network.target
After=network.target

[Service]
Type=simple
WorkingDirectory=/usr/local/bin
ExecStart=/usr/local/bin/cloud_sql_proxy -dir=/var/run/cloud-sql-proxy -instances=<instance_connection_name>=tcp:3306 -credential_file=/var/local/cloud-sql-proxy/<credential_json>.json
Restart=always
StandardOutput=journal

@bmoeskau
Copy link

I believe sudo apt-get install mysql-client should now be sudo apt-get install default-mysql-client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment