使用 CLI 在 SQL 弹性池中移动 Azure SQL 数据库Use CLI to move an Azure SQL database in a SQL elastic pool

此 Azure CLI 脚本示例创建两个弹性池,将 Azure SQL 数据库从一个 SQL 弹性池移到另一个 SQL 弹性池中,然后将数据库移出弹性池,并转为单一数据库计算大小。This Azure CLI script example creates two elastic pools and moves an Azure SQL database from one SQL elastic pool into another SQL elastic pool, and then moves the database out of elastic pool to a single database compute size.

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

本主题需要运行 Azure CLI 版本 2.0 或更高版本。This topic 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 an admin login and password for your database
export adminlogin=ServerAdmin
export password=ChangeYourAdminPassword1
# The logical server name has to be unique in the system
export servername=server-$RANDOM

# Create a resource group
az group create \
    --name myResourceGroup \
    --location "China East" 

# Create a logical server in the resource group
az sql server create \
    --name $servername \
    --resource-group myResourceGroup \
    --location "China East" \
    --admin-user $adminlogin \
    --admin-password $password

# Create two pools in the logical server
az sql elastic-pools create \
    --resource-group myResourceGroup \
    --location "China East"  \
    --server $servername \
    --name myFirstPool \
    --dtu 50 \
    --database-dtu-max 20
az sql elastic-pools create \
    --resource-group myResourceGroup \
    --location "China East"  \
    --server $servername \
    --name MySecondPool \
    --dtu 50 \
    --database-dtu-max 50

# Create a database in the first pool
az sql db create \
    --resource-group myResourceGroup \
    --server $servername \
    --name mySampleDatabase \
    --elastic-pool-name myFirstPool

# Move the database to the second pool - create command updates the db if it exists
az sql db create \
    --resource-group myResourceGroup \
    --server-name $servername \
    --name mySampleDatabase \
    --elastic-pool-name mySecondPool

# Move the database to standalone S1 performance level
az sql db create \
    --resource-group myResourceGroup \
    --server $servername \
    --name mySampleDatabase \
    --service-objective S1

清理部署Clean up deployment

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

az group delete --name myResourceGroup

脚本说明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 elastic-pools createaz sql elastic-pools create 创建弹性池。Creates an elastic pool.
az sql db createaz sql db create 创建单一数据库或创建弹性池中的数据库。Creates a single database or a database in an elastic pool.
az sql db updateaz sql db update 更新数据库属性,或者将数据库移入、移出弹性池或在弹性池之间移动。Updates database properties 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.