This page provides an overview of the ways in which you can connect to your Cloud SQL instance and describes the available authentication and authorization options.
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, or make it both publicly and privately accessible?
- 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 following sections, 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 connector libraries for Java and Python - 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.
- Built-in database authentication - log in with a username/password set in the database engine.
Use the information that follows 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. For more information, see Manage users with built-in authentication.
How to connect to Cloud SQL
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.
Public and private IP
In Cloud SQL, public IP means that the instance is accessible through the public internet. In contrast, instances using only private IP are not accessible through the public internet, but are accessible through a Virtual Private Cloud (VPC). Cloud SQL instances can have both a public and a private IP address.
Private IP
A private IP is an IPv4 address that's accessible on a Virtual Private Cloud (VPC).
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 because they don't require traversing the internet. Optionally, you can require that all connections use either the Cloud SQL proxy or self-managed SSL certificates.
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.
For private IP paths, the following services and applications connect directly to your instance through Serverless VPC Access:
- App Engine standard environment
- App Engine flexible environment
- Cloud Run functions
- Cloud Run
Learn more about using private IP with Cloud SQL
For instructions on adding a private IP to your instance, see one of the following:
Public IP
A public IP is an IPv4 address that's 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.
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.
Configuring your instance with a public IP is best when connecting from a client that doesn't meet the requirements for a VPC.
For instructions about adding a public IP to your instance, see Configuring Public IP Connectivity.
For information about connecting a sqlcmd client to a Cloud SQL instance using public IP, see Connecting using a database client.
Connection options for instances
This section contains features that Cloud SQL supports for connecting to instances. The section also contains supported connection options for creating, deactivating, and enabling instances.
Supported features
The following table lists the features that Cloud SQL supports for connecting to instances by using private connections.
Feature | Instance with private services access only | Instance with Private Service Connect only | Instance with both private services access and Private Service Connect |
---|---|---|---|
Connect from multiple VPCs | Not supported. | Supported. | Supported by using the Private Service Connect endpoint. |
External replicas | Supported. | Not supported. | Supported by using outbound connectivity for private services access. |
Write endpoint | Supported. | Not supported. | Supported for private services access. |
Change the associated VPC network for private services access | Supported. | Not applicable. | Not supported for private services access because the instance has Private Service Connect enabled for it. Not applicable for Private Service Connect. |
Visibility of the client IP address to Cloud SQL | Supported. | Not supported. | Supported by using the private services access IP address. Not supported by using the Private Service Connect endpoint. |
Linked servers | Supported. | Not supported. | Supported by using the private services access IP address. |
Managed Active Directory | Supported. | Not supported. | Supported for private services access. |
Connection options for creating instances
Cloud SQL supports private IP and public IP configurations. You can choose connection options based on your application requirements.
You can configure private IP to keep your Cloud SQL instances private within a VPC network. The following types of private connections are supported:
- Private services access: connect to Cloud SQL instances from a single VPC network
- Private Service Connect: connect to Cloud SQL instances from multiple VPC networks that belong to different groups, teams, projects, or organizations
If you want your instance to access connections from clients both inside and outside of Google Cloud's network over the public internet, then configure public IP for the instance.
Cloud SQL supports the following connection options for creating instances:
- Private services access only
- Private Service Connect only
- Public IP only
- Both private services access and Private Service Connect
- Both public IP and private services access
- Public IP, private services access, and Private Service Connect
Deactivate connection options
Cloud SQL supports deactivating the following connection options for instances:
- Public IP on an instance with both private services access and public IP
- Public IP on an instance with public IP, private services access, and Private Service Connect
- Private Service Connect on an instance with both Private Service Connect and private services access
- Private Service Connect on an instance with Private Service Connect, private services access, and public IP
Enable connection options
Cloud SQL supports enabling the following connection options for instances:
- Private services access on an instance with public IP only
- Private Service Connect on an instance with private services access only
- Private Service Connect on an instance with both private services access and public IP
- Public IP on an instance with private services access only
Limitations
- Creating an instance with both public IP and Private Service Connect isn't supported.
- Deactivating private services access on an instance with both private services access and Private Service Connect isn't supported.
- Deactivating private services access on an instance with both private services access and public IP isn't supported.
- If you have an instance that uses only public IP, then enabling both private services access and Private Service Connect together isn't supported. First, enable private services access, and then enable Private Service Connect.
- IP-based allowlisting by using authorized networks isn't supported for instances that have Private Service Connect enabled for them.
How to authorize to Cloud SQL
Cloud SQL Language Connectors
Cloud SQL Language Connectors are client libraries that provide encryption and IAM authorization when connecting to a Cloud SQL instance. Cloud SQL recommends using the Cloud SQL Language Connectors to connect to your Cloud SQL instance over other connection options.
You can use these libraries directly from their supported programming language. They provide the same authentication as the Cloud SQL Auth Proxy without requiring an external process. This provides improved security and reduced configuration requirements to connect to Cloud SQL. Cloud SQL Language Connectors also use the same code when connecting using a public IP address or a private IP address.
To get started, see About the Cloud SQL Language Connectors.
Cloud SQL Auth Proxy
The Cloud SQL Auth Proxy lets you authorize and secure your connections by 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's authorized for a Cloud SQL instance. For more details about how the Cloud SQL Auth Proxy works, see About the Cloud SQL Auth Proxy.
Using the Cloud SQL Auth Proxy is the recommended method for authenticating connections to a Cloud SQL instance because it's 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.
Some environments 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 Run 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's 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's 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 might 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 allowed only if the connection comes 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.
How to authenticate to Cloud SQL
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 to Cloud SQL
The following table contains some options for connecting to Cloud SQL:
Connection option | More information |
---|---|
Cloud SQL Auth Proxy | |
gcloud CLI | |
Cloud SQL language connectors | |
Cloud Shell | |
Cloud Code | |
Connect using third-party database administration tools | |
SQL Server Management Studio | |
SSMS 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 INSTANCE_CONNECTION_NAME &
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.
Note:
- CLOUD_SQL_CONNECTION_NAME should be represented as <MY-PROJECT>:<INSTANCE-REGION>:<INSTANCE-NAME>
- Using the argument ipTypes=PRIVATE will force the SocketFactory to connect with an instance's associated private IP
- See the JDBC socket factory version requirements for the pom.xml file here .
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.
Troubleshoot
If you're 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 from a local machine or Compute Engine.
- Learn about configuring IP connectivity.
- Learn about the Cloud SQL Auth Proxy.
- Learn about options for support.