快速入门:使用 Azure PowerShell 在 Windows 虚拟机上创建 SQL Server

适用于:Azure 上的 SQL Server

本快速入门逐步讲解如何使用 Azure PowerShell 创建 SQL Server 虚拟机 (VM)。

提示

获取 Azure 订阅

如果没有 Azure 订阅,请在开始前创建一个试用订阅

获取 Azure PowerShell

注意

本文使用 Azure Az PowerShell 模块,这是与 Azure 交互时推荐使用的 PowerShell 模块。 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

配置 PowerShell

  1. 打开 PowerShell,通过运行 Connect-AzAccount 命令建立对 Azure 帐户的访问。

    Connect-AzAccount -Environment AzureChinaCloud
    
  2. 看到“登录”窗口时,请输入凭据。 使用登录 Azure 门户时所用的相同电子邮件和密码。

创建资源组

  1. 使用唯一的资源组名称定义一个变量。 为了简化本快速入门的其余部分,其余命令将此名称作为基础用于其他资源名称。

    $ResourceGroupName = "sqlvm1"
    
  2. 为所有 VM 资源定义目标 Azure 区域的位置。

    $Location = "China East"
    
  3. 创建资源组。

    New-AzResourceGroup -Name $ResourceGroupName -Location $Location
    

配置网络设置

  1. 创建虚拟网络、子网和公共 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
    
  2. 创建网络安全组。 配置允许远程桌面 (RDP) 和 SQL Server 连接的规则。

    # 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
    
  3. 创建网络接口。

    $InterfaceName = $ResourceGroupName + "int"
    $Interface = New-AzNetworkInterface -Name $InterfaceName `
       -ResourceGroupName $ResourceGroupName -Location $Location `
       -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $Pip.Id `
       -NetworkSecurityGroupId $Nsg.Id
    

创建 SQL VM

  1. 定义登录到 VM 所需的凭据。 用户名为“azureadmin”。 确保在运行命令之前更改 <密码>。

    # Define a credential object
    $SecurePassword = ConvertTo-SecureString '<password>' `
       -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential ("azureadmin", $securePassword)
    
  2. 创建虚拟机配置对象,然后创建 VM。 以下命令在 Windows Server 2016 上创建 SQL Server 2017 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 "SQL2017-WS2016" -Skus "SQLDEV" -Version "latest" |
       Add-AzVMNetworkInterface -Id $Interface.Id
    
    # Create the VM
    New-AzVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $VMConfig
    

    提示

    创建 VM 需要几分钟时间。

注册 SQL VM RP

若要获取门户集成和 SQL VM 功能,必须向 SQL IaaS 代理扩展注册

通过远程桌面连接到 VM

  1. 使用以下命令检索新 VM 的公共 IP 地址。

    Get-AzPublicIpAddress -ResourceGroupName $ResourceGroupName | Select IpAddress
    
  2. 将返回的 IP 地址作为命令行参数传递给 mstsc,以便启动到新 VM 的远程桌面会话。

    mstsc /v:<publicIpAddress>
    
  3. 出现输入凭据的提示时,请选择输入另一个帐户的凭据。 输入带前置反斜杠的用户名(例如 \azureadmin),以及此前在本快速入门中设置的密码。

连接到 SQL Server

  1. 登录到远程桌面会话以后,从开始菜单启动 SQL Server Management Studio 2017

  2. 在“连接到服务器”对话框中,保留默认设置。 服务器名称是 VM 的名称。 身份验证设置为“Windows 身份验证”。 选择“连接”。

你现在已通过本地方式连接到 SQL Server。 若要进行远程连接,必须通过 Azure 门户或手动配置连接性

清理资源

如果不需要让 VM 持续运行,可以在不使用它时将它停止,以免产生不必要的费用。 以下命令可停止 VM,但会保留它供将来使用。

Stop-AzVM -Name $VMName -ResourceGroupName $ResourceGroupName

还可以使用 Remove-AzResourceGroup 命令永久删除与虚拟机关联的所有资源。 这样做也会永久删除虚拟机,因此请小心使用此命令。

后续步骤

本快速入门使用 Azure PowerShell 创建了一个 SQL Server 2017 虚拟机。 若要详细了解如何将数据迁移到新的 SQL Server,请参阅以下文章。