瀏覽至 Azure 入口網站中的 [選取 SQL 部署] 選項頁面。
如果您尚未登入 Azure 入口網站,請在出現提示時登入。
在 SQL 資料庫下,將資源類型設定為單一資料庫,然後選取 [建立]。
在建立 SQL 資料庫表單的基本資料索引標籤上,在專案詳細資料下,選取想要的 Azure 訂用帳戶。
針對 [資源群組],選取 [新建],輸入資源群組的名稱,然後選取 [確定]。
針對 [資料庫名稱],輸入您想要的資料庫名稱。
在伺服器中,選取 [建立新的],並以下列值填寫新伺服器表單:
- 伺服器名稱:輸入唯一的伺服器名稱。 對於 Azure 中的所有伺服器,伺服器名稱必須是全域唯一的,而不只在訂用帳戶中是唯一的。
- 伺服器管理員登入:輸入管理員登入名稱,例如
azureuser
。
- 密碼:輸入符合密碼需求的密碼,然後在 [確認密碼] 欄位中再次輸入。
- 位置:從下拉式清單中選取位置
完成時,選取 [下一步:網路功能],為於頁面底部。
在網路功能索引標籤的連線方法中,選取 [公用端點]。
針對 [防火牆規則],將 [新增目前的用戶端 IP 位址] 設定為 [是]。 將 [允許 Azure 服務和資源存取此伺服器] 設定為 [否]。
選取頁面底部的 [下一步:安全性]。
在 [安全性] 索引標籤的 [身分識別] 下方,選取 [設定身分識別]。
在 [身分識別] 窗格的 [使用者指派受控識別] 底下,選取 [新增]。 選取所需的訂用帳戶,然後在 [使用者指派的受控識別] 下,從選取的訂用帳戶選取所需的使用者指派受控識別。 然後選取 [選取] 按鈕。
在 [主要身分識別] 下方,選取上一個步驟中選取的相同使用者指派受控識別。
注意
如果系統指派的受控識別是主要身分識別,則 [主要身分識別] 欄位必須是空的。
選取 [套用]
選取頁面底部的 [檢閱 + 建立]
檢閱 [檢閱 + 建立] 頁面之後,選取 [建立]。
Azure CLI 命令 az sql server create
用來佈建新的邏輯伺服器。 下列命令以使用者指派的受控識別來佈建新的伺服器。 此範例也會啟用 僅限 Microsoft Entra 驗證,併為伺服器設定 Microsoft Entra 管理員。
將自動建立伺服器 SQL 系統管理員登入,密碼會設定為隨機密碼。 由於此處建立伺服器已停用 SQL 驗證連線,因此不使用 SQL 系統管理員登入。
Microsoft Entra 系統管理員伺服器會是您設定 <AzureADAccount>
的帳戶,而且可用來管理伺服器。
取代範例中的下列值:
<subscriptionId>
:在 Azure 入口網站中可以找到您的訂用帳戶識別碼
<ResourceGroupName>
:邏輯伺服器的資源群組名稱
<managedIdentity>
:使用者指派的受控識別。 也可以用來作為主要身分識別。
<primaryIdentity>
:要用來作為伺服器身分識別的主要身分識別
<AzureADAccount>
:可以是 Microsoft Entra 使用者或群組。 例如,DummyLogin
<AzureADAccountSID>
:使用者的 Microsoft Entra 物件識別碼
<ServerName>
:使用唯一的邏輯伺服器名稱
<Location>
:伺服器的位置,例如 westus
或 centralus
subscription_id=<subscriptionId>
resource_group=<ResourceGroupName>
managed_identity=<managedIdentity>
primary_identity=<primaryIdentity>
azure_ad_account=<AzureADAccount>
azure_ad_account_sid=<AzureADAccountSID>
server_name=<ServerName>
location=<Location>
az sql server create \
--assign-identity \
--identity-type UserAssigned \
--user-assigned-identity-id /subscriptions/$subscription_id/resourceGroups/$resource_group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$managed_identity \
--primary-user-assigned-identity-id /subscriptions/$subscription_id/resourceGroups/$resource_group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$primary_identity \
--enable-ad-only-auth \
--external-admin-principal-type User \
--external-admin-name $azure_ad_account \
--external-admin-sid $azure_ad_account_sid \
-g $resource_group \
-n $server_name \
-l $location
如需詳細資訊,請參閱 az sql server create。
注意
上述範例只以使用者指派的受控識別來佈建伺服器。 您可以將 --identity-type
設定為 UserAssigned,SystemAssigned
,表示想要隨伺服器同時建立這兩種受控識別。
若要在建立後檢查伺服器狀態,請參閱下列命令:
az sql server show --name <ServerName> --resource-group <ResourceGroupName> --expand-ad-admin
PowerShell 命令 New-AzSqlServer
用來佈建新的 Azure SQL 邏輯伺服器。 下列命令以使用者指派的受控識別來佈建新的伺服器。 此範例也會啟用 僅限 Microsoft Entra 驗證,併為伺服器設定 Microsoft Entra 管理員。
將自動建立伺服器 SQL 系統管理員登入,密碼會設定為隨機密碼。 由於此處建立伺服器已停用 SQL 驗證連線,因此不使用 SQL 系統管理員登入。
Microsoft Entra 系統管理員伺服器會是您設定 <AzureADAccount>
的帳戶,而且可用來管理伺服器。
取代範例中的下列值:
<ResourceGroupName>
:您邏輯伺服器的資源群組名稱
<Location>
:伺服器的位置,例如 West US
或 Central US
<ServerName>
:使用唯一的邏輯伺服器名稱
<subscriptionId>
:在 Azure 入口網站中可以找到您的訂用帳戶識別碼
<managedIdentity>
:使用者指派的受控識別。 也可以用來作為主要身分識別
<primaryIdentity>
:要用來作為伺服器身分識別的主要身分識別
<AzureADAccount>
:可以是 Microsoft Entra 使用者或群組。 例如, DummyLogin
$server = @{
ResourceGroupName = "<ResourceGroupName>"
Location = "<Location>"
ServerName = "<ServerName>"
ServerVersion = "12.0"
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
}
New-AzSqlServer @server
如需詳細資訊,請參閱 New-AzSqlServer。
注意
上述範例只以使用者指派的受控識別來佈建伺服器。 您可以將 -IdentityType
設定為 "UserAssigned,SystemAssigned"
,表示想要隨伺服器同時建立這兩種受控識別。
若要在建立後檢查伺服器狀態,請參閱下列命令:
Get-AzSqlServer -ResourceGroupName "<ResourceGroupName>" -ServerName "<ServerName>" -ExpandActiveDirectoryAdministrator
伺服器 - 建立或更新 REST API 可用來以使用者指派的受控識別建立邏輯伺服器。
下列腳本會布建邏輯伺服器、將 Microsoft Entra 系統管理員設定為 <AzureADAccount>
,並啟用 僅限 Microsoft Entra 驗證。 還會自動建立伺服器 SQL 系統管理員登入,密碼會設定為隨機密碼。 由於此佈建已停用 SQL 驗證連線,因此不會使用 SQL 管理員登入。
Microsoft Entra 系統管理員 <AzureADAccount>
可用來在布建完成時管理伺服器。
取代範例中的下列值:
<tenantId>
:您可以前往 Azure 入口網站,然後移至您的 Microsoft Entra ID 資源。 在 [概觀] 窗格中,應該會看到您的租用戶識別碼
<subscriptionId>
您可以在 Azure 入口網站中找到訂閱識別碼
<ServerName>
:使用唯一的邏輯伺服器名稱
<ResourceGroupName>
:您邏輯伺服器的資源群組名稱
<AzureADAccount>
:可以是 Microsoft Entra 使用者或群組。 例如, DummyLogin
<Location>
:伺服器的位置,例如 westus2
或 centralus
<objectId>
:您可以移至 Azure 入口網站,並前往您的 Microsoft Entra ID 資源。 在 [ 使用者 ] 窗格中,搜尋 Microsoft Entra 使用者並尋找其 物件識別碼
<managedIdentity>
:使用者指派的受控識別。 也可以用來作為主要身分識別
<primaryIdentity>
:要用來作為伺服器身分識別的主要身分識別
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"
$authUrl = "https://login.windows.net/$tenantId"
$serverName = "<ServerName>"
$resourceGroupName = "<ResourceGroupName>"
Login-AzAccount -tenantId $tenantId
# login as a user with SQL Server Contributor role or higher
# Get a token
$result = Get-MsalToken -RedirectUri $uri -ClientId $clientId -TenantId $tenantId -Scopes "https://management.core.windows.net/.default"
#Authetication header
$authHeader = @{
'Content-Type'='application\json; '
'Authorization'=$result.CreateAuthorizationHeader()
}
# Enable Azure AD-only auth and sets a user-managed identity as the server identity
# No server admin is specified, and only Azure AD admin and Azure AD-only authentication is set to true
# Server admin (login and password) is generated by the system
# The sid is the Azure AD Object ID for the user
# Replace all values in a <>
$body = '{
"location": "<Location>",
"identity": {"type" : "UserAssigned", "UserAssignedIdentities" : {"/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<managedIdentity>" : {}}},
"properties": { "PrimaryUserAssignedIdentityId":"/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<primaryIdentity>","administrators":{ "login":"<AzureADAccount>", "sid":"<objectId>", "tenantId":"<tenantId>", "principalType":"User", "azureADOnlyAuthentication":true }
}
}'
# Provision the server
Invoke-RestMethod -Uri https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Sql/servers/$serverName/?api-version=2020-11-01-preview -Method PUT -Headers $authHeader -Body $body -ContentType "application/json"
注意
上述範例只以使用者指派的受控識別來佈建伺服器。 您可以將 "type"
設定為 "UserAssigned,SystemAssigned"
,表示想要隨伺服器同時建立這兩種受控識別。
若要檢查伺服器狀態,您可以使用下列指令碼:
$uri = 'https://management.azure.com/subscriptions/'+$subscriptionId+'/resourceGroups/'+$resourceGroupName+'/providers/Microsoft.Sql/servers/'+$serverName+'?api-version=2020-11-01-preview&$expand=administrators/activedirectory'
$responce=Invoke-WebRequest -Uri $uri -Method PUT -Headers $authHeader -Body $body -ContentType "application/json"
$responce.statuscode
$responce.content
下列 ARM 範本範例以使用者指派的受控識別建立 Azure SQL Database 邏輯伺服器。 此範本也會為伺服器新增 Microsoft Entra 管理員集,並啟用 僅限 Microsoft Entra 的驗證,但可以從範本範例中移除。
如需詳細資訊和 ARM 範本,請參閱適用於 Azure SQL 資料庫 和 SQL 受管理執行個體 的 Azure Resource Manager 範本。
使用 Azure 入口網站中的自訂部署,然後在編輯器中建置您自己的範本。 接下來,將設定貼進範例之後,加以儲存。
若要取得使用者指派的受控識別資源識別碼,請在 Azure 入口網站中搜尋受控識別。 尋找您的受控識別,並移至 [屬性]。 UMI 資源識別碼的範例看起來像 /subscriptions/<subscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<managedIdentity>
。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
"server": {
"type": "string",
"defaultValue": "[uniqueString('sql', resourceGroup().id)]",
"metadata": {
"description": "The name of the logical server."
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
},
"aad_admin_name": {
"type": "String",
"metadata": {
"description": "The name of the Azure AD admin for the SQL server."
}
},
"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>."
}
},
"AdminLogin": {
"minLength": 1,
"type": "String"
},
"AdminLoginPassword": {
"type": "SecureString"
}
},
"resources": [
{
"type": "Microsoft.Sql/servers",
"apiVersion": "2020-11-01-preview",
"name": "[parameters('server')]",
"location": "[parameters('location')]",
"identity": {
"type": "UserAssigned",
"UserAssignedIdentities": {
"[parameters('user_identity_resource_id')]": {}
}
},
"properties": {
"administratorLogin": "[parameters('AdminLogin')]",
"administratorLoginPassword": "[parameters('AdminLoginPassword')]",
"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')]"
}
}
}
]
}