快速入门:使用 SSMS 将数据库还原到 Azure SQL 托管实例

适用于:Azure SQL 托管实例

在本快速入门中,使用 SQL Server Management Studio (SSMS) 将数据库从 Azure Blob 存储还原到 Azure SQL 托管实例。 本快速入门使用 SAS 从公共可用备份文件中还原 Wide World Importers 示例数据库。

注意

有关使用 Azure 数据库迁移服务进行迁移的详细信息,请参阅教程:使用数据库迁移服务将 SQL Server 迁移到 Azure SQL 托管实例

先决条件

本快速入门:

注意

有关使用 Blob 存储与共享访问签名密钥备份和还原 SQL Server 数据库的详细信息,请参阅将 SQL Server 备份到 URL

使用还原向导从备份文件还原

在 SSMS 中,采取以下部分中的步骤,使用还原向导将 Wide World Importers 数据库还原到 SQL 托管实例。 数据库备份文件存储在预配置的 Blob 存储帐户中。

打开还原向导

  1. 打开 SSMS 并连接到 SQL 托管实例。

  2. 在“对象资源管理器”中,右键单击 SQL 托管实例数据库的“数据库”文件夹并选择“还原数据库”以打开还原向导。

    Screenshot of Object Explorer in SSMS. The Databases folder is selected. In its shortcut menu, Restore Database is selected.

选择备份源

  1. 在还原向导中,选择省略号 (...) 选择要还原的备份集的源。

    Screenshot of a page in the restore wizard. In the Source section, Device is selected, and the ellipsis is called out.

  2. 在“选择备份设备”中,选择“添加”。 在“备份媒体类型”中,“URL”是唯一可用的选项,因为它是唯一受支持的源类型。 选择“确定”

    Screenshot of the Select backup devices dialog. The Add and OK buttons are called out.

  3. 在“选择备份文件位置”中,从三个选项中选择一个,以提供有关备份文件位置的信息:

    • 从“Azure 存储容器”列表中选择预注册的存储容器。
    • 输入新的存储容器和共享访问签名。 将为你注册一个新的 SQL 凭据。
    • 选择“添加”以浏览 Azure 订阅中的更多存储容器。

    Screenshot of the Select a Backup File Location dialog. In the Azure storage container section, Add is selected.

    如果选择“添加”,请转到下一部分 - 浏览 Azure 订阅存储容器。 如果使用其他方法提供备份文件的位置,请跳至还原数据库

    若要还原可公开访问的只读存储中的 Wide World Importers 示例数据库,请提供值 https://mitutorials.blob.core.windows.net/examples/ 并在“共享访问签名”字段提供任意值(如 SAS)。

    Screenshot of the Select a Backup File Location dialog. The sample WideWorldImporters folder location is specified. Select is boxed in red.

浏览 Azure 订阅存储容器

注意

这些步骤不需要从可公开访问的只读 Azure Blob 存储还原 WideWorldImporters 示例数据库,但需要从自己的 Azure Blob 存储还原数据库。

  1. 在连接到订阅之前,需要配置云环境。 请导航至“工具”并选择“选项”,然后在“Azure 服务”下的“Azure 云”选项中选择“AzureChinaCloud”。

    Set Cloud Environment.

  2. 在“连接到 Microsoft 订阅”中,选择“登录”以登录到 Azure 订阅:

    Screenshot of the Connect to a Microsoft Subscription dialog. The Sign In button is called out.

  3. 登录到 Azure 帐户,以在 Azure 中启动会话。

    Screenshot of the Sign in to your account dialog. The Microsoft logo, a sign-in box, and other UI elements are visible.

  4. 选择包含备份文件的存储帐户的订阅。

    Screenshot of the Connect to a Microsoft Subscription dialog. Under Select a subscription to use, the down arrow on the list box is called out.

  5. 选择包含备份文件的存储帐户。

    Screenshot of the Connect to a Microsoft Subscription dialog. The down arrow on the Select Storage Account list box is called out.

  6. 选择包含备份文件的 blob 容器。

    Screenshot of the Connect to a Microsoft Subscription dialog. The down arrow on the Select Blob Container list box is called out.

  7. 输入共享访问策略的到期日期并选择“创建凭据”。 系统将创建一个具有正确权限的共享访问签名。 选择“确定”。

    Screenshot of the Connect to a Microsoft Subscription dialog. Create Credential, OK, and the Shared Access Policy Expiration box are called out.

还原数据库

选择存储容器后,应会看到“在 Microsoft Azure 上定位备份文件”对话框。

  1. 在左侧窗格中,展开文件夹结构以显示包含备份文件的文件夹。 在右窗格中,选择与要还原的备份集相关的所有备份文件,然后选择“确定”。 例如,以下屏幕截图显示了包含 WideWorldImporters 示例数据库的可公开访问只读 Blob 存储。

    Screenshot of the Locate Backup File in Microsoft Azure dialog. The WideWorldImporters-Standard.bak backup file is selected, and the OK button is boxed in red.

    SSMS 将验证备份集。 完成此过程最多需要几秒钟。 持续时间取决于备份集的大小。

  2. 如果已验证备份,则需要为要还原的数据库指定名称。 默认情况下,在“目标”下,“数据库”框包含备份集数据库的名称。 若要更改名称,对“数据库”输入新名称。 选择“确定”。

    Screenshot of a page in the restore wizard. In the Destination section, the Database box is called out. The OK button is also called out.

    还原过程启动。 持续时间取决于备份集的大小。

    Screenshot of a page in the restore wizard. A progress indicator is called out.

  3. 还原过程完成后,一个对话框会显示还原成功。 选择“确定”

    Screenshot of a dialog over a page in the restore wizard. A message in the dialog indicates that the database was successfully restored.

  4. 在“对象资源管理器”中,选择“刷新”以检查还原的数据库。

    Screenshot of Object Explorer. The restored database is called out.

使用 T-SQL 从备份文件还原

作为还原向导的替代方法,可以使用 T-SQL 语句还原数据库。 在 SSMS 中,按照以下步骤使用 T-SQL 将 Wide World Importers 数据库还原到 SQL 托管实例。 数据库备份文件存储在预配置的 Blob 存储帐户中。

  1. 打开 SSMS 并连接到 SQL 托管实例。

  2. 在“对象资源管理器”中,右键单击 SQL 托管实例,并选择“新建查询”以打开新的查询窗口。

  3. 运行下面的 T-SQL 语句,它使用公共可用的预配置存储容器和共享访问签名密钥在 SQL 托管实例中创建凭据

    重要

    • CREDENTIAL 必须与容器路径匹配,以 https 开头,结尾不能包含正斜杠。
    • IDENTITY 必须为 SHARED ACCESS SIGNATURE
    • SECRET 必须是共享访问签名令牌,且不能包含前导 ?
    • 此示例中省略了 SECRET,因为存储帐户为公共可用。 如果使用未公开提供的存储帐户,则必须提供共享访问签名令牌。
    CREATE CREDENTIAL [https://mitutorials.blob.core.windows.net/examples/WideWorldImporters-Standard.bak]
    WITH IDENTITY = 'SHARED ACCESS SIGNATURE';
    

    Screenshot that shows the SSMS Query Editor. The CREATE CREDENTIAL statement is visible, and a message indicates that the query ran successfully.

    上一示例中的凭据路径提供对单个文件的访问权限。 还可以创建文件夹路径的共享访问签名令牌,例如:

    CREATE CREDENTIAL [https://<your storage>.blob.core.chinacloudapi.cn/databases/backups/]
    WITH IDENTITY = 'SHARED ACCESS SIGNATURE'
    , SECRET = '<your shared access signature>'; -- omit the leading ?
    
  4. 若要检查凭据是否正常工作,请运行以下语句,该语句使用 URL 来获取备份文件列表。

    RESTORE FILELISTONLY FROM URL = 'https://mitutorials.blob.core.windows.net/examples/WideWorldImporters-Standard.bak';
    

    Screenshot that shows the SSMS Query Editor. The RESTORE FILELISTONLY statement is visible, and the Results tab lists three files.

  5. 运行以下语句,还原示例 Wide World Importers 数据库。

    RESTORE DATABASE [WideWorldImportersExample] FROM URL =
      'https://mitutorials.blob.core.windows.net/examples/WideWorldImporters-Standard.bak';
    

    Screenshot that shows the SSMS Query Editor. The RESTORE DATABASE statement is visible, and a message indicates that the query ran successfully.

  6. 运行以下语句跟踪还原过程的状态。

    SELECT session_id as SPID, command, a.text AS Query, start_time, percent_complete
       , dateadd(second,estimated_completion_time/1000, getdate()) as estimated_completion_time
    FROM sys.dm_exec_requests r
    CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a
    WHERE r.command in ('BACKUP DATABASE','RESTORE DATABASE');
    
  7. 还原过程完成后,在“对象资源管理器”中查看数据库。 可以使用 sys.dm_operation_status 视图验证是否已还原数据库。

注意

数据库还原操作是异步且可重试的。 如果连接失败或超时过期,可能会在 SSMS 中得到一个错误。 SQL 托管实例在后台继续尝试还原数据库,你可以使用 sys.dm_exec_requestssys.dm_operation_status 视图来跟踪还原过程的进度。

在还原过程的某些阶段,系统视图中会显示唯一标识符,而不是实际的数据库名称。 要了解 RESTORE 语句行为差异,请参阅 SQL Server 和 Azure SQL 托管实例之间的 T-SQL 差异