使用 Azure CLI 监视和缩放 Azure Database for MariaDB 服务器Monitor and scale an Azure Database for MariaDB server using Azure CLI

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

本文需要 Azure CLI 2.0 或更高版本。This article requires Azure CLI version 2.0 or later. 通过运行 az --version 来查看版本。Check the version by running az --version. 请参阅安装 Azure CLI,了解如何安装或升级 Azure CLI 的版本。See Install Azure CLI to install or upgrade your version of 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 MariaDB server in the resource group
az mariadb 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.DBforMariaDB/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.DBforMariaDB/servers/$SERVER_NAME" \
    --metric storage_used \
    --interval PT1M

# Scale up the server by provisionining more vCores within the same tier
az mariadb 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 mariadb 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 mariadb 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
az group delete --name myresourcegroup

脚本说明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 mariadb server createaz mariadb server create 创建用于托管数据库的 MariaDB 服务器。Creates a MariaDB server that hosts the databases.
az mariadb server updateaz mariadb server update 更新 MariaDB 服务器的属性。Updates properties of the MariaDB 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