Quickstart: Integrate Azure Spring Apps with Azure Database for MySQL

Note

Azure Spring Apps is the new name for the Azure Spring Cloud service. Although the service has a new name, you'll see the old name in some places for a while as we work to update assets such as screenshots, videos, and diagrams.

This article applies to: ✔️ Basic/Standard tier ❌ Enterprise tier

Pet Clinic, as deployed in the default configuration Quickstart: Build and deploy apps to Azure Spring Apps, uses an in-memory database (HSQLDB) that is populated with data at startup. This quickstart explains how to provision and prepare an Azure Database for MySQL instance and then configure Pet Clinic on Azure Spring Apps to use it as a persistent database with only one command.

Prerequisites

An Azure account with an active subscription. Create an account for free.

Prepare an Azure Database for MySQL instance

  1. Create an Azure Database for MySQL flexible server using the az mysql flexible-server create command. Replace the placeholders <database-name>, <resource-group-name>, <MySQL-flexible-server-name>, <admin-username>, and <admin-password> with a name for your new database, the name of your resource group, a name for your new server, and an admin username and password.

    az mysql flexible-server create \
        --resource-group <resource-group-name> \
        --name <MySQL-flexible-server-name> \
        --database-name <database-name> \
        --admin-user <admin-username> \
        --admin-password <admin-password>
    

    Note

    Standard_B1ms SKU is used by default. Refer to Azure Database for MySQL pricing for pricing details.

    Tip

    Password should be at least eight characters long and contain at least one English uppercase letter, one English lowercase letter, one number, and one non-alphanumeric character (!, $, #, %, and so on.).

  2. A CLI prompt asks if you want to enable access to your IP. Enter Y to confirm.

Connect your application to the MySQL database

Use Service Connector to connect the app hosted in Azure Spring Apps to your MySQL database.

Note

The service binding feature in Azure Spring Apps is being deprecated in favor of Service Connector.

  1. If you're using Service Connector for the first time, start by running the command az provider register to register the Service Connector resource provider.

    az provider register --namespace Microsoft.ServiceLinker
    
  2. Run the az spring connection create command to create a service connection between Azure Spring Apps and the Azure MySQL database. Replace the placeholders below with your own information.

    Setting Description
    --resource-group The name of the resource group that contains the app hosted by Azure Spring Apps.
    --service The name of the Azure Spring Apps resource.
    --app The name of the application hosted by Azure Spring Apps that connects to the target service.
    --target-resource-group The name of the resource group with the storage account.
    --server The MySQL server you want to connect to
    --database The name of the database you created earlier.
    --secret name The MySQL server username.
    --secret The MySQL server password.
    az spring connection create mysql-flexible \
        --resource-group <Azure-Spring-Apps-resource-group-name> \
        --service <Azure-Spring-Apps-resource-name> \
        --app <app-name> \
        --target-resource-group <mySQL-server-resource-group> \
        --server <server-name> \
        --database <database-name> \
        --secret name=<username> secret=<secret>
    

    Tip

    If the az spring command isn't recognized by the system, check that you have installed the Azure Spring Apps extension by running az extension add --name spring.

Check connection to MySQL database

Run the az spring connection validate command to show the status of the connection between Azure Spring Apps and the Azure MySQL database. Replace the placeholders below with your own information.

az spring connection validate
    --resource-group <Azure-Spring-Apps-resource-group-name> \ 
    --service <Azure-Spring-Apps-resource-name> \
    --app <app-name> \
    --connection <connection-name>

The following output is displayed:

Name                                                           Result
-------------------------------------------------------------  --------
The target existence is validated                              success
The target service firewall is validated                       success
The configured values (except username/password) is validated  success

Tip

To get more details about the connection between your services, remove --output table from the above command.

Clean up resources

If you plan to continue working with subsequent quickstarts and tutorials, you might want to leave these resources in place. When no longer needed, delete the resource group by using the az group delete command, which deletes the resources in the resource group. Replace <resource-group> with the name of your resource group.

az group delete --name <resource-group>

Next steps