快速入门:将 BACPAC 文件导入 Azure SQL 数据库或 Azure SQL 托管实例中的数据库Quickstart: Import a BACPAC file to a database in Azure SQL Database or Azure SQL Managed Instance

适用于:是 Azure SQL 数据库 是Azure SQL 托管实例 APPLIES TO: yesAzure SQL Database yesAzure SQL Managed Instance

你可以使用 BACPAC 文件将 SQL Server 数据库导入 Azure SQL 数据库或 SQL 托管实例。You can import a SQL Server database into Azure SQL Database or SQL Managed Instance using a BACPAC file. 可以从 Azure Blob 存储(仅限标准存储)中存储的 BACPAC 文件或从本地位置中的本地存储导入数据。You can import the data from a BACPAC file stored in Azure Blob storage (standard storage only) or from local storage in an on-premises location. 若要通过提供更多且更快的资源将导入速度最大化,请在导入过程中将数据库扩展到更高的服务层级和更大的计算大小。To maximize import speed by providing more and faster resources, scale your database to a higher service tier and compute size during the import process. 然后,可以在导入成功后进行缩减。You can then scale down after the import is successful.


导入的数据库的兼容性级别基于源数据库的兼容性级别。The imported database's compatibility level is based on the source database's compatibility level.


导入数据库后,可选择以当前兼容级别(对于 AdventureWorks2008R2 数据库是级别 100)或更高级别操作数据库。After importing your database, you can choose to operate the database at its current compatibility level (level 100 for the AdventureWorks2008R2 database) or at a higher level. 有关在特定兼容级别操作数据库的影响和选项的详细信息,请参阅 ALTER DATABASE Compatibility Level(更改数据库兼容级别)。For more information on the implications and options for operating a database at a specific compatibility level, see ALTER DATABASE Compatibility Level. 有关与兼容级别相关的其他数据库级别设置的信息,另请参阅 ALTER DATABASE SCOPED CONFIGURATION(更改数据库范围的配置)。See also ALTER DATABASE SCOPED CONFIGURATION for information about additional database-level settings related to compatibility levels.

使用 Azure 门户Using Azure portal

Azure 门户仅支持在 Azure SQL 数据库中创建单个数据库,并且仅从存储在 Azure Blob 存储中的 BACPAC 文件中创建 。The Azure portal only supports creating a single database in Azure SQL Database and only from a BACPAC file stored in Azure Blob storage.

若要将数据库从 BACPAC 文件迁移到 Azure SQL 托管实例中,请使用 SQL Server Management Studio 或 SQLPackage,当前不支持使用 Azure 门户或 Azure PowerShell。To migrate a database into an Azure SQL Managed Instance from a BACPAC file, use SQL Server Management Studio or SQLPackage, using the Azure portal or Azure PowerShell is not currently supported.


处理通过 Azure 门户或 PowerShell 提交的导入/导出请求的计算机需要存储 BACPAC 文件以及数据层应用程序框架 (DacFX) 生成的临时文件。Machines processing import/export requests submitted through the Azure portal or PowerShell need to store the BACPAC file as well as temporary files generated by the Data-Tier Application Framework (DacFX). 相同大小的数据库之间所需的磁盘空间差异很大,所需的磁盘空间最多可能是数据库大小的 3 倍。The disk space required varies significantly among databases with the same size and can require disk space up to 3 times the size of the database. 运行导入/导出请求的计算机只有 450GB 的本地磁盘空间。Machines running the import/export request only have 450GB local disk space. 因此,某些请求可能会失败,并显示错误 There is not enough space on the diskAs a result, some requests may fail with the error There is not enough space on the disk. 在这种情况下,解决方法是在具有足够本地磁盘空间的计算机上运行 sqlpackage.exe。In this case, the workaround is to run sqlpackage.exe on a machine with enough local disk space. 我们建议使用 SqlPackage 导入/导出大于 150GB 的数据库以避免此问题。We encourage using SqlPackage to import/export databases larger than 150GB to avoid this issue.

  1. 若要使用 Azure 门户从 BACPAC 文件导入新的单个数据库,请打开相应的服务器页面,然后在工具栏上选择“导入数据库”。To import from a BACPAC file into a new single database using the Azure portal, open the appropriate server page and then, on the toolbar, select Import database.

    数据库 import1

  2. 选择存储帐户和 BACPAC 文件的容器,然后选择要从中导入的 BACPAC 文件。Select the storage account and the container for the BACPAC file and then select the BACPAC file from which to import.

  3. 指定新数据库大小(通常与源数据库相同)并提供目标 SQL Server 凭据。Specify the new database size (usually the same as origin) and provide the destination SQL Server credentials. 有关 Azure SQL 数据库中新数据库的可能值列表,请参阅创建数据库For a list of possible values for a new database in Azure SQL Database, see Create Database.

    数据库 import2

  4. 单击 “确定”Click OK.

  5. 若要监视导入的进度,请打开数据库的服务器页,然后在“设置”下,选择“导入/导出历史记录”。To monitor an import's progress, open the database's server page, and, under Settings, select Import/Export history. 成功导入后,状态为“已完成”。When successful, the import has a Completed status.


  6. 若要验证数据库在服务器上是否处于活动状态,请选择“SQL 数据库”并验证新数据库是否为“联机”。To verify the database is live on the server, select SQL databases and verify the new database is Online.

使用 SqlPackageUsing SqlPackage

若要使用 SqlPackage 命令行实用程序导入 SQL Server 数据库,请参阅导入参数和属性To import a SQL Server database using the SqlPackage command-line utility, see import parameters and properties. SQL Server Management StudioSQL Server Data Tools for Visual Studio 包括 SqlPackage。SQL Server Management Studio and SQL Server Data Tools for Visual Studio include SqlPackage. 还可以从 Microsoft 下载中心下载最新的 SqlPackageYou can also download the latest SqlPackage from the Microsoft download center.

在大多数生产环境中,建议使用 SqlPackage 而不是 Azure 门户来实现缩放和性能。For scale and performance, we recommend using SqlPackage in most production environments rather than using the Azure portal. 有关 SQL Server 客户咨询团队使用 BACPAC 文件进行迁移的博客,请参阅使用 BACPAC 文件从 SQL Server 迁移到 Azure SQL 数据库For a SQL Server Customer Advisory Team blog about migrating using BACPAC files, see migrating from SQL Server to Azure SQL Database using BACPAC Files.

以下 SqlPackage 命令可将 AdventureWorks2008R2 数据库从本地存储导入到名为 mynewserver20170403 的逻辑 SQL 服务器 。The following SqlPackage command imports the AdventureWorks2008R2 database from local storage to a logical SQL server named mynewserver20170403. 它将创建名为 myMigratedDatabase 的新数据库,其中包含“高级”服务层级和 P6 服务目标。It creates a new database called myMigratedDatabase with a Premium service tier and a P6 Service Objective. 根据你的环境更改这些值。Change these values as appropriate for your environment.

sqlpackage.exe /a:import /tcs:"Data Source=<serverName>.database.chinacloudapi.cn;Initial Catalog=<migratedDatabase>;User Id=<userId>;Password=<password>" /sf:AdventureWorks2008R2.bacpac /p:DatabaseEdition=Premium /p:DatabaseServiceObjective=P6


若要从公司防火墙后连接到 Azure SQL 数据库,该防火墙必须打开端口 1433。To connect to Azure SQL Database from behind a corporate firewall, the firewall must have port 1433 open. 若要连接到 SQL 托管实例,必须有点到站点连接或快速路由连接。To connect to SQL Managed Instance, you must have a point-to-site connection or an express route connection.

此示例演示如何通过 Active Directory 通用身份验证,使用 SqlPackage 来导入数据库。This example shows how to import a database using SqlPackage with Active Directory Universal Authentication.

sqlpackage.exe /a:Import /sf:testExport.bacpac /tdn:NewDacFX /tsn:apptestserver.database.chinacloudapi.cn /ua:True /tid:"apptest.partner.onmschina.cn"

使用 PowerShellUsing PowerShell


SQL 托管实例当前不支持使用 Azure PowerShell 从 BACPAC 文件将数据库迁移到实例数据库。A SQL Managed Instance does not currently support migrating a database into an instance database from a BACPAC file using Azure PowerShell. 若要导入 SQL 托管实例,请使用 SQL Server Management Studio 或 SQLPackage。To import into a SQL Managed Instance, use SQL Server Management Studio or SQLPackage.


处理通过门户或 Powershell 提交的导入/导出请求的计算机需要存储 bacpac 文件以及数据层应用程序框架 (DacFX) 生成的临时文件。The machines processing import/export requests submitted through portal or Powershell need to store the bacpac file as well as temporary files generated by Data-Tier Application Framework (DacFX). 所需的磁盘空间在具有相同大小的 DB 之间存在显著差异,并且最多可占数据库大小的 3 倍。The disk space required varies significantly among DBs with same size and can take up to 3 times of the database size. 运行导入/导出请求的计算机只有 450GB 的本地磁盘空间。Machines running the import/export request only have 450GB local disk space. 因此,某些请求可能会失败,出现“磁盘空间不足”错误。As result, some requests may fail with "There is not enough space on the disk" error. 在这种情况下,解决方法是在具有足够本地磁盘空间的计算机上运行 sqlpackage.exe。In this case, the workaround is to run sqlpackage.exe on a machine with enough local disk space. 导入/导出大于 150GB 的数据库时,请使用 SqlPackage 来避免此问题。When importing/exporting databases larger than 150GB, use SqlPackage to avoid this issue.


仍然支持 PowerShell Azure 资源管理器 (RM) 模块,但是所有未来的开发都是针对 Az.Sql 模块。The PowerShell Azure Resource Manager (RM) module is still supported, but all future development is for the Az.Sql module. AzureRM 模块至少在 2020 年 12 月之前将继续接收 bug 修补程序。The AzureRM module will continue to receive bug fixes until at least December 2020. Az 模块和 AzureRm 模块中的命令参数大体上是相同的。The arguments for the commands in the Az module and in the AzureRm modules are substantially identical. 若要详细了解其兼容性,请参阅新 Azure PowerShell Az 模块简介For more about their compatibility, see Introducing the new Azure PowerShell Az module.

使用 New-AzSqlDatabaseImport cmdlet 向 Azure 提交导入数据库请求。Use the New-AzSqlDatabaseImport cmdlet to submit an import database request to Azure. 根据数据库大小,导入操作可能需要一些时间才能完成。Depending on database size, the import may take some time to complete.

$importRequest = New-AzSqlDatabaseImport -ResourceGroupName "<resourceGroupName>" `
    -ServerName "<serverName>" -DatabaseName "<databaseName>" `
    -DatabaseMaxSizeBytes "<databaseSizeInBytes>" -StorageKeyType "StorageAccessKey" `
    -StorageKey $(Get-AzStorageAccountKey `
        -ResourceGroupName "<resourceGroupName>" -StorageAccountName "<storageAccountName>").Value[0] `
        -StorageUri "https://myStorageAccount.blob.core.chinacloudapi.cn/importsample/sample.bacpac" `
        -Edition "Standard" -ServiceObjectiveName "P6" `
        -AdministratorLogin "<userId>" `
        -AdministratorLoginPassword $(ConvertTo-SecureString -String "<password>" -AsPlainText -Force)

可以使用 Get-AzSqlDatabaseImportExportStatus cmdlet 检查导入的进度。You can use the Get-AzSqlDatabaseImportExportStatus cmdlet to check the import's progress. 如果在提交请求后立即运行此 cmdlet,通常会返回 Status: InProgressRunning the cmdlet immediately after the request usually returns Status: InProgress. 当看到 Status: Succeeded 时,导入已完成。The import is complete when you see Status: Succeeded.

$importStatus = Get-AzSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink

while ($importStatus.Status -eq "InProgress") {
    $importStatus = Get-AzSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink
    Start-Sleep -s 10



有关另一个脚本示例,请参阅从 BACPAC 文件导入数据库For another script example, see Import a database from a BACPAC file.


  • 不支持导入到弹性池中的数据库。Importing to a database in elastic pool isn't supported. 可以将数据导入到单一数据库,然后将数据库移到弹性池。You can import data into a single database and then move the database to an elastic pool.
  • 当“允许访问 Azure 服务”设置为“关闭”时,导入/导出服务无法正常工作。Import Export Service does not work when Allow access to Azure services is set to OFF. 不过,可通过以下方式解决此问题:在 Azure VM 中手动运行 sqlpackage.exe,或者直接在代码中使用 DACFx API 执行导出。However you can work around the problem by manually running sqlpackage.exe from an Azure VM or performing the export directly in your code by using the DACFx API.

使用向导导入Import using wizards

还可以使用以下向导。You can also use these wizards.

后续步骤Next steps