使用 CLI 创建单一 Azure SQL 数据库并配置防火墙规则Use CLI to create a single Azure SQL database and configure a firewall rule

此 Azure CLI 脚本示例创建 Azure SQL 数据库,并配置服务器级防火墙规则。This Azure CLI script example creates an Azure SQL database and configure a server-level firewall rule. 成功运行该脚本后,可以通过所有 Azure 服务和配置的 IP 地址访问 SQL 数据库。Once the script has been successfully run, the SQL Database can be accessed from all Azure services and the configured IP address.

如果没有 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
# The ip address range that you want to allow to access your DB
export startip=0.0.0.0
export endip=0.0.0.0

# Create a resource group
az group create \
    --name myResourceGroup \
    --location chinaeast

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

# Configure a firewall rule for the server
az sql server firewall-rule create \
    --resource-group myResourceGroup \
    --server $servername \
    -n AllowYourIp \
    --start-ip-address $startip \
    --end-ip-address $endip

# Create a database in the server
az sql db create \
    --resource-group myResourceGroup \
    --server $servername \
    --name mySampleDatabase \
    --sample-name AdventureWorksLT \
    --service-objective S0

清理部署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 a single database or elastic pool.
az sql server firewall createaz sql server firewall create 创建一个防火墙规则,允许从输入的 IP 地址范围访问 SQL 数据库服务器上的所有单一数据库和弹性池。Creates a firewall rule to allow access to all single databases an elastic pools on the SQL Database server from the entered IP address range.
az sql db createaz sql db create 创建单一数据库或弹性池。Creates a single database or elastic pool.
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.