快速入门:将 BACPAC 文件导入 Azure SQL 数据库中的数据库Quickstart: Import a BACPAC file to a database in Azure SQL Database

可以使用 BACPAC 文件将 SQL Server 数据库导入 Azure SQL 数据库中的数据库。You can import a SQL Server database into a database in Azure SQL Database 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.

Note

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

Important

导入数据库后,可选择以当前兼容级别(对于 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 门户中从 BACPAC 文件导入Import from a BACPAC file in the 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.

目前不支持使用 Azure PowerShell 将数据库从 BACPAC 文件迁移到托管实例中。Migrating a database into a managed instance from a BACPAC file using Azure PowerShell is not currently supported. 请改用 SQL Server Management Studio 或 SQLPackage。Use SQL Server Management Studio or SQLPackage instead.

Note

处理通过 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 database 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 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 database server, select SQL databases and verify the new database is Online.

使用 SqlPackage 从 BACPAC 文件导入Import from a BACPAC file using SqlPackage

若要使用 SqlPackage 命令行实用程序导入 SQL Server 数据库,请参阅导入参数和属性To import a SQL Server database using the SqlPackage command-line utility, see import parameters and properties. SqlPackage 具有最新的 SQL Server Management StudioSQL Server Data Tools for Visual StudioSqlPackage has the latest SQL Server Management Studio and SQL Server Data Tools for Visual Studio. 还可以从 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 来实现缩放和性能。For scale and performance, we recommend using SqlPackage in most production environments. 如需 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 的 Azure SQL 数据库服务器。The following SqlPackage command imports the AdventureWorks2008R2 database from local storage to an Azure SQL Database server called 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=mynewserver20170403.database.chinacloudapi.cn;Initial Catalog=myMigratedDatabase;User Id=<your_server_admin_account_user_id>;Password=<your_server_admin_account_password>" /sf:AdventureWorks2008R2.bacpac /p:DatabaseEdition=Premium /p:DatabaseServiceObjective=P6

Important

若要从公司防火墙后连接到管理单一数据库的 SQL 数据库服务器,该防火墙必须打开端口 1433。To connect to a SQL Database server managing a single database from behind a corporate firewall, the firewall must have port 1433 open. 若要连接到托管实例,必须有点到站点连接或快速路由连接。To connect to a 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"

使用 PowerShell 从 BACPAC 文件导入单个数据库Import into a single database from a BACPAC file using PowerShell

Note

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

Note

处理通过门户或 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.

Note

本文进行了更新,以便使用新的 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.

Important

PowerShell Azure 资源管理器模块仍受 Azure SQL 数据库的支持,但所有未来的开发都是针对 Az.Sql 模块的。The PowerShell Azure Resource Manager module is still supported by Azure SQL Database, but all future development is for the Az.Sql module. 若要了解这些 cmdlet,请参阅 AzureRM.SqlFor these cmdlets, see AzureRM.Sql. Az 模块和 AzureRm 模块中的命令参数大体上是相同的。The arguments for the commands in the Az module and in the AzureRm modules are substantially identical.

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

$importRequest = New-AzSqlDatabaseImport 
   -ResourceGroupName "<your_resource_group>" `
   -ServerName "<your_server>" `
   -DatabaseName "<your_database>" `
   -DatabaseMaxSizeBytes "<database_size_in_bytes>" `
   -StorageKeyType "StorageAccessKey" `
   -StorageKey $(Get-AzStorageAccountKey -ResourceGroupName "<your_resource_group>" -StorageAccountName "<your_storage_account").Value[0] `
   -StorageUri "https://myStorageAccount.blob.core.chinacloudapi.cn/importsample/sample.bacpac" `
   -Edition "Standard" `
   -ServiceObjectiveName "P6" `
   -AdministratorLogin "<your_server_admin_account_user_id>" `
   -AdministratorLoginPassword $(ConvertTo-SecureString -String "<your_server_admin_account_password>" -AsPlainText -Force)

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

$importStatus = Get-AzSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink
[Console]::Write("Importing")
while ($importStatus.Status -eq "InProgress")
{
    $importStatus = Get-AzSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink
    [Console]::Write(".")
    Start-Sleep -s 10
}
[Console]::WriteLine("")
$importStatus

Tip

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

限制Limitations

不支持导入到弹性池中的数据库。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.

使用向导导入Import using wizards

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

后续步骤Next steps