快速入门:使用 PowerShell 在 Azure VM 上创建 SQL Server
本快速入门分步演示了如何使用 Azure PowerShell 在 SQL Server 虚拟机 (VM) 上创建 Windows SQL Server。
提示
- 本快速入门提供了一种可快速预配并连接到 SQL Server VM 的路径。 要详细了解可用于创建 SQL Server VM 的其他 Azure PowerShell 选项,请参阅 SQL Server VM 预配指南(使用 Azure PowerShell)。
- 如果对 SQL Server 虚拟机有任何疑问,请参阅常见问题解答。
若要完成本快速入门,你应具有:
- Azure 订阅。 如果没有 Azure 订阅,请在开始前创建一个试用订阅。
- 最新版本的 Azure PowerShell
备注
本文使用 Azure Az PowerShell 模块,这是与 Azure 交互时推荐使用的 PowerShell 模块。 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az。
打开 PowerShell,并通过运行 Connect-AzAccount 命令建立对 Azure 帐户的访问权限,然后使用 Set-AzContext 设置订阅上下文。
Connect-AzAccount -Environment AzureChinaCloud Set-AzContext -subscription <Subscription ID>
看到“登录”窗口时,请输入凭据。 使用登录 Azure 门户时所用的相同电子邮件和密码。
为唯一的资源组名称定义变量,并为所有 VM 资源提供目标 Azure 区域的位置。 然后,运行 New-AzResourceGroup 以创建资源组。 为了简化本快速入门的其余部分,其余命令将此名称作为基础用于其他资源名称。
$ResourceGroupName = "sqlvm1"
$Location = "China North 3"
$ResourceGroupParams = @{
Name = $ResourceGroupName
Location = $Location
Tag = @{Owner="SQLDocs-Samples"}
}
New-AzResourceGroup @ResourceGroupParams
使用 New-AzVirtualNetworkSubnetConfig、New-AzVirtualNetwork 和 New-AzPublicIpAddress 创建虚拟网络、子网和公共 IP 地址。 这些资源用来与虚拟机建立网络连接,以及连接到 Internet。
$SubnetName = $ResourceGroupName + "subnet" $VnetName = $ResourceGroupName + "vnet" $PipName = $ResourceGroupName + $(Get-Random) # Create a subnet configuration $SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix 192.168.1.0/24 # Create a virtual network $Vnet = New-AzVirtualNetwork -ResourceGroupName $ResourceGroupName -Location $Location ` -Name $VnetName -AddressPrefix 192.168.0.0/16 -Subnet $SubnetConfig # Create a public IP address and specify a DNS name $Pip = New-AzPublicIpAddress -ResourceGroupName $ResourceGroupName -Location $Location ` -AllocationMethod Static -IdleTimeoutInMinutes 4 -Name $PipName
在使用 New-AzNetworkSecurityRuleConfig 配置了允许远程桌面 (RDP) 和 SQL Server 连接的规则后,使用 New-AzNetworkSecurityGroup 创建网络安全组。
# Rule to allow remote desktop (RDP) $NsgRuleRDP = New-AzNetworkSecurityRuleConfig -Name "RDPRule" -Protocol Tcp ` -Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * ` -DestinationAddressPrefix * -DestinationPortRange 3389 -Access Allow #Rule to allow SQL Server connections on port 1433 $NsgRuleSQL = New-AzNetworkSecurityRuleConfig -Name "MSSQLRule" -Protocol Tcp ` -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * ` -DestinationAddressPrefix * -DestinationPortRange 1433 -Access Allow # Create the network security group $NsgName = $ResourceGroupName + "nsg" $Nsg = New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroupName ` -Location $Location -Name $NsgName ` -SecurityRules $NsgRuleRDP,$NsgRuleSQL
使用 New-AzNetworkInterface 创建网络接口。
$InterfaceName = $ResourceGroupName + "int" $Interface = New-AzNetworkInterface -Name $InterfaceName ` -ResourceGroupName $ResourceGroupName -Location $Location ` -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $Pip.Id ` -NetworkSecurityGroupId $Nsg.Id
定义登录到 VM 所需的凭据。 用户名为“azureadmin”。 确保在运行命令之前更改 <密码>。
# Define a credential object $userName = "azureadmin" $SecurePassword = ConvertTo-SecureString '<strong password>' ` -AsPlainText -Force $Cred = New-Object System.Management.Automation.PSCredential ($userName, $securePassword)
使用 New-AzVMConfig 创建虚拟机配置对象,然后使用 New-AzVM 创建 VM。 以下命令在 Windows Server 2022 上创建 SQL Server 2022 Developer Edition VM。
# Create a virtual machine configuration $VMName = $ResourceGroupName + "VM" $VMConfig = New-AzVMConfig -VMName $VMName -VMSize Standard_DS13_V2 | Set-AzVMOperatingSystem -Windows -ComputerName $VMName -Credential $Cred -ProvisionVMAgent -EnableAutoUpdate | Set-AzVMSourceImage -PublisherName "MicrosoftSQLServer" -Offer "sql2022-ws2022" -Skus "sqldev-gen2" -Version "latest" | Add-AzVMNetworkInterface -Id $Interface.Id # Create the VM New-AzVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VMConfig
提示
创建 VM 需要几分钟时间。
若要获取门户集成和 SQL VM 功能,必须向 SQL IaaS 代理扩展注册。
为此,请先使用 Register-AzResourceProvider 将订阅注册到资源提供程序:
# Register the SQL IaaS Agent extension to your subscription
Register-AzResourceProvider -ProviderNamespace Microsoft.SqlVirtualMachine
接下来,使用 New-AzSqlVM 将 SQL Server VM 注册到 SQL IaaS 代理扩展:
$License = 'PAYG'
# Register SQL Server VM with the extension
New-AzSqlVM -Name $VMName -ResourceGroupName $ResourceGroupName -Location $Location `
-LicenseType $License
使用 Get-AzPublicIpAddress 检索新 VM 的公共 IP 地址。
Get-AzPublicIpAddress -ResourceGroupName $ResourceGroupName | Select IpAddress
将返回的 IP 地址作为命令行参数传递给 mstsc,以便启动到新 VM 的远程桌面会话。
mstsc /v:<publicIpAddress>
出现输入凭据的提示时,请选择输入另一个帐户的凭据。 输入带前置反斜杠的用户名(例如
\azureadmin
),以及此前在本快速入门中设置的密码。
登录到远程桌面会话以后,从开始菜单启动 SQL Server Management Studio 2017。
在“连接到服务器”对话框中,保留默认设置。 服务器名称是 VM 的名称。 身份验证设置为“Windows 身份验证”。 选择“连接”。
你现在已通过本地方式连接到 SQL Server。 若要进行远程连接,必须通过 Azure 门户或手动配置连接性。
如果不需要让 VM 持续运行,可以在不使用它时将它停止,以免产生不必要的费用。 以下命令可停止 VM,但会保留它供将来使用。
Stop-AzVM -Name $VMName -ResourceGroupName $ResourceGroupName
还可以使用 Remove-AzResourceGroup 命令永久删除与虚拟机关联的所有资源。 这样做也会永久删除虚拟机,因此请小心使用此命令。
本快速入门使用 Azure PowerShell 创建了一个 SQL Server 2022 虚拟机。 若要详细了解如何将数据迁移到新的 SQL Server,请参阅以下文章。