您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 Azure 资源管理器模板导入 SQL BACPAC 文件Tutorial: Import SQL BACPAC files with Azure Resource Manager templates

了解如何使用 Azure SQL 数据库扩展,以通过 Azure 资源管理器模板导入 BACPAC 文件。Learn how to use Azure SQL Database extensions to import a BACPAC file with Azure Resource Manager templates. 部署项目包括主模板文件以及完成部署所需的任何文件。Deployment artifacts are any files, in addition to the main template file that are needed to complete a deployment. BACPAC 文件是一个项目。The BACPAC file is an artifact. 在本教程中,你将创建一个模板来部署 Azure SQL Server、SQL 数据库并导入一个 BACPAC 文件。In this tutorial, you create a template to deploy an Azure SQL Server, a SQL Database, and import a BACPAC file. 若要了解如何使用 Azure 资源管理器模板来部署 Azure 虚拟机扩展,请参阅 # 教程:使用 Azure 资源管理器模板部署虚拟机扩展For information about deploying Azure virtual machine extensions using Azure Resource Manager templates, see # Tutorial: Deploy virtual machine extensions with Azure Resource Manager templates.

本教程涵盖以下任务:This tutorial covers the following tasks:

  • 准备 BACPAC 文件Prepare a BACPAC file
  • 打开快速入门模板Open a Quickstart template
  • 编辑模板Edit the template
  • 部署模板Deploy the template
  • 验证部署Verify the deployment

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

先决条件Prerequisites

若要完成本文,需要做好以下准备:To complete this article, you need:

准备 BACPAC 文件Prepare a BACPAC file

BACPAC 文件在可以公开访问的 Azure 存储帐户上共享。A BACPAC file is shared on an Azure Storage account with the public access. 若要创建自己的文件,请参阅将 Azure SQL 数据库导出到 BACPAC 文件To create your own, see Export an Azure SQL database to a BACPAC file. 如果选择将文件发布到你自己的位置,则必须在教程的后面部分更新模板。If you choose to publish the file to your own location, you must update the template later in the tutorial.

打开快速入门模板Open a Quickstart template

本教程中使用的模板存储在 Azure 存储帐户中。The template used in this tutorial is stored in an Azure Storage account.

  1. 在 Visual Studio Code 中,选择“文件”>“打开文件”。From Visual Studio Code, select File>Open File.

  2. 在“文件名”中粘贴以下 URL:In File name, paste the following URL:

    https://armtutorials.blob.core.windows.net/createsql/azuredeploy.json
    
  3. 选择“打开”以打开该文件。Select Open to open the file.

    有三个在此模板中定义的资源:There are three resources defined in the template:

    • Microsoft.Sql/serversMicrosoft.Sql/servers. 请参阅模板参考See the template reference.

    • Microsoft.SQL/servers/securityAlertPoliciesMicrosoft.SQL/servers/securityAlertPolicies. 请参阅模板参考See the template reference.

    • Microsoft.SQL.servers/databasesMicrosoft.SQL.servers/databases. 请参阅模板参考See the template reference.

      在自定义模板之前,不妨对其进行一些基本的了解。It is helpful to get some basic understanding of the template before customizing it.

  4. 选择“文件”>“另存为”,将该文件的副本保存到名为 azuredeploy.json 的本地计算机。Select File>Save As to save a copy of the file to your local computer with the name azuredeploy.json.

编辑模板Edit the template

向模板添加两个其他资源。Add two additional resources to the template.

  • 若要允许 SQL 数据库扩展导入 BACPAC 文件,需允许访问 Azure 服务。To allow the SQL database extension to import BACPAC files, you need to allow access to Azure services. 将以下 JSON 添加到 SQL 服务器定义:Add the following JSON to the SQL server definition:

    {
        "type": "firewallrules",
        "name": "AllowAllAzureIps",
        "location": "[parameters('location')]",
        "apiVersion": "2015-05-01-preview",
        "dependsOn": [
            "[variables('databaseServerName')]"
        ],
        "properties": {
            "startIpAddress": "0.0.0.0",
            "endIpAddress": "0.0.0.0"
        }
    }
    

    模板应如下所示:The template shall look like:

    Azure 资源管理器部署 sql 扩展 BACPAC

  • 使用以下 JSON 将 SQL 数据库扩展资源添加到数据库定义:Add a SQL Database extension resource to the database definition with the following JSON:

    "resources": [
        {
            "name": "Import",
            "type": "extensions",
            "apiVersion": "2014-04-01",
            "dependsOn": [
                "[resourceId('Microsoft.Sql/servers/databases', variables('databaseServerName'), variables('databaseName'))]"
            ],
            "properties": {
                "storageKeyType": "SharedAccessKey",
                "storageKey": "?",
                "storageUri": "https://armtutorials.blob.core.windows.net/sqlextensionbacpac/SQLDatabaseExtension.bacpac",
                "administratorLogin": "[variables('databaseServerAdminLogin')]",
                "administratorLoginPassword": "[variables('databaseServerAdminLoginPassword')]",
                "operationMode": "Import",
            }
        }
    ]
    

    模板应如下所示:The template shall look like:

    Azure 资源管理器部署 sql 扩展 BACPAC

    若要了解资源定义,请参阅 SQL 数据库扩展参考To understand the resource definition, see the SQL Database extension reference. 下面是一些重要元素:The following are some important elements:

    • dependsOn:必须在创建 SQL 数据库以后才能创建扩展资源。dependsOn: The extension resource must be created after the SQL database has been created.
    • storageKeyType:要使用的存储密钥的类型。storageKeyType: The type of the storage key to use. 值可以是 StorageAccessKeySharedAccessKeyThe value can be either StorageAccessKey or SharedAccessKey. 由于提供的 BACPAC 文件在可以公开访问的 Azure 存储帐户上共享,因此此处使用“SharedAccessKey”。Because the provided BACPAC file is shared on an Azure Storage account with public access, `SharedAccessKey' is used here.
    • storageKey:要使用的存储密钥。storageKey: The storage key to use. 如果存储密钥类型为 SharedAccessKey,则必须以“?”为前缀。If storage key type is SharedAccessKey, it must be preceded with a "?."
    • storageUri:要使用的存储 URI。storageUri: The storage uri to use. 如果选择不使用提供的 BACPAC 文件,则需更新这些值。If you choose not to use the BACPAC file provided, you need to update the values.
    • administratorLoginPassword:SQL 管理员的密码。administratorLoginPassword: The password of the SQL administrator. 使用生成的密码。Use a generated password. 请参阅先决条件See Prerequisites.

部署模板Deploy the template

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

有关部署过程,请参阅部署模板部分。Refer to the Deploy the template section for the deployment procedure. 改用以下 PowerShell 部署脚本:Use the following PowerShell deployment script instead:

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
$location = Read-Host -Prompt "Enter the location (i.e. centralus)"
$adminUsername = Read-Host -Prompt "Enter the SQL admin username"
$adminPassword = Read-Host -Prompt "Enter the admin password" -AsSecureString

New-AzResourceGroup -Name $resourceGroupName -Location $location
New-AzResourceGroupDeployment `
    -ResourceGroupName $resourceGroupName `
    -adminUser $adminUsername `
    -adminPassword $adminPassword `
    -TemplateFile "$HOME/azuredeploy.json"

使用生成的密码。Use a generated password. 请参阅先决条件See Prerequisites.

验证部署Verify the deployment

在门户中,从新部署的资源组中选择 SQL 数据库。In the portal, select the SQL database from the newly deployed resource group. 选择“查询编辑器(预览)”,然后输入管理员凭据。Select Query editor (preview), and then enter the administrator credentials. 此时会看到两个表导入到数据库中:You shall see two tables imported into the database:

Azure 资源管理器部署 sql 扩展 BACPAC

清理资源Clean up resources

不再需要 Azure 资源时,请通过删除资源组来清理部署的资源。When the Azure resources are no longer needed, clean up the resources you deployed by deleting the resource group.

  1. 在 Azure 门户上的左侧菜单中选择“资源组”。From the Azure portal, select Resource group from the left menu.
  2. 在“按名称筛选”字段中输入资源组名称。Enter the resource group name in the Filter by name field.
  3. 选择资源组名称。Select the resource group name. 应会看到,该资源组中总共有六个资源。You shall see a total of six resources in the resource group.
  4. 在顶部菜单中选择“删除资源组”。Select Delete resource group from the top menu.

后续步骤Next steps

在本教程中,你部署了 SQL Server、SQL 数据库并导入了 BACPAC 文件。In this tutorial, you deployed a SQL Server, a SQL Database, and imported a BACPAC file. BACPAC 文件存储在 Azure 存储帐户中。The BACPAC file is stored in an Azure storage account. 得到该 URL 的任何人都可以访问该文件。Anybody with the URL can access the file. 若要了解如何保护 BACPAC 文件(项目),请参阅To learn how to secure the BACPAC file (artifact), see