This page provides a summary of options for connecting to your Cloud SQL instance.
Introduction
Using the Cloud SQL Auth proxy is the recommended method for connecting to a Cloud SQL instance. The Cloud SQL Auth proxy:
- Works with both public and private IP endpoints
- Validates connections using credentials for a user or service account
- Wraps the connection in a SSL/TLS layer that is authorized for a Cloud SQL instance
Some Google Cloud services and applications provide connections for public IP paths with encryption and authorization using the Cloud SQL Auth proxy, including:
For private IP paths, these services and applications connect directly to your instance through Serverless VPC Access.
Applications running in GKE can also connect using the Cloud SQL Auth proxy.
See the Quickstart for using the Cloud SQL Auth proxy for a basic introduction to its usage.
Overview
When considering how to connect to your Cloud SQL instance, there are many choices to keep in mind, including:
- Do you want your Cloud SQL instance to be accessible from the internet, or kept private within a virtual private cloud (VPC) network?
- Are you planning to write your own connection code, or connect using publicly available tools such as the Cloud SQL Auth proxy or a sqlcmd client?
- Do you want to require encryption through SSL/TLS or allow unencrypted traffic?
In the sections below, we discuss the options Cloud SQL provides for connecting, authorizing, and authenticating to your database.
- How to connect - which network path you use to reach your instance:
- An internal, VPC-only (Private) IP address.
- An external, internet-accessible (Public) IP address.
- How to authorize - which connections are authorized and allowed to
connect to your Cloud SQL instance:
- Cloud SQL Auth proxy and Cloud SQL language connectors - these provide access based on IAM.
- Self-managed SSL/TLS certificates - these only allow connections based on specific public keys.
- Authorized networks - a list of IP addresses allowed to connect.
- How to authenticate - the method to login to your database.
- Native database authentication - login with a username/password set in the database engine.
Use the information below to decide which connection, authorization and authentication options work best for you.
Before you start
Granting access to an application does not automatically enable a database user account to connect to the instance. Before you can connect to an instance, you must have a database user account you can connect with. For new instances, this means you must have configured the default user account. Learn more.
Connection options
Private IP
A private IP is an IPv4 or IPv6 address that is accessible on a Virtual Private Cloud (VPC).
Security considerations
You can use this address to connect from other resources with access to the VPC; connections over private IP typically provide lower latency and limited attack vectors, as they don't require traversing the internet. Optionally, you can require all connections use either the Cloud SQL proxy or self-managed SSL certificates.
Recommended use case
Configuring your instance with a private IP is preferred when connecting from a client on a resource with access to a VPC. For more information about what resources can use private IP, see Requirements for Private IP.
Learn more
Learn more about using private IP with Cloud SQL
For instructions on adding a private IP to your instance, see Configuring Private IP Connectivity[configure-private-ip].
Public IP
A public IP is an IPv4 or IPv6 address that is available externally on the public internet. This address can receive connections from devices both inside and outside of Google's network, including from locations like your home or office.
Security considerations
In order to help keep your instance secure, any connections to a Cloud SQL instance using a public IP must be authorized using either the Cloud SQL Auth proxy or authorized networks.
Recommended use case
Configuring your instance with a public IP is best when connecting from a client that doesn't meet the requirements for a VPC.
Learn more
For instructions about adding a public IP to your instance, see Configuring Public IP Connectivity.
Authorization options
The Cloud SQL Auth proxy
Security considerations
The Cloud SQL Auth proxy allows you to authorize and secure your connections using Identity and Access Management (IAM) permissions. The Cloud SQL Auth proxy validates connections using credentials for a user or service account, and wrapping the connection in a SSL/TLS layer that is authorized for a Cloud SQL instance. For more details about how the Cloud SQL Auth proxy works, see About the Cloud SQL Auth proxy.
Recommended use case
Using the Cloud SQL Auth proxy is the recommended method for authenticating connections to a Cloud SQL instance, as it is the most secure method.
The Cloud SQL Auth proxy is an open source library distributed as an executable binary. The Cloud SQL Auth proxy acts as an intermediary server that listens for incoming connections, wraps them in SSL/TLS, and then passes them to a Cloud SQL instance.
Additionally, some languages have the option of using a client library. You can use these libraries directly from the language environment; they provide the same authentication as the Cloud SQL Auth proxy without requiring an external process. To get started, see the following pages:
Finally, some environments such as Cloud Run, Cloud Functions, and App Engine provide a mechanism that connects using the Cloud SQL Auth proxy. For instructions about connecting using these environments, see one of the following:
- Connecting from Cloud Run
- Connecting from Cloud Functions
- Connecting from the App Engine standard environment
- Connecting from the App Engine flexible environment
- Connecting from Google Kubernetes Engine
Self-managed SSL/TLS certificates
Instead of using the Cloud SQL Auth proxy to encrypt your connections, it is possible to set up client/server SSL/TLS certificates that are specific to a Cloud SQL instance. These certificates are used to both validate the client and server to each other and encrypt connections between them.
It is strongly recommended to use self-managed SSL/TLS certificates to provide encryption when not using the Cloud SQL Auth proxy. Failing to do so means your data is being transmitted insecurely, and may be intercepted or inspected by a third-party.
To get started with self-managed SSL/TLS certificates, see Authorizing with SSL/TLS certificates.
Authorized networks
Unless using the Cloud SQL Auth proxy, connections to the public IP address of an instance are only allowed if the connection come from from an authorized network. Authorized networks are IP addresses or ranges that the user has specified as having permission to connect.
To get started with authorized networks, see Authorizing with Authorized Networks.
Managing database connections
Database connections consume resources on the server and the connecting application. Always use good connection management practices to minimize your application's footprint and reduce the likelihood of exceeding Cloud SQL connection limits. For more information, see Managing database connections.
Authentication options
Authentication provides access control by verifying the identity of a user. For end users, authentication is achieved when the user enters credentials (a username and password). For applications, authentication is achieved when a user's credentials are assigned to a service account.
Cloud SQL uses the database's built-in authentication that authenticates using a username and password. For more information, see creating and managing SQL Server users.
Tools for connecting
The following table contains some options for connecting to Cloud SQL:
Connection option | More information |
---|---|
Cloud SQL Auth proxy | |
gcloud command-line tool |
|
Cloud SQL language connectors | |
Cloud Shell | |
Apps Script | |
Connect using third-party database administration tools | |
SQL Server Management Studio | |
SMSS Object Explorer | |
Visual Studio |
Code samples
You can connect to the Cloud SQL Auth proxy from any language that enables you to connect to a TCP socket. Below are some code snippets from complete examples on GitHub to help you understand how they work together in your application.
Connecting with TCP
Cloud SQL Auth proxy invocation statement:
./cloud_sql_proxy -instances=INSTANCE_CONNECTION_NAME=tcp:1433 &
Python
To see this snippet in the context of a web application, view the README on GitHub.
Java
To see this snippet in the context of a web application, view the README on GitHub.
Node.js
To see this snippet in the context of a web application, view the README on GitHub.
Go
To see this snippet in the context of a web application, view the README on GitHub.
C#
To see this snippet in the context of a web application, view the README on GitHub.
Ruby
To see this snippet in the context of a web application, view the README on GitHub.
PHP
To see this snippet in the context of a web application, view the README on GitHub.
Troubleshooting
If you are having problems connecting, check the following pages for help debugging or finding solutions to known issues:
- Debugging connection issues
- Known connectivity errors
- Troubleshooting Cloud SQL Auth proxy connection
- Common connection issues
What's next
- Learn how to connect with the Quickstart for Cloud SQL for sqlserver.
- Learn best practices for managing database connections.
- Learn about IAM database authentication.
- Learn about connecting using a sqlcmd client.
- Learn about configuring IP connectivity.
- Learn about the Cloud SQL Auth proxy.
- Learn about options for support.