使用使用者指派的受控識別建立 Azure SQL 受控執行個體
- 發行項
適用於:Azure SQL 受控執行個體
本操作指南概述從 Microsoft Entra ID 建立具有使用者指派受控識別的 Azure SQL 受控執行個體的步驟(先前稱為 Azure Active Directory)。 如需在 Azure SQL 資料庫 中使用使用者指派受控識別來取得伺服器身分識別之優點的詳細資訊,請參閱 Microsoft Entra for Azure SQL 中的使用者指派受控識別。
注意
Microsoft Entra 標識符 先前稱為 Azure Active Directory (Azure AD)。
必要條件
- 若要使用使用者指派的受控識別布建 SQL 受管理執行個體,需要 SQL 受管理執行個體 參與者角色(或具有更高許可權的角色),以及包含下列動作的 Azure RBAC 角色:
- Microsoft.ManagedIdentity/userAssignedIdentities/*/assign/action - 例如,受控識別操作員具有此動作。
- 建立使用者指派的受控識別,並指派必要權限,以作為伺服器身分識別或受控執行個體身分識別。 如需詳細資訊,請參閱管理使用者指派的受控識別和適用於 Azure SQL 的使用者指派受控識別權限。
- 使用 PowerShell 來設定使用者指派的受控識別時,需要 Az.Sql 模組 3.4 或更高版本。
- 需要 Azure CLI 2.26.0 或更高版本,才能使用 Azure CLI 來設定使用者指派的受控識別。
- 如需使用使用者指派受控識別的限制和已知問題清單,請參閱 Microsoft Entra for Azure SQL 中的使用者指派受控識別
瀏覽至 Azure 入口網站中的 [選取 SQL 部署] 選項頁面。
如果您尚未登入 Azure 入口網站,請在出現提示時登入。
在 [SQL 受控實例] 下,將 [資源類型] 保留為 [單一實例],然後選取 [建立]。
針對 [專案詳細數據] 和 [受控執行個體 詳細數據,填寫 [基本] 索引標籤上所需的必要資訊。 這是布建 SQL 受管理執行個體 所需的最小資訊集。
如需設定選項的詳細資訊,請參閱快速入門:建立 Azure SQL 受控執行個體。
接下來,流覽 [ 網络] 索引 標籤,或保留預設設定。
在 [安全性] 索引標籤的 [身分識別] 下方,選取 [設定身分識別]。
在 [身分識別] 窗格的 [使用者指派受控識別] 底下,選取 [新增]。 選取所需的訂用帳戶,然後在 [使用者指派的受控識別] 下,從選取的訂用帳戶選取所需的使用者指派受控識別。 然後選取 [選取] 按鈕。
在 [主要身分識別] 下方,選取上一個步驟中選取的相同使用者指派受控識別。
注意
如果系統指派的受控識別是主要身分識別,則 [主要身分識別] 欄位必須是空的。
選取 [套用]
您可以保留其餘的設定預設值。 如需其他索引標籤和設定的詳細資訊,請遵循快速入門:建立 Azure SQL 受控執行個體 一文中的指南。
完成設定之後,請選取 [ 檢閱 + 建立 ] 以繼續進行。 選取 [建立] 以開始佈建受控執行個體。
Azure CLI 命令az sql mi create
可用來布建新的 Azure SQL 受控執行個體。 下列命令會布建具有使用者指派受控識別的受控實例,並啟用 僅限 Microsoft Entra 的驗證。
注意
腳本需要建立虛擬網路和子網作為必要條件。
受控實例 SQL 管理員 istrator 登入將會自動建立,並將密碼設定為隨機密碼。 由於 SQL 驗證連線已停用此佈建,因此不會使用 SQL 管理員 istrator 登入。
Microsoft Entra 系統管理員將會是您設定 <AzureADAccount>
的帳戶,而且可用來在布建完成時管理實例。
取代範例中的下列值:
<subscriptionId>
:在 Azure 入口網站中可以找到您的訂用帳戶識別碼<ResourceGroupName>
:受控實例的資源群組名稱。 資源群組也應該包含建立的虛擬網路和子網<managedIdentity>
:使用者指派的受控識別。 也可以用來作為主要身分識別。<primaryIdentity>
:您想要用來作為實例身分識別的主要身分識別<AzureADAccount>
:可以是 Microsoft Entra 使用者或群組。 例如,DummyLogin
<AzureADAccountSID>
:使用者的 Microsoft Entra 物件識別碼<managedinstancename>
:將您想要建立的受控實例命名為subnet
參數必須以<subscriptionId>
、<ResourceGroupName>
<VNetName>
、 和<SubnetName>
更新。
# Define variables for resources
subscriptionId="<subscriptionId>"
resourceGroupName="<ResourceGroupName>"
managedIdentity="<managedIdentity>"
primaryIdentity="<primaryIdentity>"
AzureADAccount="<AzureADAccount>"
AzureADAccountSID="<AzureADAccountSID>"
VNetName="<VNetName>"
SubnetName="<SubnetName>"
managedinstancename="<managedinstancename>"
# Create a managed instance with a user-assigned managed identity
az sql mi create \
--assign-identity \
--identity-type UserAssigned \
--user-assigned-identity-id "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$managedIdentity" \
--primary-user-assigned-identity-id "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$primaryIdentity" \
--enable-ad-only-auth \
--external-admin-principal-type User \
--external-admin-name $AzureADAccount \
--external-admin-sid $AzureADAccountSID \
-g $resourceGroupName \
-n $managedinstancename \
--subnet "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Network/virtualNetworks/$VNetName/subnets/$SubnetName"
如需詳細資訊,請參閱 az sql mi create。
注意
上述範例只布建具有使用者指派受控識別的受控實例。 如果您要使用 實體建立這兩種類型的受控識別,您可以將 設定--identity-type
UserAssigned,SystemAssigned
為 。
PowerShell 命令New-AzSqlInstance
可用來布建新的 Azure SQL 受控執行個體。 下列命令會布建具有使用者指派受控識別的受控實例,並啟用 僅限 Microsoft Entra 的驗證。
注意
腳本需要建立虛擬網路和子網作為必要條件。
受控實例 SQL 管理員 istrator 登入會自動建立,並將密碼設定為隨機密碼。 由於 SQL 驗證連線已使用此佈建停用,因此不會使用 SQL 管理員 istrator 登入。
Microsoft Entra 系統管理員將會是您設定 <AzureADAccount>
的帳戶,而且可用來在布建完成時管理實例。
取代範例中的下列值:
<managedinstancename>
:將您想要建立的受控實例命名為<ResourceGroupName>
:受控實例的資源群組名稱。 資源群組也應該包含建立的虛擬網路和子網<subscriptionId>
:在 Azure 入口網站中可以找到您的訂用帳戶識別碼<managedIdentity>
:使用者指派的受控識別。 也可以用來作為主要身分識別。<primaryIdentity>
:您想要用來作為實例身分識別的主要身分識別<Location>
:受控實例的位置,例如West US
、 或Central US
<AzureADAccount>
:可以是 Microsoft Entra 使用者或群組。 例如,DummyLogin
SubnetId
參數必須以<subscriptionId>
、<ResourceGroupName>
<VNetName>
、 和<SubnetName>
更新。
$instanceName = @{
Name = "<managedinstancename>"
ResourceGroupName = "<ResourceGroupName>"
AssignIdentity = $true
IdentityType = "UserAssigned"
UserAssignedIdentityId = "/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<managedIdentity>"
PrimaryUserAssignedIdentityId = "/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<primaryIdentity>"
ExternalAdminName = "<AzureADAccount>"
EnableActiveDirectoryOnlyAuthentication = $true
Location = "<Location>"
SubnetId = "/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.Network/virtualNetworks/<VNetName>/subnets/<SubnetName>"
LicenseType = "LicenseIncluded"
StorageSizeInGB = 1024
VCore = 16
Edition = "GeneralPurpose"
ComputeGeneration = "Gen5"
}
New-AzSqlInstance @instanceName
如需詳細資訊,請參閱 New-AzSqlInstance。
注意
上述範例只布建具有使用者指派受控識別的受控實例。 如果您要使用 實體建立這兩種類型的受控識別,您可以將 設定-IdentityType
"UserAssigned,SystemAssigned"
為 。
SQL 受管理執行個體 - 建立或更新 REST API 可用來建立具有使用者指派受控識別的受控實例。
注意
腳本需要建立虛擬網路和子網作為必要條件。
下列腳本會布建具有使用者指派受控識別的受控實例、將 Microsoft Entra 系統管理員設定為 <AzureADAccount>
,並啟用 僅限 Microsoft Entra 驗證。 也會自動建立實例 SQL 管理員 istrator 登入,並將密碼設定為隨機密碼。 由於此佈建已停用 SQL 驗證連線,因此不會使用 SQL 管理員登入。
Microsoft Entra 系統管理員 <AzureADAccount>
可用來管理布建完成時的實例。
取代範例中的下列值:
<tenantId>
:您可以移至 Azure 入口網站,然後移至您的 Microsoft Entra ID 資源。 在 [概觀] 窗格中,應該會看到您的租用戶識別碼<subscriptionId>
您可以在 Azure 入口網站中找到訂閱識別碼<instanceName>
:使用唯一的受控實例名稱<ResourceGroupName>
:您邏輯伺服器的資源群組名稱<AzureADAccount>
:可以是 Microsoft Entra 使用者或群組。 例如,DummyLogin
<Location>
:伺服器的位置,例如westus2
或centralus
<objectId>
:您可以移至 Azure 入口網站,並前往您的 Microsoft Entra ID 資源。 在 [ 使用者 ] 窗格中,搜尋 Microsoft Entra 使用者並尋找其 物件識別碼- 參數
subnetId
必須以<ResourceGroupName>
、Subscription ID
、 和<VNetName>
更新<SubnetName>
Import-Module Azure
Import-Module MSAL.PS
$tenantId = '<tenantId>'
$clientId = '1950a258-227b-4e31-a9cf-717495945fc2' # Static Microsoft client ID used for getting a token
$subscriptionId = '<subscriptionId>'
$uri = "urn:ietf:wg:oauth:2.0:oob"
$instanceName = "<instanceName>"
$resourceGroupName = "<ResourceGroupName>"
$scopes ="https://management.core.windows.net/.default"
Login-AzAccount -tenantId $tenantId
# Login as an Azure AD user with permission to provision a managed instance
$result = Get-MsalToken -RedirectUri $uri -ClientId $clientId -TenantId $tenantId -Scopes $scopes
$authHeader = @{
'Content-Type'='application\json; '
'Authorization'=$result.CreateAuthorizationHeader()
}
$body = '{
"name": "<instanceName>", "type": "Microsoft.Sql/managedInstances", "identity": {"type" : "UserAssigned", "UserAssignedIdentities" : {"/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<managedIdentity>" : {}}},"location": "<Location>", "sku": {"name": "GP_Gen5", "tier": "GeneralPurpose", "family":"Gen5","capacity": 8},
"properties": { "PrimaryUserAssignedIdentityId":"/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<primaryIdentity>","administrators":{ "login":"<AzureADAccount>", "sid":"<objectId>", "tenantId":"<tenantId>", "principalType":"User", "azureADOnlyAuthentication":true },
"subnetId": "/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.Network/virtualNetworks/<VNetName>/subnets/<SubnetName>",
"licenseType": "LicenseIncluded", "vCores": 8, "storageSizeInGB": 2048, "collation": "SQL_Latin1_General_CP1_CI_AS", "proxyOverride": "Proxy", "timezoneId": "UTC", "privateEndpointConnections": [], "storageAccountType": "GRS", "zoneRedundant": false
}
}'
# To provision the instance, execute the `PUT` command
Invoke-RestMethod -Uri https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/managedInstances/$instanceName/?api-version=2020-11-01-preview -Method PUT -Headers $authHeader -Body $body -ContentType "application/json"
若要檢查結果,請執行 GET
命令:
Invoke-RestMethod -Uri https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/managedInstances/$instanceName/?api-version=2020-11-01-preview -Method GET -Headers $authHeader | Format-List
若要布建使用 Microsoft Entra 系統管理員、使用者指派的受控識別和僅限 Microsoft Entra 驗證設定的新虛擬網路、子網和新的受控實例,請使用下列範本。
使用 Azure 入口網站中的自訂部署,然後在編輯器中建置您自己的範本。 接下來,將設定貼進範例之後,加以儲存。
若要取得使用者指派的受控識別資源識別碼,請在 Azure 入口網站中搜尋受控識別。 尋找您的受控識別,並移至 [屬性]。 UMI 資源識別碼的範例看起來像 /subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<managedIdentity>
。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.1",
"parameters": {
"managedInstanceName": {
"type": "String",
"metadata": {
"description": "Enter managed instance name."
}
},
"aad_admin_name": {
"type": "String",
"metadata": {
"description": "The name of the Azure AD admin for the SQL managed instance."
}
},
"aad_admin_objectid": {
"type": "String",
"metadata": {
"description": "The Object ID of the Azure AD admin."
}
},
"aad_admin_tenantid": {
"type": "String",
"defaultValue": "[subscription().tenantId]",
"metadata": {
"description": "The Tenant ID of the Azure Active Directory"
}
},
"aad_admin_type": {
"defaultValue": "User",
"allowedValues": [
"User",
"Group",
"Application"
],
"type": "String"
},
"aad_only_auth": {
"defaultValue": true,
"type": "Bool"
},
"user_identity_resource_id": {
"defaultValue": "",
"type": "String",
"metadata": {
"description": "The Resource ID of the user-assigned managed identity, in the form of /subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<managedIdentity>."
}
},
"location": {
"defaultValue": "[resourceGroup().location]",
"type": "String",
"metadata": {
"description": "Enter location. If you leave this field blank resource group location would be used."
}
},
"virtualNetworkName": {
"type": "String",
"defaultValue": "SQLMI-VNET",
"metadata": {
"description": "Enter virtual network name. If you leave this field blank name will be created by the template."
}
},
"addressPrefix": {
"defaultValue": "10.0.0.0/16",
"type": "String",
"metadata": {
"description": "Enter virtual network address prefix."
}
},
"subnetName": {
"type": "String",
"defaultValue": "ManagedInstances",
"metadata": {
"description": "Enter subnet name. If you leave this field blank name will be created by the template."
}
},
"subnetPrefix": {
"defaultValue": "10.0.0.0/24",
"type": "String",
"metadata": {
"description": "Enter subnet address prefix."
}
},
"skuName": {
"defaultValue": "GP_Gen5",
"allowedValues": [
"GP_Gen5",
"BC_Gen5"
],
"type": "String",
"metadata": {
"description": "Enter sku name."
}
},
"vCores": {
"defaultValue": 16,
"allowedValues": [
8,
16,
24,
32,
40,
64,
80
],
"type": "Int",
"metadata": {
"description": "Enter number of vCores."
}
},
"storageSizeInGB": {
"defaultValue": 256,
"minValue": 32,
"maxValue": 8192,
"type": "Int",
"metadata": {
"description": "Enter storage size."
}
},
"licenseType": {
"defaultValue": "LicenseIncluded",
"allowedValues": [
"BasePrice",
"LicenseIncluded"
],
"type": "String",
"metadata": {
"description": "Enter license type."
}
}
},
"variables": {
"networkSecurityGroupName": "[concat('SQLMI-', parameters('managedInstanceName'), '-NSG')]",
"routeTableName": "[concat('SQLMI-', parameters('managedInstanceName'), '-Route-Table')]"
},
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-06-01",
"name": "[variables('networkSecurityGroupName')]",
"location": "[parameters('location')]",
"properties": {
"securityRules": [
{
"name": "allow_tds_inbound",
"properties": {
"description": "Allow access to data",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "1433",
"sourceAddressPrefix": "VirtualNetwork",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 1000,
"direction": "Inbound"
}
},
{
"name": "allow_redirect_inbound",
"properties": {
"description": "Allow inbound redirect traffic to Managed Instance inside the virtual network",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "11000-11999",
"sourceAddressPrefix": "VirtualNetwork",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 1100,
"direction": "Inbound"
}
},
{
"name": "deny_all_inbound",
"properties": {
"description": "Deny all other inbound traffic",
"protocol": "*",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Deny",
"priority": 4096,
"direction": "Inbound"
}
},
{
"name": "deny_all_outbound",
"properties": {
"description": "Deny all other outbound traffic",
"protocol": "*",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Deny",
"priority": 4096,
"direction": "Outbound"
}
}
]
}
},
{
"type": "Microsoft.Network/routeTables",
"apiVersion": "2020-06-01",
"name": "[variables('routeTableName')]",
"location": "[parameters('location')]",
"properties": {
"disableBgpRoutePropagation": false
}
},
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-06-01",
"name": "[parameters('virtualNetworkName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[variables('routeTableName')]",
"[variables('networkSecurityGroupName')]"
],
"properties": {
"addressSpace": {
"addressPrefixes": [
"[parameters('addressPrefix')]"
]
},
"subnets": [
{
"name": "[parameters('subnetName')]",
"properties": {
"addressPrefix": "[parameters('subnetPrefix')]",
"routeTable": {
"id": "[resourceId('Microsoft.Network/routeTables', variables('routeTableName'))]"
},
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
},
"delegations": [
{
"name": "miDelegation",
"properties": {
"serviceName": "Microsoft.Sql/managedInstances"
}
}
]
}
}
]
}
},
{
"type": "Microsoft.Sql/managedInstances",
"apiVersion": "2020-11-01-preview",
"name": "[parameters('managedInstanceName')]",
"location": "[parameters('location')]",
"dependsOn": [
"[parameters('virtualNetworkName')]"
],
"sku": {
"name": "[parameters('skuName')]"
},
"identity": {
"type": "UserAssigned",
"UserAssignedIdentities": {
"[parameters('user_identity_resource_id')]": {}
}
},
"properties": {
"subnetId": "[resourceId('Microsoft.Network/virtualNetworks/subnets', parameters('virtualNetworkName'), parameters('subnetName'))]",
"storageSizeInGB": "[parameters('storageSizeInGB')]",
"vCores": "[parameters('vCores')]",
"licenseType": "[parameters('licenseType')]",
"PrimaryUserAssignedIdentityId": "[parameters('user_identity_resource_id')]",
"administrators": {
"login": "[parameters('aad_admin_name')]",
"sid": "[parameters('aad_admin_objectid')]",
"tenantId": "[parameters('aad_admin_tenantid')]",
"principalType": "[parameters('aad_admin_type')]",
"azureADOnlyAuthentication": "[parameters('aad_only_auth')]"
}
}
}
]
}
相關內容
意見反應
此頁面對您有幫助嗎?
意見反應
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:https://aka.ms/ContentUserFeedback。
提交並檢視相關的意見反應