使用 CLI 将应用服务应用连接到 SQL 数据库Connect an App Service app to a SQL database using CLI

此示例脚本创建一个 Azure SQL 数据库和一个应用服务应用。This sample script creates an Azure SQL database and an App Service app. 然后,它将使用应用设置将 SQL 数据库链接到应用。It then links the SQL database to the app using app settings.

如果没有 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, you need Azure CLI version 2.0 or later. 若要查找版本,请运行 az --versionTo find the version, run az --version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

示例脚本Sample script

#/bin/bash

# Variables
appName="webappwithSQL$RANDOM"
serverName="webappwithsql$RANDOM"
location="ChinaNorth"
startip="0.0.0.0"
endip="0.0.0.0"
username="<replace-with-username>"
sqlServerPassword="<replace-with-password>"

# Create a resource group 
az group create --name myResourceGroup --location $location

# Create an App Service Plan
az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --location $location

# Create a Web App
az webapp create --name $appName --plan myAppServicePlan --resource-group myResourceGroup

# Create a SQL Database server
az sql server create --name $serverName --resource-group myResourceGroup --location $location --admin-user $username --admin-password $sqlServerPassword

# Configure firewall for Azure access
az sql server firewall-rule create --server $serverName --resource-group myResourceGroup \
--name AllowYourIp --start-ip-address $startip --end-ip-address $endip

# Create a database called 'MySampleDatabase' on server
az sql db create --server $serverName --resource-group myResourceGroup --name MySampleDatabase \
--service-objective S0

# Get connection string for the database
connstring=$(az sql db show-connection-string --name MySampleDatabase --server $serverName \
--client ado.net --output tsv)

# Add credentials to connection string
connstring=${connstring//<username>/$username}
connstring=${connstring//<password>/$sqlServerPassword}

# Assign the connection string to an app setting in the web app
az webapp config appsettings set --name $appName --resource-group myResourceGroup \
--settings "SQLSRV_CONNSTR=$connstring" 

清理部署Clean up deployment

运行示例脚本后,可以使用以下命令删除资源组以及与其关联的所有资源。After the sample script has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令创建资源组、应用服务应用、SQL 数据库和所有相关资源。This script uses the following commands to create a resource group, App Service app, SQL Database, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az appservice plan create 创建应用服务计划。Creates an App Service plan.
az webapp create 创建应用服务应用。Creates an App Service app.
az sql server create 创建 SQL 数据库服务器。Creates a SQL Database Server.
az sql db create 使用 SQL 数据库服务器创建新的数据库。Creates a new database with the SQL Database Server.
az sql db show-connection-string 生成数据库的连接字符串。Generates a connection string to a database.
az webapp config appsettings set 创建或更新应用服务应用的应用设置。Creates or updates an app setting for an App Service app. 应用设置将作为应用的环境变量公开。App settings are exposed as environment variables for your app.

后续步骤Next steps

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

可以在 Azure 应用服务文档中找到其他应用服务 CLI 脚本示例。Additional App Service CLI script samples can be found in the Azure App Service documentation.