使用 CLI 监视和缩放单一 SQL 数据库Use CLI to monitor and scale a single SQL database

此 Azure CLI 脚本示例在查询数据库的大小信息后,将单个 Azure SQL 数据库缩放为不同的计算大小。This Azure CLI script example scales a single Azure SQL database to a different compute size after querying the size information of the database.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

如果选择在本地安装并使用 CLI,本文要求运行 Azure CLI 2.0 版或更高版本。If you choose to install and use the CLI locally, this article requires that you are running the Azure CLI version 2.0 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

示例脚本Sample script

#!/bin/bash

# set execution context (if necessary)
az account set --subscription <replace with your subscription name or id>

# Set the resource group name and location for your server
&resourceGroupName="myResourceGroup-$RANDOM"
&location="chinaeast"

# Set an admin login and password for your database
&adminlogin="ServerAdmin"
&password=`openssl rand -base64 16`
# password="<EnterYourComplexPasswordHere1>"

# The logical server name has to be unique in the system
&servername="server-$RANDOM"

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

# Create a server
az sql server create \
    --name $servername \
    --resource-group $resourceGroupName \
    --location $location \
    --admin-user $adminlogin \
    --admin-password $password

# Create a General Purpose Gen4 database with 1 vCore
az sql db create \
    --resource-group $resourceGroupName \
    --server $servername \
    --name mySampleDatabase \
    --edition GeneralPurpose \
    --family Gen4 \
    --capacity 1 

# Monitor database size
az sql db list-usages \
    --name mySampleDatabase \
    --resource-group $resourceGroupName \
    --server $servername

# Scale up database to 2 vCores (create command executes update if DB already exists)
az sql db create \
    --resource-group $resourceGroupName \
    --server $servername \
    --name mySampleDatabase \
    --edition GeneralPurpose \
    --family Gen4 \
    --capacity 2

# Echo random password
echo $password

Tip

使用 az sql db op list 获取对数据库执行的操作列表,并使用 az sql db op cancel 取消对数据库的更新操作。Use az sql db op list to get a list of operations performed on the database and use az sql db op cancel to cancel an update operation on the database.

清理部署Clean up deployment

使用以下命令删除资源组及其相关的所有资源。Use the following command to remove the resource group and all resources associated with it.

az group delete --name $resourceGroupName

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az sql server createaz sql server create 创建托管单一数据库和弹性池的 SQL 数据库服务器。Creates a SQL Database server that hosts single databases and elastic pools.
az sql db show-usageaz sql db show-usage 显示单一数据库或入池数据库的大小使用情况信息。Shows the size usage information for a single or pooled database.
az sql db updateaz sql db update 更新数据库属性(如服务层级或计算大小),或者将数据库移入、移出弹性池或在弹性池之间移动。Updates database properties (such as the service tier or compute size) or moves a database into, out of, or between elastic pools.
az group deleteaz group delete 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

其他 SQL 数据库 CLI 脚本示例可以在 Azure SQL 数据库文档中找到。Additional SQL Database CLI script samples can be found in the Azure SQL Database documentation.