使用 Azure CLI 監視和調整 Azure SQL Database 中的單一資料庫Use the Azure CLI to monitor and scale a single database in Azure SQL Database

適用於: Azure SQL Database

此 Azure CLI 指令碼範例會在查詢 Azure SQL Database 中的單一資料庫的大小資訊後,將其調整為不同的計算大小。This Azure CLI script example scales a single database in Azure SQL Database to a different compute size after querying the size information of the database.

如果您選擇在本機安裝和使用 Azure CLI,本文會要求您執行 Azure CLI 2.0 版或更新版本。If you choose to install and use the Azure 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

登入 AzureSign in to Azure

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

$subscription = "<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

執行指令碼Run the script

#!/bin/bash
location="East US"
randomIdentifier=random123

resource="resource-$randomIdentifier"
server="server-$randomIdentifier"
database="database-$randomIdentifier"

login="sampleLogin"
password="samplePassword123!"

echo "Using resource group $resource with login: $login, password: $password..."

echo "Creating $resource..."
az group create --name $resource --location "$location"

echo "Creating $server on $resource..."
az sql server create --name $server --resource-group $resource --location "$location" --admin-user $login --admin-password $password

echo "Creating $database on $server..."
az sql db create --resource-group $resource --server $server --name $database --edition GeneralPurpose --family Gen4 --capacity 1 

echo "Monitoring size of $database..."
az sql db list-usages --name $database --resource-group $resource --server $server

echo "Scaling up $database..." # create command executes update if database already exists
az sql db create --resource-group $resource --server $server --name $database --edition GeneralPurpose --family Gen4 --capacity 2

提示

使用 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 $resource

範例參考Sample reference

此指令碼會使用下列命令。This script uses the following commands. 下表中的每個命令都會連結至命令特定的文件。Each command in the table links to command-specific documentation.

指令碼Script 描述Description
az sql serveraz sql server 伺服器命令。Server commands.
az sql db show-usageaz sql db show-usage 顯示資料庫的大小使用量資訊。Shows the size usage information for a database.

後續步驟Next steps

如需 Azure CLI 的詳細資訊,請參閱 Azure CLI 文件For more information on the Azure CLI, see Azure CLI documentation.

您可以在 Azure CLI 範例指令碼中找到其他的 CLI 指令碼範例。Additional CLI script samples can be found in Azure CLI sample scripts.