This page describes how to connect a sqlcmd client to your Cloud SQL instance, from a client machine running Linux or a Compute Engine Linux instance, using the Cloud SQL Auth Proxy Docker image.
Before you begin
You must have:
- Installed the gcloud CLI. Learn more.
- Authorized the gcloud CLI. Learn more.
- Set the default project for the gcloud CLI. Learn more.
- Configured a database user on your Cloud SQL instance. Learn more.
Required roles
To get the permissions that you need to connect to a Cloud SQL instance using the Cloud SQL Auth Proxy Docker image, ask your administrator to grant you the following IAM roles on your project:
-
To connect using the Cloud SQL Auth Proxy:
Cloud SQL Client (
roles/cloudsql.client) -
If you use IAM database authentication:
Cloud SQL Instance User (
roles/cloudsql.instanceUser)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Connect a sqlcmd client using the Cloud SQL Auth Proxy Docker image
To connect using the Cloud SQL Auth Proxy Docker image:
Enable the Cloud SQL Admin API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.- If you are using a Compute Engine instance, prepare the instance:
-
Display the Compute Engine instance properties:
gcloud compute instances describe [GCE_INSTANCE_NAME]
- Verify the service account
and access scopes configured on the instance:
- Ensure that the service account attached to the VM has the
Cloud SQL Client (
roles/cloudsql.client) predefined Identity and Access Management (IAM) role to connect using the Cloud SQL Auth Proxy or the Cloud SQL Admin API. If you use IAM database authentication, also grant the Cloud SQL Instance User (roles/cloudsql.instanceUser) role. For more information about predefined roles, see Required roles. - Ensure that the VM access scope is set to
cloud-platform(https://www.googleapis.com/auth/cloud-platform, or Allow full access to all Cloud APIs in the Google Cloud console). Access scopes are a legacy method for specifying authorization for a VM and only set the maximum OAuth authorization scope for requests. Manage access control through predefined IAM roles instead of access scopes.
If your Compute Engine instance doesn't have the required service account roles or access scope, you can update the instance. For more information about changing the service account and access scopes on a VM, see Change the attached service account in the Compute Engine documentation.
- Ensure that the service account attached to the VM has the
Cloud SQL Client (
- Open a terminal connection to the instance, using the instructions at Connecting to Linux instances.
-
Display the Compute Engine instance properties:
-
Install the sqlcmd client on the Compute Engine instance or client
machine, if it is not already installed.
Debian/Ubuntu
For Debian/Ubuntu, install the applicable SQL Server command-line tools.
CentOS/RHEL
For CentOS/RHEL, install the applicable SQL Server command-line tools.
openSUSE
For openSUSE, install the applicable SQL Server command-line tools.
Other platforms
See the landing page for installing SQL Server, as well as the SQL Server downloads page.
-
If needed, install the Docker client:
curl https://get.docker.com | sh sudo usermod -aG docker $USER
If you are using a container-optimized Compute Engine instance, it already has the Docker client installed.
-
Install the Cloud SQL Auth Proxy Docker image from the
Google Container Registry.
docker pull gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.27.0
- If you are running the Cloud SQL Auth Proxy Docker image on a local machine (not a
Compute Engine instance), or if the service account attached to your
Compute Engine instance doesn't have the required IAM roles and
access scope, create a Google Cloud service account.
- In the Google Cloud console, go to the Service accounts page.
- Select the project that contains your Cloud SQL instance.
- Click Create service account.
- In the Service account name field, enter a descriptive name for the service account.
- Change the Service account ID to a unique, recognizable value and then click Create and continue.
-
Click the Select a role field and select one of the following roles:
- Cloud SQL > Cloud SQL Client
- Cloud SQL > Cloud SQL Editor
- Cloud SQL > Cloud SQL Admin
- Click Done to finish creating the service account.
- Click the action menu for your new service account and then select Manage keys.
- Click the Add key drop-down menu and then click Create new key.
-
Confirm that the key type is JSON and then click Create.
The private key file is downloaded to your machine. You can move it to another location. Keep the key file secure.
You provide the path to the key file as "PATH_TO_KEY_FILE" when you start the Cloud SQL Auth Proxy.
- Go to the Cloud SQL Instances page in the Google Cloud console.
-
Select the instance to open its Instance details page and copy the Instance connection name.
For example:
myproject:us-central1:myinstance. -
Start the Cloud SQL Auth Proxy.
TCP sockets
docker run -d
-v PATH_TO_KEY_FILE:/path/to/service-account-key.json
-p 127.0.0.1:1433:1433
gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.27.0
--address 0.0.0.0 --port 1433
--credentials-file /path/to/service-account-key.json INSTANCE_CONNECTION_NAMEIf you're using the credentials provided by your Compute Engine instance, don't include the
--credentials-fileparameter and the-v PATH_TO_KEY_FILE:/path/to/service-account-key.jsonline.Always specify the
127.0.0.1prefix in-pso that the Cloud SQL Auth Proxy isn't exposed outside the local host. The--address 0.0.0.0flag is required to make the port accessible from outside of the Docker container. - Start the client:
The connection string you use depends on whether you started the Cloud SQL Auth Proxy using a TCP socket or Docker.
TCP sockets
- Start the sqlcmd client:
sqlcmd -S tcp:127.0.0.1,1433 -U USERNAME -P PASSWORD
When you connect using TCP sockets, the Cloud SQL Auth Proxy is accessed through
127.0.0.1. - If prompted, enter the password.
- The sqlcmd prompt appears.
- Start the sqlcmd client:
Keep the Cloud SQL Auth Proxy Docker image up to date
The Cloud SQL Auth Proxy Docker image is based on a specific version of the Cloud SQL Auth Proxy. When a new version of the Cloud SQL Auth Proxy becomes available, pull the new version of the Cloud SQL Auth Proxy Docker image to keep your environment up to date. You can see the current version of the Cloud SQL Auth Proxy by checking the Cloud SQL Auth Proxy GitHub releases page.
What's next
- Learn more about the Cloud SQL Auth Proxy.
- Get help troubleshooting connection issues for the Cloud SQL Auth Proxy.
- Create users and databases.
- Learn about options for connecting to your instance from your application.
- Learn about Docker.
- Learn about the Google Container Registry.
- Learn about options for support.