Cloud SQL is a fully managed database service that makes it easy to set up, maintain, manage, and administer your relational databases on Google Cloud. You can use Cloud SQL with either Cloud SQL for MySQL or Cloud SQL for PostgreSQL.

What you'll do

What you'll need

Self-paced environment setup

Sign in to Cloud Console and create a new project or reuse an existing one. (If you don't already have a Gmail or G Suite account, you must create one.)

Note: You can easily access Cloud Console by memorizing its URL, which is console.cloud.google.com.

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Note: If you're using a Gmail account, you can leave the default location set to No organization. If you're using a G Suite account, then choose a location that makes sense for your organization.

Next, you'll need to enable billing in Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running.

Activate Cloud Shell

From the Cloud Console, click Activate Cloud Shell .

If you've never started Cloud Shell before, you'll be presented with an intermediate screen (below the fold) describing what it is. If that's the case, click Continue (and you won't ever see it again). Here's what that one-time screen looks like:

It should only take a few moments to provision and connect to Cloud Shell.

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory and runs in Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this codelab can be done with simply a browser or your Chromebook.

Once connected to Cloud Shell, you should see that you are already authenticated and that the project is already set to your project ID.

Run the following command in Cloud Shell to confirm that you are authenticated:

Note: The gcloud command-line tool is the powerful and unified command-line tool in Google Cloud. It comes preinstalled in Cloud Shell. You will notice its support for tab completion. For more information, see gcloud command-line tool overview.

gcloud config list project

Command output

[core]
project = <PROJECT_ID>

If it is not, you can set it with this command:

gcloud config set project <PROJECT_ID>

Command output

Updated property [core/project].

After Cloud Shell launches, you can use the command line to create a new Cloud SQL instance:

A Cloud SQL instance is zonal. It means it'll be running in one of the Google Cloud zones. In this codelab, you don't specify the zone or region. The command line will automatically use us-central as the region and pick a zone to host your database. For more information, see gcloud sql instances create.

Get the instance connection name in the format project-id:zone-id:instance-id by running the following command. You will use this later in configuring your Spring Boot app.

Update application-mysql.properties

Replace the content of src/main/resources/application-mysql.propertieswith the following properties. You'll need to set the instance connection name from the earlier step.

src/main/resources/application-mysql.properties

database=mysql
# Delete the rest of the original content of the file and replace with the following:
spring.cloud.gcp.sql.database-name=petclinic
spring.cloud.gcp.sql.instance-connection-name=YOUR_CLOUD_SQL_INSTANCE_CONNECTION_NAME
# Initialize the database since the newly created Cloud SQL database has no tables. The following flag is for Spring Boot 2.
spring.datasource.initialization-mode=always

Finally, enable a Cloud SQL for MySQL profile in the Spring Boot app by adding mysql to application.properties' spring.profiles.active property:

src/main/resources/application.properties

# Keep the content of the file the same
...
# In the last line, add mysql to the spring.profiles.active property
spring.profiles.active=mysql

You can start the Spring Boot app normally with the Spring Boot plugin:

$ ./mvnw -DskipTests spring-boot:run

Once the app has started, click Web Preview in the Cloud Shell toolbar, then select Preview on port 8080.

You should see the Spring Petclinic homepage again as shown here in your browser:

Note: The Spring Cloud GCP starter you added automatically configured the Cloud SQL for MySQL connection URL. By default, Cloud SQL servers are not exposed via a public IP address. The Spring Cloud GCP starter also used a special SSL socket factory that automatically established a secure connection to the Cloud SQL database server.

Add a pet owner entry.

Optional: Verify that Cloud SQL has persisted the data

You can verify that the data you entered has been persisted to Cloud SQL as shown here. Hit enter (return on Macintosh) when prompted for a password.