使用 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 存储资源的访问权限。

更新注册表

警告

错误编辑注册表会严重损坏您的系统。 更改注册表项之前,建议您备份计算机中的所有重要数据。

更新注册表子项 \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQLServer\FederatedAuthentication。 为数据存储类型添加以下条目:

价值
AADDataLakeEndPoint datalake.azure.net
AADAzureStorageEndpoint storage.azure.com

注册表示例

以下示例脚本插入名为 SQL25Inst 的 SQL Server 2025(17.x) 预览版命名实例的注册表项(如果尚不存在):

# Change to your SQL Server instance.
$yourInstance = "MSSQL17.SQL25Inst"

# Define the registry path
$regPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$($yourInstance)\MSSQLServer\FederatedAuthentication"
Write-Host "Path to be updated: $regPath"

# Ensure the path exists
if (-not (Test-Path $regPath)) {
    New-Item -Path $regPath -Force | Out-Null
}

# Define the values to create
$values = @{
    "AADDataLakeEndPoint" = "datalake.azure.net"
    "AADAzureStorageEndpoint" = "storage.azure.com"
}

foreach ($name in $values.Keys) {
    $existing = Get-ItemProperty -Path $regPath -Name $name -ErrorAction SilentlyContinue
    if ($null -eq $existing) {
        New-ItemProperty -Path $regPath -Name $name -Value $values[$name] -PropertyType String -Force
        Write-Host "Created registry value '$name' with '$($values[$name])'"
    }
    else {
        Write-Host "Registry value '$name' already exists. Skipping..."
    }
}

将这些密钥与 Azure Arc 启用的 SQL Server 托管标识(预览版)中所述的密钥一起添加。

创建数据库范围凭据

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

  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 外部的存储容器?
  • 文件是否完全锁定?