快速入门:使用 Azure PowerShell 在 Windows 虚拟机上创建 SQL ServerQuickstart: Create SQL Server on a Windows virtual machine with Azure PowerShell

适用于: Azure VM 上的 SQL Server

本快速入门逐步讲解如何使用 Azure PowerShell 创建 SQL Server 虚拟机 (VM)。This quickstart steps through creating a SQL Server virtual machine (VM) with Azure PowerShell.


获取 Azure 订阅Get an Azure subscription

如果没有 Azure 订阅,请在开始前创建一个试用订阅If you don't have an Azure subscription, create a Trial Subscription before you begin.

获取 Azure PowerShellGet Azure PowerShell


本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 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 new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

配置 PowerShellConfigure PowerShell

  1. 打开 PowerShell,通过运行 Connect-AzAccount 命令建立对 Azure 帐户的访问。Open PowerShell and establish access to your Azure account by running the Connect-AzAccount command.

    Connect-AzAccount -Environment AzureChinaCloud
  2. 看到“登录”窗口时,请输入凭据。When you see the sign-in window, enter your credentials. 使用登录 Azure 门户时所用的相同电子邮件和密码。Use the same email and password that you use to sign in to the Azure portal.

创建资源组Create a resource group

  1. 使用唯一的资源组名称定义一个变量。Define a variable with a unique resource group name. 为了简化本快速入门的其余部分,其余命令将此名称作为基础用于其他资源名称。To simplify the rest of the quickstart, the remaining commands use this name as a basis for other resource names.

    $ResourceGroupName = "sqlvm1"
  2. 为所有 VM 资源定义目标 Azure 区域的位置。Define a location of a target Azure region for all VM resources.

    $Location = "China East"
  3. 创建资源组。Create the resource group.

    New-AzResourceGroup -Name $ResourceGroupName -Location $Location

配置网络设置Configure network settings

  1. 创建虚拟网络、子网和公共 IP 地址。Create a virtual network, subnet, and a public IP address. 这些资源用来与虚拟机建立网络连接,以及连接到 Internet。These resources are used to provide network connectivity to the virtual machine and connect it to the internet.

    $SubnetName = $ResourceGroupName + "subnet"
    $VnetName = $ResourceGroupName + "vnet"
    $PipName = $ResourceGroupName + $(Get-Random)
    # Create a subnet configuration
    $SubnetConfig = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix
    # Create a virtual network
    $Vnet = New-AzVirtualNetwork -ResourceGroupName $ResourceGroupName -Location $Location `
       -Name $VnetName -AddressPrefix -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. 创建网络安全组。Create a network security group. 配置允许远程桌面 (RDP) 和 SQL Server 连接的规则。Configure rules to allow remote desktop (RDP) and SQL Server connections.

    # 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. 创建网络接口。Create the network interface.

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

创建 SQL VMCreate the SQL VM

  1. 定义登录到 VM 所需的凭据。Define your credentials to sign in to the VM. 用户名为“azureadmin”。The username is "azureadmin". 请确保在运行命令之前更改 <password>。Make sure you change <password> before running the command.

    # Define a credential object
    $SecurePassword = ConvertTo-SecureString '<password>' `
       -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential ("azureadmin", $securePassword)
  2. 创建虚拟机配置对象,然后创建 VM。Create a virtual machine configuration object and then create the VM. 以下命令在 Windows Server 2016 上创建 SQL Server 2017 Developer Edition VM。The following command creates a SQL Server 2017 Developer Edition VM on Windows Server 2016.

    # 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 需要几分钟时间。It takes several minutes to create the VM.

安装 SQL IaaS 代理Install the SQL IaaS Agent

若要获取门户集成和 SQL VM 功能,必须安装 SQL Server IaaS 代理扩展To get portal integration and SQL VM features, you must install the SQL Server IaaS Agent Extension. 若要在新 VM 上安装该代理,请在创建 VM 后运行以下命令。To install the agent on the new VM, run the following command after the VM is created.

Set-AzVMSqlServerExtension -ResourceGroupName $ResourceGroupName -VMName $VMName -name "SQLIaasExtension" -version "2.0" -Location $Location

通过远程桌面连接到 VMRemote desktop into the VM

  1. 使用以下命令检索新 VM 的公共 IP 地址。Use the following command to retrieve the public IP address for the new VM.

    Get-AzPublicIpAddress -ResourceGroupName $ResourceGroupName | Select IpAddress
  2. 将返回的 IP 地址作为命令行参数传递给 mstsc,以便启动到新 VM 的远程桌面会话。Pass the returned IP address as a command-line parameter to mstsc to start a Remote Desktop session into the new VM.

    mstsc /v:<publicIpAddress>
  3. 出现输入凭据的提示时,请选择输入另一个帐户的凭据。When prompted for credentials, choose to enter credentials for a different account. 输入带前置反斜杠的用户名(例如 \azureadmin),以及此前在本快速入门中设置的密码。Enter the username with a preceding backslash (for example, \azureadmin), and the password that you set previously in this quickstart.

连接到 SQL ServerConnect to SQL Server

  1. 登录到远程桌面会话以后,从开始菜单启动 SQL Server Management Studio 2017After signing in to the Remote Desktop session, launch SQL Server Management Studio 2017 from the start menu.

  2. 在“连接到服务器”对话框中,保留默认设置。In the Connect to Server dialog box, keep the defaults. 服务器名称是 VM 的名称。The server name is the name of the VM. 身份验证设置为“Windows 身份验证”。Authentication is set to Windows Authentication. 选择“连接”。Select Connect.

你现在已通过本地方式连接到 SQL Server。You're now connected to SQL Server locally. 若要进行远程连接,必须通过 Azure 门户或手动配置连接性If you want to connect remotely, you must configure connectivity from the Azure portal or manually.

清理资源Clean up resources

如果不需要让 VM 持续运行,可以在不使用它时将它停止,以免产生不必要的费用。If you don't need the VM to run continuously, you can avoid unnecessary charges by stopping it when not in use. 以下命令可停止 VM,但会保留它供将来使用。The following command stops the VM but leaves it available for future use.

Stop-AzVM -Name $VMName -ResourceGroupName $ResourceGroupName

还可以使用 Remove-AzResourceGroup 命令永久删除与虚拟机关联的所有资源。You can also permanently delete all resources associated with the virtual machine with the Remove-AzResourceGroup command. 这样做也会永久删除虚拟机,因此请小心使用此命令。Doing so permanently deletes the virtual machine as well, so use this command with care.

后续步骤Next steps

本快速入门使用 Azure PowerShell 创建了一个 SQL Server 2017 虚拟机。In this quickstart, you created a SQL Server 2017 virtual machine using Azure PowerShell. 若要详细了解如何将数据迁移到新的 SQL Server,请参阅以下文章。To learn more about how to migrate your data to the new SQL Server, see the following article.