将 PowerShell 与 Azure 资源管理器模板配合使用,以创建托管实例Use PowerShell with an Azure Resource Manager template to create a managed instance

适用于:是Azure SQL 托管实例 APPLIES TO: yesAzure SQL Managed Instance

可以使用 Azure PowerShell 库和 Azure 资源管理器模板创建托管实例。You can create a managed instance by using the Azure PowerShell library and Azure Resource Manager templates.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

备注

本文进行了更新,以便使用新的 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.

本教程需要 Azure PowerShell 1.4.0 或更高版本。This tutorial requires Azure PowerShell 1.4.0 or later. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 需要运行 Connect-AzAccount -Environment AzureChinaCloud 以创建与 Azure 的连接。You need to run Connect-AzAccount -Environment AzureChinaCloud to create a connection to Azure.

Azure PowerShell 命令可以使用预定义的 Azure 资源管理器模板启动部署。Azure PowerShell commands can start deployment using a predefined Azure Resource Manager template. 可以在模板中指定以下属性:The following properties can be specified in the template:

  • 托管实例名称Managed instance name
  • SQL 管理员用户名和密码。SQL administrator username and password.
  • 实例的大小(内核数和最大存储大小)。Size of the instance (number of cores and max storage size).
  • 要将实例放置在其中的 VNet 和子网。VNet and subnet where the instance will be placed.
  • 实例的服务器级排序规则(预览版)。Server-level collation of the instance (preview).

实例名称、SQL 管理员用户名、VNet/子网和排序规则以后无法更改。Instance name, SQL administrator username, VNet/subnet, and collation cannot be changed later. 其他实例属性可以更改。Other instance properties can be changed.

先决条件Prerequisites

此示例假定已为托管实例创建了有效的网络环境修改了现有的 VNetThis sample assumes that you have created a valid network environment or modified an existing VNet for your managed instance. 如有必要,可以使用单独的 Azure 资源管理器模板准备网络环境。You can prepare the network environment using a separate Azure Resource Manager template, if necessary.

此示例使用 cmdlet New-AzResourceGroupDeploymentGet-AzVirtualNetwork,因此请确保已安装以下 PowerShell 模块:The sample uses the cmdlets New-AzResourceGroupDeployment and Get-AzVirtualNetwork, so make sure that you have installed the following PowerShell modules:

Install-Module Az.Network
Install-Module Az.Resources

Azure Resource Manager 模板Azure Resource Manager template

将以下脚本保存到 .json 文件中,并记下文件位置:Save the following script into a .json file, and note the file location:

{
    "$schema": "https://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.1",
    "parameters": {
        "instance": {
            "type": "string"
        },
        "user": {
            "type": "string"
        },
        "pwd": {
            "type": "securestring"
        },
        "subnetId": {
            "type": "string"
        }
    },
    "resources": [
        {
            "name": "[parameters('instance')]",
            "location": "China East 2",
            "tags": {
                "Description":"GP Instance with custom instance collation - Serbian_Cyrillic_100_CS_AS"
            },
            "sku": {
                "name": "GP_Gen5",
                "tier": "GeneralPurpose"
            },
            "properties": {
                "administratorLogin": "[parameters('user')]",
                "administratorLoginPassword": "[parameters('pwd')]",
                "subnetId": "[parameters('subnetId')]",
                "storageSizeInGB": 256,
                "vCores": 8,
                "licenseType": "LicenseIncluded",
                "hardwareFamily": "Gen5",
                "collation": "Serbian_Cyrillic_100_CS_AS"
            },
            "type": "Microsoft.Sql/managedInstances",
            "identity": {
                "type": "SystemAssigned"
            },
            "apiVersion": "2015-05-01-preview"
        }
    ]
}

使用以前保存的 .json 文件的正确文件路径更新以下 PowerShell 脚本,并更改脚本中对象的名称:Update the following PowerShell script with the correct file path for the .json file you saved previously, and change the names of the objects in the script:

$subscriptionId = "ed827499-xxxx-xxxx-xxxx-xxxxxxxxxx"
Select-AzSubscription -SubscriptionId $subscriptionId

# Managed instance properties
$resourceGroup = "rg_mi"
$location = "China East 2"
$name = "managed-instance-name"
$user = "miSqlAdmin"
$secpasswd = ConvertTo-SecureString "<Put some strong password here>" -AsPlainText -Force

# Network configuration
$vNetName = "my_vnet"
$vNetResourceGroup = "rg_mi_vnet"
$subnetName = "ManagedInstances"
$vNet = Get-AzVirtualNetwork -Name $vNetName -ResourceGroupName $vNetResourceGroup
$subnet = Get-AzVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNet
$subnetId = $subnet.Id

# Deploy instance using Azure Resource Manager template:
New-AzResourceGroupDeployment  -Name MyDeployment -ResourceGroupName $resourceGroup  `
                                    -TemplateFile 'C:\...\create-managed-instance.json' `
                                    -instance $name -user $user -pwd $secpasswd -subnetId $subnetId

该脚本完成后,可以通过所有 Azure 服务和配置的 IP 地址访问托管实例。Once the script completes, the managed instance can be accessed from all Azure services and the configured IP address.

后续步骤Next steps

有关 Azure PowerShell 的详细信息,请参阅 Azure PowerShell 文档For more information on Azure PowerShell, see Azure PowerShell documentation.

Azure SQL 托管实例的其他 PowerShell 脚本示例可在 Azure SQL 托管实例 PowerShell 脚本中找到。Additional PowerShell script samples for Azure SQL Managed Instance can be found in Azure SQL Managed Instance PowerShell scripts.