A thin MySQL command wrapper for Google Cloud SQL.
cloudsql-env is a thin wrapper that makes MySQL access to Google Cloud SQL easier from the shell.
It provides a preconfigured db command on top of mysql.
The wrapper applies a few fixed MySQL client defaults, including --get-server-public-key and --delimiter='$$'.
The $$ delimiter makes it easier to run stored routine definitions without treating semicolons inside the routine body as statement terminators.
Clone this repository into a project where you use MySQL.
Install Google Cloud SDK and Cloud SQL Auth Proxy when needed.
You also need a local MySQL client command such as mysql.
Run the installer below if gcloud is not already available on your machine.
./bin/install-google-cloud-sdk.shAfter installation, authenticate for both gcloud commands and Application Default Credentials, then choose the target project.
~/.local/google-cloud-sdk/bin/gcloud auth login
~/.local/google-cloud-sdk/bin/gcloud auth application-default login
~/.local/google-cloud-sdk/bin/gcloud config set project YOUR_PROJECT_IDRun the installer below to download the proxy into bin/ and verify it with --version.
./bin/install-cloud-sql-proxy.shFor a smaller Homebrew install, use the client-only formula.
brew install mysql-clientThis tool automatically looks for mysql in the usual Homebrew locations, so no extra setup is typically needed after installation.
If your setup uses a different location, either add it to PATH:
export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"or point this tool directly at the binary:
export MYSQL_BIN="/opt/homebrew/opt/mysql-client/bin/mysql"Load a connection script with source. . is the short form of source.
# dot form
. ./connect.sh DB_INSTANCE DB_USER
# source form
source ./connect.sh DB_INSTANCE DB_USERAdd a schema name when you want db to connect to a specific schema by default.
. ./connect.sh DB_INSTANCE DB_USER DB_SCHEMAPass --secret when you want to use a different Secret Manager secret name.
. ./connect.sh DB_INSTANCE DB_USER --secret SECRET_NAME
. ./connect.sh DB_INSTANCE DB_USER DB_SCHEMA --secret SECRET_NAMEIf --secret is omitted, the script uses this Secret Manager name by default:
cloudsql--DB_INSTANCE--DB_USER
cloudsql is the fixed prefix, and DB_INSTANCE and DB_USER are placeholders.
After that, the db command becomes available.
db -e "select schema()" # Runs SQL passed as text.
db < file.sql # Runs SQL from a file.To disconnect:
. ./disconnect.shYou can add your own local wrapper scripts on top of connect.sh when you want shortcuts for a specific instance, user, or schema.