用于管理 Azure SQL 数据库 DNS 别名的 PowerShellPowerShell for DNS Alias to Azure SQL Database

适用于:是Azure SQL 数据库是Azure Synapse Analytics (SQL DW)APPLIES TO: yesAzure SQL Database yesAzure Synapse Analytics (SQL DW)

本文提供了一个 PowerShell 脚本,演示如何为托管 Azure SQL 数据库的 SQL 服务器管理 DNS 别名。This article provides a PowerShell script that demonstrates how you can manage a DNS alias for the SQL server hosting your Azure SQL Database.

备注

本文进行了更新,以便使用 Azure PowerShell Az 模块或 Azure CLI。This article has been updated to use either the Azure PowerShell Az module or Azure CLI. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020.

若要详细了解 Az 模块和 AzureRM 兼容性,请参阅 Azure Powershell Az 模块简介To learn more about the Az module and AzureRM compatibility, see Introducing the Azure PowerShell Az module. 有关安装说明,请参阅安装 Azure PowerShell安装 Azure CLIFor installation instructions, see Install Azure PowerShell or Install Azure CLI.

连接字符串中的 DNS 别名DNS alias in connection string

若要连接逻辑 SQL 服务器,如 SQL Server Management Studio (SSMS) 客户端可以提供的 DNS 别名名称而不是真正的服务器名称。To connect a logical SQL server, a client such as SQL Server Management Studio (SSMS) can provide the DNS alias name instead of the true server name. 在下面的示例服务器字符串中,别名 any 的唯一别名的名称- 替换四个节点服务器字符串中的第一个圆点分隔节点:In the following example server string, the alias any-unique-alias-name replaces the first dot-delimited node in the four node server string:

<yourServer>.database.chinacloudapi.cn

先决条件Prerequisites

如果想要运行演示这篇文章中提供的 PowerShell 脚本,适用以下先决条件:If you want to run the demo PowerShell script given in this article, the following prerequisites apply:

  • Azure 订阅和帐户。如需 1 元试用版,请参阅 Azure 试用版An Azure subscription and account, for a 1rmb trial, see Azure trials
  • 两台服务器Two servers

示例Example

以下代码示例一开始就将文字值分配给多个变量。The following code example starts by assigning literal values to several variables.

若要运行此代码,请编辑占位符值,使之与系统中的实际值匹配。To run the code, edit the placeholder values to match real values in your system.

使用的 cmdlet 如下:The cmdlets used are the following:

若要进行安装或升级,请参阅安装 Azure PowerShell 模块To install or upgrade, see Install Azure PowerShell module.

powershell_ise.exe 中使用 Get-Module -ListAvailable Az 来查找版本。Use Get-Module -ListAvailable Az in powershell_ise.exe, to find the version.

$subscriptionName = '<subscriptionName>';
$sqlServerDnsAliasName = '<aliasName>';
$resourceGroupName = '<resourceGroupName>';  
$sqlServerName = '<sqlServerName>';
$resourceGroupName2 = '<resourceGroupNameTwo>'; # can be same or different than $resourceGroupName
$sqlServerName2 = '<sqlServerNameTwo>'; # must be different from $sqlServerName.

# login to Azure
Connect-AzAccount -SubscriptionName $subscriptionName -Environment AzureChinaCloud;
$subscriptionId = Get-AzSubscription -SubscriptionName $subscriptionName;

Write-Host 'Assign an alias to server 1...';
New-AzSqlServerDnsAlias -ResourceGroupName $resourceGroupName -ServerName $sqlServerName `
    -Name $sqlServerDnsAliasName;

Write-Host 'Get the aliases assigned to server 1...';
Get-AzSqlServerDnsAlias -ResourceGroupName $resourceGroupName -ServerName $sqlServerName;

Write-Host 'Move the alias from server 1 to server 2...';
Set-AzSqlServerDnsAlias -ResourceGroupName $resourceGroupName2 -TargetServerName $sqlServerName2 `
    -Name $sqlServerDnsAliasName `
    -SourceServerResourceGroup $resourceGroupName -SourceServerName $sqlServerName `
    -SourceServerSubscriptionId $subscriptionId.Id;

Write-Host 'Get the aliases assigned to server 2...';
Get-AzSqlServerDnsAlias -ResourceGroupName $resourceGroupName2 -ServerName $sqlServerName2;

Write-Host 'Remove the alias from server 2...';
Remove-AzSqlServerDnsAlias -ResourceGroupName $resourceGroupName2 -ServerName $sqlServerName2 `
    -Name $sqlServerDnsAliasName;

后续步骤Next steps

有关 SQL 数据库的 DNS 别名功能的完整说明,请参阅 Azure SQL 数据库的 DNS 别名For a full explanation of the DNS alias feature for SQL Database, see DNS alias for Azure SQL Database.