使用 PolyBase 中的托管标识连接到 Azure 存储

适用于: SQL Server 2025 (17.x) 及更高版本

从 SQL Server 2025(17.x)开始,可以使用 托管标识 访问以下 Azure 资源:

  • Azure Blob 存储
  • Azure Data Lake

先决条件

  • SQL Server 2025 (17.x)
  • 由 Azure Arc 启用的 SQL Server
  • allow server scoped db credentials启用服务器配置选项
  • 授予托管标识对 Azure Blob 存储资源的访问权限。

创建数据库范围凭据

为托管标识添加数据库作用域凭据。

  1. 允许服务器范围内的数据库凭证。 运行以下 Transact-SQL 查询:

    EXECUTE sp_configure 'allow server scoped db credentials', 1;
    GO
    RECONFIGURE;
    
  2. 创建数据库范围的凭据。 此示例使用名称 managed_id

    CREATE DATABASE SCOPED CREDENTIAL [managed_id]
    WITH IDENTITY = 'Managed Identity';
    

创建外部数据源

使用以下设置创建外部数据源。

  • 连接器位置前缀

    • abs
  • 位置路径

    • abs://<container_name>@<storage_account_name>.blob.core.windows.net/
    • abs://<storage_account_name>.blob.core.windows.net/<container_name>
  • 产品/服务支持的位置

    • 由 Azure Arc 支持的 SQL Server 2025 (17.x)
    • SQL Server 2022 (16.x):支持的分层命名空间
  • 身份验证

    • 共享访问签名(SAS),或
    • 托管标识

在 Azure Blob 存储中查询 Parquet 文件

SQL Server 2025 (17.x) 支持通过 Azure Arc 进行托管标识。有关说明,请参阅 由 Azure Arc 启用的 SQL Server 的托管标识

以下示例在 Azure Blob 存储中查询 Parquet 文件:

EXECUTE sp_configure 'allow server scoped db credentials', 1;
RECONFIGURE;
GO

CREATE DATABASE SCOPED CREDENTIAL [managed_id]
WITH IDENTITY = 'Managed Identity';

CREATE EXTERNAL DATA SOURCE [my_external_data_source]
WITH (
    LOCATION = 'abs://<container>@<storage_account_name>.blob.core.windows.net/',
    CREDENTIAL = managed_id
);

错误和解决方案

无法访问外部表(错误 16562)

如果缺少先决条件,则尝试访问 Azure Blob 存储或 Azure Data Lake 时,可能会遇到错误 16562:

Msg 16562, Level 16, State 1, Line 79
External table <name> is not accessible because location does not exist or it is used by another process.

检查以下项:

无法打开文件(错误 13822)

如果托管身份对存储帐户缺乏权限,或者存储的网络访问被阻止,在访问 Azure Blob 存储或 Azure Data Lake 时,您可能会遇到错误 13822。

Msg 13822, Level 16, State 1, Line 9
File <file> cannot be opened because it does not exist or it is used by another process.

检查以下项:

  • 托管标识是否有权访问存储容器?
  • 托管标识是否可以访问 SQL Server 外部的存储容器?
  • 文件是否完全锁定?