GoogleCloudPlatform/cloudsql-proxy

Name: cloudsql-proxy

Owner: Google Cloud Platform

Description: null

Created: 2015-10-01 23:13:02.0

Updated: 2018-05-24 03:30:41.0

Pushed: 2018-05-17 14:54:13.0

Homepage: null

Size: 209

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Cloud SQL Proxy

The Cloud SQL Proxy allows a user with the appropriate permissions to connect to a Second Generation Cloud SQL database without having to deal with IP whitelisting or SSL certificates manually. It works by opening unix/tcp sockets on the local machine and proxying connections to the associated Cloud SQL instances when the sockets are used.

To build from source, ensure you have go installed and have set GOPATH. Then, simply do a go get:

go get github.com/GoogleCloudPlatform/cloudsql-proxy/cmd/cloud_sql_proxy

The cloud_sql_proxy will be placed in $GOPATH/bin after go get completes.

cloud_sql_proxy takes a few arguments to configure:

Note: -instances and -instances_metadata may be used at the same time but are not compatible with the -fuse flag.

By default, the proxy will authenticate under the default service account of the Compute Engine VM it is running on. Therefore, the VM must have at least the sqlservice.admin API scope (“https://www.googleapis.com/auth/sqlservice.admin”) and the associated project must have the SQL Admin API enabled. The default service account must also have at least WRITER/EDITOR priviledges to any projects of target SQL instances.

Specifying the -credential_file flag allows use of the proxy outside of Google's cloud. Simply create a new service account, download the associated JSON file, and set -credential_file to the path of the JSON file. You can also set the GOOGLE_APPLICATION_CREDENTIALS environment variable instead of passing this flag.

Example invocations:
./cloud_sql_proxy -dir=/cloudsql -instances=my-project:us-central1:sql-inst &
mysql -u root -S /cloudsql/my-project:us-central1:sql-inst

# For -fuse you do not need to specify instance names ahead of time:
./cloud_sql_proxy -dir=/cloudsql -fuse &
mysql -u root -S /cloudsql/my-project:us-central1:sql-inst

# For programs which do not support using Unix Domain Sockets, specify tcp:
./cloud_sql_proxy -dir=/cloudsql -instances=my-project:us-central1:sql-inst=tcp:3306 &
mysql -u root -h 127.0.0.1
To use inside a Go program:

If your program is written in Go you can use the Cloud SQL Proxy as a library, avoiding the need to start the Proxy as a companion process.

MySQL

If you're using the the MySQL go-sql-driver you can use helper functions found in the proxy/dialers/mysql package. See example usage.

Postgres

If you're using the the Postgres lib/pq, you can use the cloudsqlpostgres driver from here. See example usage.

I'm open to adding more drivers, feel free to file an issue.

To use from Kubernetes:
Deploying Cloud SQL Proxy as a sidecar container

Follow this page. See also Connecting from Google Kubernetes Engine.

Deploy Cloud SQL Proxy as a Cluster Service using Helm

Follow this instruction. This chart creates a Deployment and a Service, but we recommend deploying the proxy as a sidecar container in your pods.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.