Cloud SQL
Last updated
Was this helpful?
Last updated
Was this helpful?
is managed MySQL, PostgreSQL, and SQL Server. Cloud SQL automates backups, replication, and failover to ensure your database is reliable, highly available.
Cloud SQL has automatic data encryption at rest and in transit. Private connectivity with Virtual Private Cloud (VPC) and user-controlled network access that includes firewall protection. Compliant with SSAE 16, ISO 27001, PCI DSS v3.0, and HIPAA
Create a new Cloud SQL - MySQL Instance.
Create a new database inside of the MySQL database instance.
By default, every database instance has a public IP address. However, the instance is not publicly accessible because it's protected by the firewall.
To easily connect to the database instance from command line:
Connect to the MySQL instance using gcloud
CLI.
From the command line connection, you can use the client to create a table for the corresponding database. For example:
You can add a user using gcloud
command line:
Use gcloud
command line to create a new user:
Every Cloud SQL Instance has a unique instance connection name for the form of PROJECT_ID:REGION:INSTANCE_NAME
.
Find the Instance Connection Name using gcloud
command line:
MySQL instance's Instance Connection Name
There are different ways to connect to a Cloud SQL instance. All methods will configure a JDBC URL to allow you to use the corresponding JDBC Driver, and subsequently, JPA / Hibernate and Spring Data.
Method
MySQL
PostgreSQL
SQL Server
Considerations
Cloud SQL Starter
✅
✅
🚫
Easy to configure for Spring Boot projects.
Cloud SQL Socket Factory
✅
✅
🚫
Works with non Spring Boot projects.
Cloud SQL Proxy
✅
✅
✅
Offloads authentication to proxy.
VPC Private IP
✅
✅
✅
Access via VPC. Can be used with all of the other methods above.
Cloud SQL starter will automatically:
Configure the JDBC URL for the corresponding database instance.
Add the Cloud SQL Starter dependency:
Maven:
Gradle:
In addition to the JDBC Driver dependency, add the Cloud SQL Socket Factory dependency:
Maven:
Gradle:
MySQL instance's JDBC URL with Cloud SQL Socket Factory follows the format of:
The JDBC URL for the Cloud SQL instance in this example is:
Install Cloud SQL Proxy:
Start the proxy:
You can then establish connections on localhost
with the corresponding ports.
Connect with mysql
CLI:
Or, connect with JDBC using JDBC URL:
You can use R2DBC driver for reactive database access when you connect to Cloud SQL instances using:
Cloud SQL Proxy
VPC Private IP
Using R2DBC Cloud SQL Connector
You can use R2DBC Cloud SQL Connector that automatically exchanges the certificates like the Cloud SQL Socket Factory.
You need the installed locally first, so that you can use mysql
to connect to any MySQL server.
You need the installed locally first, so that you can use psql
to connect to any PostgreSQL server.
You need the installed locally first, so that you can use mssql-cli
to connect to any SQL Server.
You can configure Cloud SQL instances to only have , so that it's only accessible from a Virtual Private Cloud network.
The new user has no privileges. Connect to the database server and grant privileges. Refer to .
The new user has no privileges. Connect to the database server and grant privileges. Refer to .
The new user has no privileges. Connect to the database server and grant privileges. Refer to .
When using Spring Boot, you can use .
Add dependency to the corresponding JDBC driver, and the . You do not need to add those dependency separately.
Configure Spring Boot application'sapplication.properties
with and the database name:
If you don't use Spring Cloud GCP's Cloud SQL starter, and need to configure JDBC URL directly, you can use with existing JDBC driver.
Different MySQL Socket Factory artifact is needed for different MySQL Connector/J versions. See for more information.
is the generic way of establishing secured connection to a Cloud SQL instance. Rather than using the Cloud SQL Socket Factory to exchange certificates, Cloud SQL Proxy will authenticate and exchange the certificates.
You can optionally configure Cloud SQL Proxy to expose not a TCP IP port, but using Unix Socket Domain instead, and configure the Cloud SQL Socket Factory to connect using the Unix Socket Domain. See for more details.
If your Cloud SQL instance is on , and your application is running in the Cloud able to access the same VPC, then configure JDBC drivers normally connecting to the private IP address.
You can use standard R2DBC driver to connect using the IP address. See for corresponding driver usages:
See for more information on configuring the R2DBC driver for Cloud SQL.