Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example Systemd file for starting cloud sql proxy at system start
  1. We assume you already read this: https://cloud.google.com/sql/docs/mysql/connect-external-app#proxy and created a service account for Cloud SQL Client
  2. Download cloud_sql_proxy to /usr/local/bin
  3. Make the following folders, all chown to root:root :
  4. /var/run/cloud-sql-proxy
  5. /var/local/cloud-sql-proxy
  6. Copy downloaded credential json file inside /var/local/cloud-sql-proxy, make sure only root can read as it is credential for connection.
  7. Copy above gist to /lib/systemd/system/cloud-sql-proxy.service
  8. Run systemctl daemon-reload
  9. Run systemctl start cloud-sql-proxy
  10. Profit$$$$
[Install]
WantedBy=multi-user.target
[Unit]
Description=Google Cloud Compute Engine SQL Proxy
Requires=networking.service
After=networking.service
[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:5432 -credential_file=/var/local/cloud_sql_proxy/<credential_json>.json
Restart=always
StandardOutput=journal
User=root
@larryzoumas

This comment has been minimized.

Copy link

@larryzoumas larryzoumas commented Jun 6, 2018

This is cool thank you... but small typo in cloud-sql-proxy.service

-credential_file=/var/local/cloud_sql_proxy/<credential_json>.json

should be:

-credential_file=/var/local/cloud-sql-proxy/<credential_json>.json

@jgngo

This comment has been minimized.

Copy link

@jgngo jgngo commented Jan 14, 2019

For cloud sql proxy to auto-start:

$ sudo systemctl enable cloud-sql-proxy.service
@jgngo

This comment has been minimized.

Copy link

@jgngo jgngo commented Jan 29, 2019

For Centos 7, use the following:

[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
@joeswann

This comment has been minimized.

Copy link

@joeswann joeswann commented Jul 24, 2019

If you find that /var/run/cloud-sql-proxy gets deleted on reboot - add this under line 10

RuntimeDirectory=cloud-sql-proxy
@tbrown-payments

This comment has been minimized.

Copy link

@tbrown-payments tbrown-payments commented May 6, 2021

This works for RHEL8/Centos8

[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=prod-modpress-prj:northamerica-northeast1:prod-modpress-inst=tcp:3306
Restart=always
StandardOutput=journal
User=root
@brokeyourbike

This comment has been minimized.

Copy link

@brokeyourbike brokeyourbike commented Jun 10, 2021

For ubuntu 20.04 this should work:

[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
User=root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment