Connect an Azure Database for MySQL instance to your application in Azure Spring Apps

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: ✅ Java ✅ C#

This article applies to: ✅ Basic/Standard ✅ Enterprise

With Azure Spring Apps, you can connect selected Azure services to your applications automatically, instead of having to configure your Spring Boot application manually. This article shows you how to connect your application to your Azure Database for MySQL instance.

Prerequisites

Prepare your project

  1. In your project's pom.xml file, add the following dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-starter-jdbc-mysql</artifactId>
    </dependency>
    
  2. In the application.properties file, remove any spring.datasource.* properties.

  3. Update the current app by running az spring app deploy, or create a new deployment for this change by running az spring app deployment create.

Connect your app to the Azure Database for MySQL instance

Note

By default, Service Connectors are created at the application level. To override the connections, you can create other connections again in the deployments.

Follow these steps to configure your Spring app to connect to an Azure Database for MySQL Flexible Server with a system-assigned managed identity.

  1. Use the following command to install the Service Connector passwordless extension for the Azure CLI.

    az extension add --name serviceconnector-passwordless --upgrade
    
  2. Then, use the following command to create a user-assigned managed identity for Microsoft Entra authentication. Be sure to replace the variables in the example with actual values. For more information, see Set up Microsoft Entra authentication for Azure Database for MySQL - Flexible Server.

    export AZ_IDENTITY_RESOURCE_ID=$(az identity create \
        --name $AZURE_USER_IDENTITY_NAME \
        --resource-group $AZURE_IDENTITY_RESOURCE_GROUP \
        --query id \
        --output tsv)
    
  3. Run the az spring connection create command, as shown in the following example. Be sure to replace the variables in the example with actual values.

    az spring connection create mysql-flexible \
        --resource-group $AZURE_SPRING_APPS_RESOURCE_GROUP \
        --service $AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME \
        --app $APP_NAME \
        --target-resource-group $MYSQL_RESOURCE_GROUP \
        --server $MYSQL_SERVER_NAME \
        --database $DATABASE_NAME \
        --system-identity mysql-identity-id=$AZ_IDENTITY_RESOURCE_ID
    

Next steps

In this article, you learned how to connect an application in Azure Spring Apps to an Azure Database for MySQL instance. To learn more about connecting services to an application, see Connect an Azure Cosmos DB database to an application in Azure Spring Apps.