将 Azure Database for MySQL 实例绑定到 Azure Spring Cloud 应用程序Bind an Azure Database for MySQL instance to your Azure Spring Cloud application

可以通过 Azure Spring Cloud 将所选 Azure 服务自动绑定到应用程序,而不必手动配置 Spring Boot 应用程序。With Azure Spring Cloud, you can bind select Azure services to your applications automatically, instead of having to configure your Spring Boot application manually. 本文介绍如何将应用程序绑定到 Azure Database for MySQL 实例。This article shows you how to bind your application to your Azure Database for MySQL instance.

先决条件Prerequisites

  • 部署的 Azure Spring Cloud 实例A deployed Azure Spring Cloud instance
  • Azure Database for MySQL 帐户An Azure Database for MySQL account
  • Azure CLIAzure CLI

如果没有已部署的 Azure Spring Cloud 实例,请按照快速入门:使用 Azure 门户启动 Azure Spring Cloud 应用中的说明部署第一个 Spring Cloud 应用。If you don't have a deployed Azure Spring Cloud instance, follow the instructions in Quickstart: Launch an Azure Spring Cloud application by using the Azure portal to deploy your first Spring Cloud app.

将应用绑定到 Azure Database for MySQL 实例Bind your app to your Azure Database for MySQL instance

  1. 记下 Azure Database for MySQL 帐户的管理员用户名和密码。Note the admin username and password of your Azure Database for MySQL account.

  2. 连接到服务器,从 MySQL 客户端创建名为“testdb”的数据库,然后创建新的非管理员帐户。Connect to the server, create a database named testdb from a MySQL client, and then create a new non-admin account.

  3. 在项目的 pom.xml 文件中,添加以下依赖项: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>
    
  4. 在 application.properties 文件中,删除所有 spring.datasource.* 属性。In the application.properties file, remove any spring.datasource.* properties.

  5. 通过运行 az spring-cloud app update 更新当前部署,或者通过运行 az spring-cloud app deployment create 针对此更改创建新的部署。Update the current deployment by running az spring-cloud app update, or create a new deployment for this change by running az spring-cloud app deployment create. 这些命令会使用新的依赖项更新或创建应用程序。These commands either update or create the application with the new dependency.

  6. 在 Azure 门户的“Azure Spring Cloud”服务页上,查找“应用程序仪表板”,然后选择要绑定到 Azure Database for MySQL 实例的应用程序 。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.

  7. 选择“服务绑定”,然后选择“创建服务绑定”按钮 。Select Service binding, and then select the Create service binding button.

  8. 填充窗体,选择“Azure MySQL”作为“绑定类型”,使用以前使用的数据库名称以及在第一步记下的用户名和密码。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.

  9. 重启应用,此绑定现在应该生效。Restart the app, and this binding should now work.

  10. 若要确保服务绑定正确,请选择绑定名称,然后验证其详细信息。To ensure that the service binding is correct, select the binding name, and then verify its detail. property 字段应如下所示:The property field should look like this:

    spring.datasource.url=jdbc:mysql://some-server.mysql.database.chinacloudapi.cn: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

本文介绍了如何将 Azure Spring Cloud 应用程序绑定到 Azure Database for MySQL 实例。In this article, you learned how to bind an Azure Spring Cloud application to an Azure Database for MySQL instance. 若要详细了解如何将服务绑定到应用程序,请参阅将 Azure Cosmos DB 数据库绑定到 Azure Spring Cloud 应用程序To learn more about binding services to an application, see Bind an Azure Cosmos DB database to an Azure Spring Cloud application.