Bind an Azure Database for MySQL instance to your application in Azure Spring Cloud
This article applies to: ✔️ Java
With Azure Spring Cloud, you can bind select Azure services to your applications automatically, instead of having to configure your Spring Boot application manually. This article shows you how to bind your application to your Azure Database for MySQL instance.
Prerequisites
- A deployed Azure Spring Cloud instance
- An Azure Database for MySQL account
- Azure CLI
If you don't have a deployed Azure Spring Cloud instance, follow the instructions in Quickstart: Launch an application in Azure Spring Cloud by using the Azure portal to deploy your first Spring Cloud app.
Prepare your Java project
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>In the application.properties file, remove any
spring.datasource.*properties.Update the current app by running
az spring-cloud app deploy, or create a new deployment for this change by runningaz spring-cloud app deployment create.
Bind your app to the Azure Database for MySQL instance
Note the admin username and password of your Azure Database for MySQL account.
Connect to the server, create a database named testdb from a MySQL client, and then create a new non-admin account.
In the Azure portal, on your Azure Spring Cloud service page, look for the Application Dashboard, and then select the application to bind to your Azure Database for MySQL instance. This is the same application that you updated or deployed in the previous step.
Select Service binding, and then select the Create service binding button.
Fill out the form, selecting Azure MySQL as the Binding type, using the same database name you used earlier, and using the same username and password you noted in the first step.
Restart the app, and this binding should now work.
To ensure that the service binding is correct, select the binding name, and then verify its detail. The
propertyfield should look like this:spring.datasource.url=jdbc:mysql://some-server.mysql.database.azure.com:3306/testdb?useSSL=true&requireSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username=admin@some-server spring.datasource.password=abc****** spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
Next steps
In this article, you learned how to bind an application in Azure Spring Cloud to an Azure Database for MySQL instance. To learn more about binding services to an application, see Bind an Azure Cosmos DB database to an application in Azure Spring Cloud.