使用受控識別設定 Azure SQL 的索引子連線

本文說明如何使用受控識別來設定 Azure SQL 資料庫的索引子連線,而不是在連接字串中提供認證。

您可以使用系統指派的受控識別或使用者指派的受控識別(預覽)。 受控識別是 Microsoft Entra 登入,需要 Azure 角色指派才能存取 Azure SQL 中的資料。

必要條件

1 - 指派許可權以讀取資料庫

請遵循下列步驟來指派搜尋服務或使用者指派的受控識別許可權,以讀取資料庫。

  1. 連線至 Visual Studio

    Connect to Visual Studio

  2. 使用您的 Microsoft Entra 帳戶進行驗證

    Authenticate

  3. 執行下列命令:

    在搜尋服務名稱或使用者指派的受控識別名稱周圍加上括弧。

    CREATE USER [insert your search service name here or user-assigned managed identity name] FROM EXTERNAL PROVIDER;
    EXEC sp_addrolemember 'db_datareader', [insert your search service name here or user-assigned managed identity name];
    

    New query

    Execute query

如果您稍後在指派許可權之後變更搜尋服務身分識別或使用者指派的身分識別,您必須移除角色成員資格,並移除 SQL 資料庫中的使用者,然後重複許可權指派。 您可以執行下列命令來移除角色成員資格和使用者:

sp_droprolemember 'db_datareader', [insert your search service name or user-assigned managed identity name];

DROP USER IF EXISTS [insert your search service name or user-assigned managed identity name];

2 - 新增角色指派

在本節中,您會提供 Azure AI 搜尋服務許可權,以從 SQL Server 讀取資料。 如需詳細步驟,請參閱使用 Azure 入口網站指派 Azure 角色

  1. 在Azure 入口網站中,流覽至您的 Azure SQL Server 頁面。

  2. 選取 [存取控制 (IAM)]。

  3. 選取 [新增 > 角色指派 ]。

    Screenshot that shows Access control (IAM) page with Add role assignment menu open.

  4. 在 [ 角色] 索引標籤上,選取適當的 讀者 角色。

  5. 在 [ 成員] 索引標籤上,選取 [受控識別 ],然後選取 [ 選取成員 ]。

  6. 選取 Azure 訂閱。

  7. 如果您使用系統指派的受控識別,請選取 [系統指派的受控識別 ]、搜尋您的搜尋服務,然後選取它。

  8. 否則,如果您使用使用者指派的受控識別,請選取 [使用者指派的受控識別 ],搜尋使用者指派的受控識別名稱,然後加以選取。

  9. 在 [檢閱 + 指派] 索引標籤上,選取 [檢閱 + 指派] 以指派角色。

3 - 建立資料來源

建立資料來源,並提供系統指派的受控識別或使用者指派的受控識別(預覽)。

系統指派的受控識別

REST API 、Azure 入口網站和 .NET SDK 支援系統指派的受控識別。

當您使用系統指派的受控識別進行連線時,資料來源定義的唯一變更就是 「credentials」 屬性的格式。 您將提供初始目錄或資料庫名稱,以及沒有帳戶金鑰或密碼的 ResourceId。 ResourceId 必須包含 Azure SQL 資料庫的訂用帳戶識別碼、SQL 資料庫的資源群組,以及 SQL 資料庫的名稱。

以下是如何使用建立資料來源 REST API 和受控識別連接字串,建立資料來源以從儲存體帳戶 編制資料的索引。 REST API、.NET SDK 和Azure 入口網站的受控識別連接字串格式相同。

POST https://[service name].search.windows.net/datasources?api-version=2020-06-30
Content-Type: application/json
api-key: [admin key]

{
    "name" : "sql-datasource",
    "type" : "azuresql",
    "credentials" : { 
        "connectionString" : "Database=[SQL database name];ResourceId=/subscriptions/[subscription ID]/resourceGroups/[resource group name]/providers/Microsoft.Sql/servers/[SQL Server name];Connection Timeout=30;"
    },
    "container" : { 
        "name" : "my-table" 
    }
} 

使用者指派的受控識別 (預覽)

2021-04-30-preview REST API 支援以使用者指派的受控識別為基礎的連線。 當您使用使用者指派的受控識別進行連線時,資料來源定義有兩個變更:

  • 首先,「認證」屬性的格式是初始目錄或資料庫名稱,以及沒有帳戶金鑰或密碼的 ResourceId。 ResourceId 必須包含 Azure SQL 資料庫的訂用帳戶識別碼、SQL 資料庫的資源群組,以及 SQL 資料庫的名稱。 這與系統指派的受控識別格式相同。

  • 其次,您將新增包含使用者指派受控識別集合的「身分識別」屬性。 建立資料來源時,應該只提供一個使用者指派的受控識別。 將它設定為輸入 「userAssignedIdentities」。

以下是如何使用預覽建立或更新資料來源 REST API 來建立索引子資料來源物件的 範例:

POST https://[service name].search.windows.net/datasources?api-version=2021-04-30-preview
Content-Type: application/json
api-key: [admin key]

{
    "name" : "sql-datasource",
    "type" : "azuresql",
    "credentials" : { 
        "connectionString" : "Database=[SQL database name];ResourceId=/subscriptions/[subscription ID]/resourceGroups/[resource group name]/providers/Microsoft.Sql/servers/[SQL Server name];Connection Timeout=30;"
    },
    "container" : { 
        "name" : "my-table" 
    },
    "identity" : { 
        "@odata.type": "#Microsoft.Azure.Search.DataUserAssignedIdentity",
        "userAssignedIdentity" : "/subscriptions/[subscription ID]/resourcegroups/[resource group name]/providers/Microsoft.ManagedIdentity/userAssignedIdentities/[managed identity name]"
    }
}   

4 - 建立索引

索引會指定檔、屬性和其他建構中塑造搜尋體驗的欄位。

以下是具有可 booktitle 搜尋欄位的建立 索引 REST API 呼叫:

POST https://[service name].search.windows.net/indexes?api-version=2020-06-30
Content-Type: application/json
api-key: [admin key]

{
    "name" : "my-target-index",
    "fields": [
        { "name": "id", "type": "Edm.String", "key": true, "searchable": false },
        { "name": "booktitle", "type": "Edm.String", "searchable": true, "filterable": false, "sortable": false, "facetable": false }
    ]
}

5 - 建立索引子

索引子會使用目標搜尋索引連接資料來源,並提供排程來自動化資料重新整理。 建立索引和資料來源之後,您就可以開始建立索引子。 如果索引子成功,連線語法和角色指派是有效的。

以下是使用 Azure SQL 索引子定義建立索引子 REST API 呼叫。 當您提交要求時,索引子將會執行。

POST https://[service name].search.windows.net/indexers?api-version=2020-06-30
Content-Type: application/json
api-key: [admin key]

{
    "name" : "sql-indexer",
    "dataSourceName" : "sql-datasource",
    "targetIndexName" : "my-target-index"

疑難排解

如果索引子嘗試連線到指出用戶端不允許存取伺服器的資料來源時發生錯誤,請查看 常見的索引子錯誤

您也可以嘗試連線,並不受限制地排除任何防火牆問題。

另請參閱