Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cloudsql-env

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.

Installation

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.

Google Cloud SDK

Run the installer below if gcloud is not already available on your machine.

./bin/install-google-cloud-sdk.sh

After 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_ID

Cloud SQL Auth Proxy

Run the installer below to download the proxy into bin/ and verify it with --version.

./bin/install-cloud-sql-proxy.sh

MySQL client

For a smaller Homebrew install, use the client-only formula.

brew install mysql-client

This 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"

Usage

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_USER

Add a schema name when you want db to connect to a specific schema by default.

. ./connect.sh DB_INSTANCE DB_USER DB_SCHEMA

Pass --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_NAME

If --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.sh

Examples

You can add your own local wrapper scripts on top of connect.sh when you want shortcuts for a specific instance, user, or schema.

About

A thin MySQL command wrapper for Google Cloud SQL.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages