使用 PowerShell 與 Azure Resource Manager 範本在 Azure SQL Database 中建立受控執行個體Use PowerShell with Azure Resource Manager template to create a managed instance in Azure SQL Database

您可以使用 Azure PowerShell 程式庫和 Azure Resource Manager 範本建立 Azure SQL Database 受控執行個體。Azure SQL Database Managed Instance can be created using Azure PowerShell library and Azure Resource Manager templates.

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free 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 Cloud ShellUse Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell 可讓您使用 bashPowerShell 以與 Azure 服務搭配使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要啟動 Azure Cloud Shell:To launch Azure Cloud Shell:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的試試看範例
請前往 https://shell.azure.com 或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. <a href="https://shell.azure.com" title="啟動 Azure Cloud Shell
選取 Azure 入口網站右上角功能表列中的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

若要在 Azure Cloud Shell 中執行本文中的程式碼:To run the code in this article in Azure Cloud Shell:

  1. 啟動 Cloud Shell。Launch Cloud Shell.
  2. 選取程式碼區塊上的 [複製] 按鈕,複製程式碼。Select the Copy button on a code block to copy the code.
  3. 在 Windows 和 Linux 上按 Ctrl+Shift+V;或在 macOS 上按 Cmd+Shift+V,將程式碼貼到 Cloud Shell工作階段中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
  4. 按下 Enter 鍵執行程式碼。Press Enter to run the code.

如果選擇在本機安裝和使用 PowerShell,此教學課程需要 AZ PowerShell 1.4.0 或更新版本。If you choose to install and use the PowerShell locally, this tutorial requires AZ PowerShell 1.4.0 or later. 如果您需要升級,請參閱安裝 Azure PowerShell 模組If you need to upgrade, see Install Azure PowerShell module. 如果您在本機執行 PowerShell,則也需要執行 Connect-AzAccount 以建立與 Azure 的連線。If you are running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.

Azure PowerShell 命令可以使用預先定義的 Azure Resource Manager 範本啟動部署。Azure PowerShell commands can start deployment using predefined Azure Resource Manager template. 範本中可指定下列屬性:The following properties can be specified in the template:

  • 執行個體名稱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 user name, 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 existing VNet for your Managed Instance. 此範例會使用 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

下列內容應放置在代表範本的檔案內,此範本將用來建立執行個體:The following content should be placed in a file that represents a template that will be used to create the instance:

{
    "$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": "West Central US",
            "tags": {
                "Description":"GP Instance with custom instance collation - Serbian_Cyrillic_100_CS_AS"
            },
            "sku": {
                "name": "GP_Gen4",
                "tier": "GeneralPurpose"
            },
            "properties": {
                "administratorLogin": "[parameters('user')]",
                "administratorLoginPassword": "[parameters('pwd')]",
                "subnetId": "[parameters('subnetId')]",
                "storageSizeInGB": 256,
                "vCores": 8,
                "licenseType": "LicenseIncluded",
                "hardwareFamily": "Gen4",
                "collation": "Serbian_Cyrillic_100_CS_AS"
            },
            "type": "Microsoft.Sql/managedInstances",
            "identity": {
                "type": "SystemAssigned"
            },
            "apiVersion": "2015-05-01-preview"
        }
    ]
}

我們假設已正確設定子網路的 Azure VNet 已存在。Assumption is that Azure VNet with the properly configured subnet already exists. 如果您沒有正確設定的子網路,請使用不同的 Azure 資源管理範本來準備網路環境,此動作可以獨立執行或包含在此範本中。If you do not have a properly configured subnet, prepare the network environment using separate Azure Resource Managed template that can be executed independently or included in this template.

將此檔案的內容儲存成 .json 檔案,並將檔案路徑放入下列 PowerShell 指令碼中,然後變更指令碼中的物件名稱:Save the content of this file as .json file, put the file path in the following PowerShell script, 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 = "West Central US"
$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 位址存取 SQL Database。Once the script has been successfully run, the SQL Database can be accessed from all Azure services and the configured IP address.

後續步驟Next steps

如需有關 Azure PowerShell 的詳細資訊,請參閱 Azure PowerShell 文件For more information on the Azure PowerShell, see Azure PowerShell documentation.

其他的 SQL Database PowerShell 指令碼範例可於 Azure SQL Database PowerShell 指令碼中找到。Additional SQL Database PowerShell script samples can be found in the Azure SQL Database PowerShell scripts.