Use the Azure CLI to monitor and scale a single database in Azure SQL Database

APPLIES TO: 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.

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. Run az --version to find the version. If you need to install or upgrade, see Install the Azure CLI.

Sample script

Sign in to 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

Tip

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 server Server commands.
az sql db show-usage Shows the size usage information for a database.

Next steps

For more information on the Azure CLI, see Azure CLI documentation.

Additional CLI script samples can be found in Azure CLI sample scripts.