快速入門:使用 ARM 範本在 Azure SQL 資料庫中建立單一資料庫

適用於:Azure SQL Database

建立單一資料庫是在 Azure SQL 資料庫中建立資料庫的最快速、最簡單的選項。 本快速入門說明如何使用 Azure Resource Manager 範本 (ARM 範本) 建立單一資料庫。

ARM 範本 是一個 JavaScript Object Notation (JSON) 檔案,為您的專案定義基礎結構與設定。 範本使用宣告式語法。 在宣告式語法中,您不需要撰寫用於建立部署的程式設計命令序列,就能建立您所需的部署。

如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。 範本會在 Azure 入口網站中開啟。

Deploy to Azure

必要條件

如果您沒有 Azure 訂閱,請建立免費帳戶

檢閱範本

單一資料庫具有一組使用兩個購買模型之一的已定義計算、記憶體、IO 和儲存體資源。 當您建立單一資料庫時,也會定義要用來加以管理的伺服器,並將其置於指定區域中的 Azure 資源群組內。

本快速入門中使用的範本是來自 Azure 快速入門範本

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.12.40.16777",
      "templateHash": "16856611863128783179"
    }
  },
  "parameters": {
    "serverName": {
      "type": "string",
      "defaultValue": "[uniqueString('sql', resourceGroup().id)]",
      "metadata": {
        "description": "The name of the SQL logical server."
      }
    },
    "sqlDBName": {
      "type": "string",
      "defaultValue": "SampleDB",
      "metadata": {
        "description": "The name of the SQL Database."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "administratorLogin": {
      "type": "string",
      "metadata": {
        "description": "The administrator username of the SQL logical server."
      }
    },
    "administratorLoginPassword": {
      "type": "secureString",
      "metadata": {
        "description": "The administrator password of the SQL logical server."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Sql/servers",
      "apiVersion": "2022-05-01-preview",
      "name": "[parameters('serverName')]",
      "location": "[parameters('location')]",
      "properties": {
        "administratorLogin": "[parameters('administratorLogin')]",
        "administratorLoginPassword": "[parameters('administratorLoginPassword')]"
      }
    },
    {
      "type": "Microsoft.Sql/servers/databases",
      "apiVersion": "2022-05-01-preview",
      "name": "[format('{0}/{1}', parameters('serverName'), parameters('sqlDBName'))]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard",
        "tier": "Standard"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]"
      ]
    }
  ]
}

範本中定義了下列資源:

Azure 快速入門範本中提供了更多 Azure SQL 資料庫範本範例。

部署範本

從下列 PowerShell 程式代碼區塊中選取 [試用] 以開啟 Azure Cloud Shell。

$projectName = Read-Host -Prompt "Enter a project name that is used for generating resource names"
$location = Read-Host -Prompt "Enter an Azure location (i.e. centralus)"
$adminUser = Read-Host -Prompt "Enter the SQL server administrator username"
$adminPassword = Read-Host -Prompt "Enter the SQL Server administrator password" -AsSecureString

$resourceGroupName = "${projectName}rg"

New-AzResourceGroup -Name $resourceGroupName -Location $location
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.sql/sql-database/azuredeploy.json" -administratorLogin $adminUser -administratorLoginPassword $adminPassword

Read-Host -Prompt "Press [ENTER] to continue ..."

驗證部署

若要查詢資料庫,請參閱查詢資料庫

清除資源

若要移至後續步驟,請保留此資源群組、伺服器和單一資料庫。 後續步驟會示範如何使用不同的方法來連線和查詢資料庫。

刪除資源群組:

$resourceGroupName = Read-Host -Prompt "Enter the Resource Group name"
Remove-AzResourceGroup -Name $resourceGroupName

下一步