Quickstart: Integrate Azure Spring Apps with Azure Database for MySQL
Note
The Basic, Standard, and Enterprise plans will be deprecated starting from mid-March, 2025, with a 3 year retirement period. We recommend transitioning to Azure Container Apps. For more information, see the Azure Spring Apps retirement announcement.
The Standard consumption and dedicated plan will be deprecated starting September 30, 2024, with a complete shutdown after six months. We recommend transitioning to Azure Container Apps. For more information, see Migrate Azure Spring Apps Standard consumption and dedicated plan to Azure Container Apps.
This article applies to: ✅ Basic/Standard ❎ Enterprise
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.
Prerequisites
An Azure account with an active subscription. Create an account for free.
Create an Azure Database for MySQL instance
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. Use single quotes around the value for admin-password
.
az mysql flexible-server create \
--resource-group <resource-group-name> \
--name <MySQL-flexible-server-name> \
--database-name <database-name> \
--public-access 0.0.0.0 \
--admin-user <admin-username> \
--admin-password '<admin-password>'
Note
The Standard_B1ms
SKU is used by default. For pricing details, see Azure Database for MySQL pricing.
Tip
The 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.).
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.
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
Run the
az spring connection create
command to create a service connection between thecustomers-service
app and the Azure MySQL database. Replace the placeholders for the following settings with your own information. Use single quotes around the value for MySQL serversecret
.Setting Description --connection
The name of the connection that identifies the connection between your app and target service. --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= secret=
The MySQL server username and password. az spring connection create mysql-flexible \ --resource-group <Azure-Spring-Apps-resource-group-name> \ --service <Azure-Spring-Apps-resource-name> \ --app customers-service \ --connection <mysql-connection-name-for-app> \ --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 runningaz extension add --name spring
.
Repeat these steps to create connections for the customers-service
, vets-service
, and visits-service
applications.
Check connection to MySQL database
Run the az spring connection validate
command to show the status of the connection between the customers-service
app and the Azure MySQL database. Replace the placeholders with your own information.
az spring connection validate \
--resource-group <Azure-Spring-Apps-resource-group-name> \
--service <Azure-Spring-Apps-resource-name> \
--app customers-service \
--connection <mysql-connection-name-for-app> \
--output table
The following output is displayed:
Name Result Description
------------------------------------ -------- -------------
Target resource existence validated. success
Target service firewall validated. success
Username and password validated. success
Tip
To get more details about the connection between your services, remove --output table
from the above command.
Repeat these instructions to validate the connections for the customers-service
, vets-service
, and visits-service
applications.
Update apps to use MySQL profile
The following section explains how to update the apps to connect to the MySQL database.
Use the following command to set an environment variable to activate the mysql
profile for the customers-service
app:
az spring app update \
--resource-group <Azure-Spring-Apps-resource-group-name> \
--service <Azure-Spring-Apps-resource-name> \
--name customers-service \
--env SPRING_PROFILES_ACTIVE=mysql
Repeat these instructions to update app configuration for the customers-service
, vets-service
, and visits-service
applications.
Validate the apps
To validate the Pet Clinic service and to query records from the MySQL database to confirm the database connection, follow the instructions in the Verify the services section of Quickstart: Build and deploy apps to Azure Spring Apps.
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>