Skip to content

Instantly share code, notes, and snippets.

@goodwill
Last active December 15, 2023 06:37
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save goodwill/a981c2912ae6a83761a624f657f34d9f to your computer and use it in GitHub Desktop.
Save goodwill/a981c2912ae6a83761a624f657f34d9f to your computer and use it in GitHub Desktop.
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
@arnoldj-devops
Copy link

I have created a wrapper for CloudSQL proxy
https://github.com/arnoldj-devops/cloudsql-proxy-pal

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