Cloud SQL 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
Cloud SQL Instance
Enable API
gcloudservicesenablesqladmin.googleapis.com
Create an Instance
Create a new Cloud SQL - MySQL Instance.
gcloudsqlinstancescreatemysql-instance \--database-version=MYSQL_5_7 \--region=us-central1 \--cpu=2 \--memory=4G \--root-password=[CHOOSE A PASSWORD]
Create a new Cloud SQL - PostgreSQL instance.
gcloudsqlinstancescreatepostgresql-instance \--database-version=POSTGRES_11 \--region=us-central1 \--cpu=2 \--memory=4G \--root-password=[CHOOSE A PASSWORD]
Create a new Cloud SQL - SQL Server instance.
gcloudbetasqlinstancescreatesqlserver-instance \--database-version=SQLSERVER_2017_STANDARD \--region=us-central1 \--cpu=2 \--memory=4G \--root-password=[CHOOSE A PASSWORD]
Create a Database
Create a new database inside of the MySQL database instance.
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.
Cloud SQL Starter is not supported for SQL Server. Use Cloud SQL Proxy instead.
Configuration
Configure Spring Boot application'sapplication.properties with Instance Connection Name and the database name:
application.properties
# Retrieve instance connection name from the previous stepspring.cloud.gcp.sql.instance-connection-name=INSTANCE_CONNECTION_NAMEspring.cloud.gcp.sql.database-name=orders# Cloud SQL starter automatically configures the JDBC URL# Configure username/passwordspring.datasource.username=...spring.datasource.password=...# Configure connection pooling if neededspring.datasource.hikari.maximum-pool-size=10
If you don't use Spring Cloud GCP's Cloud SQL starter, and need to configure JDBC URL directly, you can use Cloud SQL Socket Factory with existing JDBC driver.
Dependency
In addition to the JDBC Driver dependency, add the Cloud SQL Socket Factory dependency:
Cloud SQL Socket Factory is not supported for SQL Server. Use Cloud SQL Proxy instead.
Cloud SQL Proxy
Cloud SQL Proxy 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.
Install Cloud SQL Proxy:
gcloudcomponentsinstallcloud_sql_proxy
Start the proxy:
# Refer to Instance Connection Name from previous sectioncloud_sql_proxy-instances=INSTANCE_CONNECTION_NAME=tcp:3306
# Refer to Instance Connection Name from previous sectioncloud_sql_proxy-instances=INSTANCE_CONNECTION_NAME=tcp:5432
# Refer to Instance Connection Name from previous sectioncloud_sql_proxy-instances=INSTANCE_CONNECTION_NAME=tcp:1433
You can then establish connections on localhost with the corresponding ports.
Connect with mysql CLI:
mysql-uroot-p
Or, connect with JDBC using JDBC URL:
jdbc:mysql://localhost/orders
Connect with psql CLI:
psql -h localhost -U postgres
Or, connect with JDBC using JDBC URL:
jdbc:postgresql://localhost/orders
Connect with mssql-cli CLI:
mssql-cli -U sqlserver
Or, connect with JDBC using JDBC URL:
jdbc:sqlserver://localhost/databaseName=orders
Unix Socket Domain
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 Connect External App documentation for more details.
VPC Private IP
If your Cloud SQL instance is on VPC and has a private IP, 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.
R2DBC
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
Cloud SQL Proxy or VPC Private IP
You can use standard R2DBC driver to connect using the IP address. See R2DBC documentation for corresponding driver usages: