Skip to content

Latest commit

 

History

History
147 lines (95 loc) · 8.71 KB

configure-spring-data-jdbc-with-azure-mysql.md

File metadata and controls

147 lines (95 loc) · 8.71 KB
titledescriptionauthorms.datems.authorms.reviewerms.topicms.customzone_pivot_group_filenamezone_pivot_groups
Use Spring Data JDBC with Azure Database for MySQL
Learn how to use Spring Data JDBC with an Azure Database for MySQL database.
KarlErickson
08/28/2024
karler
seal
article
devx-track-java, devx-track-azurecli, team=cloud_advocates, passwordless-java, spring-cloud-azure, devx-track-extended-java
java/java-zone-pivot-groups.json
passwordless-mysql

Use Spring Data JDBC with Azure Database for MySQL

This tutorial demonstrates how to store data in Azure Database for MySQL database using Spring Data JDBC.

JDBC is the standard Java API to connect to traditional relational databases.

In this tutorial, we include two authentication methods: Microsoft Entra authentication and MySQL authentication. The Passwordless tab shows the Microsoft Entra authentication and the Password tab shows the MySQL authentication.

Microsoft Entra authentication is a mechanism for connecting to Azure Database for MySQL using identities defined in Microsoft Entra ID. With Microsoft Entra authentication, you can manage database user identities and other Microsoft services in a central location, which simplifies permission management.

MySQL authentication uses accounts stored in MySQL. If you choose to use passwords as credentials for the accounts, these credentials will be stored in the user table. Because these passwords are stored in MySQL, you need to manage the rotation of the passwords by yourself.

[!INCLUDE spring-data-prerequisites.md]

  • MySQL command line client.

  • If you don't have a Spring Boot application, create a Maven project with the Spring Initializr. Be sure to select Maven Project and, under Dependencies, add the Spring Web, Spring Data JDBC, and MySQL Driver dependencies, and then select Java version 8 or higher.

::: zone pivot="mysql-passwordless-flexible-server"

See the sample application

In this tutorial, you'll code a sample application. If you want to go faster, this application is already coded and available at https://github.com/Azure-Samples/quickstart-spring-data-jdbc-mysql.

[!INCLUDE spring-data-azure-mysql-flexible-server-setup.md]

Configure Spring Boot to use Azure Database for MySQL

To store data from Azure Database for MySQL using Spring Data JDBC, follow these steps to configure the application:

  1. Configure Azure Database for MySQL credentials by adding the following properties to your application.properties configuration file.

    logging.level.org.springframework.jdbc.core=DEBUG spring.datasource.url=jdbc:mysql://mysqlflexibletest.mysql.database.azure.com:3306/demo?serverTimezone=UTC spring.datasource.username=<your_mysql_ad_non_admin_username> spring.datasource.azure.passwordless-enabled=true spring.sql.init.mode=always
    logging.level.org.springframework.jdbc.core=DEBUG spring.datasource.url=jdbc:mysql://mysqlflexibletest.mysql.database.azure.com:3306/demo?serverTimezone=UTC spring.datasource.username=<your_mysql_non_admin_username> spring.datasource.password=<your_mysql_non_admin_password> spring.sql.init.mode=always

    [!WARNING] The configuration property spring.sql.init.mode=always means that Spring Boot will automatically generate a database schema, using the schema.sql file that you'll create next, each time the server is started. This feature is great for testing, but remember that it will delete your data at each restart, so you shouldn't use it in production.

    The configuration property spring.datasource.url has ?serverTimezone=UTC appended to tell the JDBC driver to use the UTC date format (or Coordinated Universal Time) when connecting to the database. Without this parameter, your Java server wouldn't use the same date format as the database, which would result in an error.

::: zone-end

::: zone pivot="mysql-passwordless-single-server"

See the sample application

In this article, you'll code a sample application. If you want to go faster, this application is already coded and available at https://github.com/Azure-Samples/quickstart-spring-data-jdbc-mysql.

[!INCLUDE spring-data-azure-mysql-single-server-setup.md]

Configure Spring Boot to use Azure Database for MySQL

To store data from Azure Database for MySQL using Spring Data JDBC, follow these steps to configure the application:

  1. Configure Azure Database for MySQL credentials by adding the following properties to your application.properties configuration file.

    logging.level.org.springframework.jdbc.core=DEBUG spring.datasource.url=jdbc:mysql://mysqlsingletest.mysql.database.azure.com:3306/demo?serverTimezone=UTC spring.datasource.username=<your_mysql_ad_non_admin_username>@mysqlsingletest spring.datasource.azure.passwordless-enabled=true spring.sql.init.mode=always
    logging.level.org.springframework.jdbc.core=DEBUG spring.datasource.url=jdbc:mysql://mysqlsingletest.mysql.database.azure.com:3306/demo?serverTimezone=UTC spring.datasource.username=<your_mysql_non_admin_username>@mysqlsingletest spring.datasource.password=<your_mysql_non_admin_password> spring.sql.init.mode=always

    [!WARNING] The configuration property spring.sql.init.mode=always means that Spring Boot will automatically generate a database schema, using the schema.sql file that you'll create next, each time the server is started. This feature is great for testing, but remember that it will delete your data at each restart, so you shouldn't use it in production.

    The configuration property spring.datasource.url has ?serverTimezone=UTC appended to tell the JDBC driver to use the UTC date format (or Coordinated Universal Time) when connecting to the database. Without this parameter, your Java server wouldn't use the same date format as the database, which would result in an error.

::: zone-end

  1. Create the src/main/resources/schema.sql configuration file to configure the database schema, then add the following contents.

    DROPTABLE IF EXISTS todo; CREATETABLEtodo (id SERIALPRIMARY KEY, description VARCHAR(255), details VARCHAR(4096), done BOOLEAN);

[!INCLUDE spring-data-jdbc-create-application.md]

[!INCLUDE deploy-to-azure-spring-apps]

Next steps

[!div class="nextstepaction"] Azure for Spring developersSpring Cloud Azure MySQL Samples

close