使用 Azure CLI 监视和缩放用于 MySQL 服务器的 Azure 数据库Monitor and scale an Azure Database for MySQL server using Azure CLI

此示例 CLI 脚本在查询指标后为单个 Azure Database for MySQL 服务器缩放计算和存储。This sample CLI script scales compute and storage for a single Azure Database for MySQL server after querying the metrics. 计算可以增加或减少。Compute can scale up or down. 存储只能增加。Storage can only scale up.

备注

将要查看的是 Azure Database for MySQL 的新服务。You are viewing the new service of Azure Database for MySQL. 若要查看经典 MySQL Database for Azure 的文档,请访问此页To view the documentation for classic MySQL Database for Azure, please visit this page.

先决条件Prerequisites

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the Azure CLI.

    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.

  • 本文需要 Azure CLI 版本 2.0 或更高版本。This article requires version 2.0 or later of the Azure CLI.

示例脚本Sample script

使用你的订阅 ID 更新脚本。Update the script with your subscription ID.

#!/bin/bash

# Set up variables
RESOURCE_GROUP="myresourcegroup"
SERVER_NAME="mydemoserver-$RANDOM"
PASSWORD=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 12) 
echo password $PASSWORD # returns the generated password
LOCATION="chinaeast2"
ADMIN_USER="myadmin"
SUBSCRIPTION_ID="" # enter your subscription ID

# Create a resource group
az group create \
    --name $RESOURCE_GROUP \
    --location $LOCATION

# Create a MySQL server in the resource group
az mysql server create \
    --name $SERVER_NAME \
    --resource-group $RESOURCE_GROUP \
    --location $LOCATION \
    --admin-user $ADMIN_USER \
    --admin-password $PASSWORD \
    --sku-name GP_Gen5_2

# Monitor usage metrics - CPU
az monitor metrics list \
    --resource "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.DBforMySQL/servers/$SERVER_NAME" \
    --metric cpu_percent \
    --interval PT1M

# Monitor usage metrics - Storage
az monitor metrics list \
    --resource "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.DBforMySQL/servers/$SERVER_NAME" \
    --metric storage_used \
    --interval PT1M

# Scale up the server by provisionining more vCores within the same tier
az mysql server update \
    --resource-group $RESOURCE_GROUP \
    --name $SERVER_NAME \
    --sku-name GP_Gen5_4

# Scale down the server by provisioning fewer vCores within the same tier
az mysql server update \
    --resource-group $RESOURCE_GROUP \
    --name $SERVER_NAME \
    --sku-name GP_Gen5_2

# Scale up the server to provision a storage size of 7GB
# Storage size cannot be reduced
az mysql server update \
    --resource-group $RESOURCE_GROUP \
    --name $SERVER_NAME \
    --storage-size 7168

清理部署Clean up deployment

运行脚本示例后,请使用以下命令删除资源组以及与其关联的所有资源。Use the following command to remove the resource group and all resources associated with it after the script has been run.

#!/bin/bash
RESOURCE_GROUP = "" # enter your resource group name
az group delete --name $RESOURCE_GROUP

脚本说明Script explanation

此脚本使用下表中列出的命令:This script uses the commands outlined in the following table:

命令Command 说明Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az mysql server createaz mysql server create 创建用于托管数据库的 MySQL 服务器。Creates a MySQL server that hosts the databases.
az mysql server updateaz mysql server update 更新 MySQL 服务器的属性。Updates properties of the MySQL server.
az monitor metrics listaz monitor metrics list 列出资源的指标值。List the metric value for the resources.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps