快速入门:使用 Azure 资源管理器模板在 Azure SQL 数据库中创建单一数据库Quickstart: Create a single database in Azure SQL Database using the Azure Resource Manager template

在 Azure SQL 数据库中创建数据库时,创建单一数据库是最快速且最简单的部署选项。Creating a single database is the quickest and simplest deployment option for creating a database in Azure SQL Database. 本快速入门介绍如何使用 Azure 资源管理器模板创建单一数据库。This quickstart shows you how to create a single database using the Azure Resource Manager template.

资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。Resource Manager template is a JavaScript Object Notation (JSON) file that defines the infrastructure and configuration for your project. 该模板使用声明性语法,使你可以声明要部署的内容,而不需要编写一系列编程命令来进行创建。The template uses declarative syntax, which lets you state what you intend to deploy without having to write the sequence of programming commands to create it. 若要详细了解如何开发资源管理器模板,请参阅资源管理器文档If you want to learn more about developing Resource Manager templates, see Resource Manager documentation.

如果没有 Azure 订阅,可创建一个 1 元人民币试用帐户If you don't have an Azure subscription, create a 1rmb trial account.

先决条件Prerequisites

None

创建单一数据库Create a single database

单一数据库有一组通过两个购买模型中的一个定义的计算、内存和存储资源。A single database has a defined set of compute, memory, IO, and storage resources using one of two purchasing models. 创建单一数据库时,也定义一个 SQL 数据库服务器来管理它并将它放置在指定区域的 Azure 资源组中。When you create a single database, you also define a SQL Database server to manage it and place it within Azure resource group in a specified region.

查看模板Review the template

本快速入门中使用的模板来自 Azure 快速入门模板The template used in this quickstart is from Azure Quickstart templates.

{
 "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
 "contentVersion": "1.0.0.0",
 "parameters": {
   "serverName": {
     "type": "string",
     "defaultValue":"[uniqueString('sql', resourceGroup().id)]",
     "metadata": {
       "description": "The name of the SQL logical server."
     }
   },
   "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."
     }
   },
   "enableADS": {
     "defaultValue": true,
     "type": "bool",
     "metadata": {
       "description": "Enable Advanced Data Security, the user deploying the template must have an administrator or owner permissions."
     }
   },
   "allowAzureIPs": {
     "defaultValue": true,
     "type": "bool",
     "metadata": {
       "description": "Allow Azure services to access server."
     }
   },
   "connectionType": {
     "defaultValue": "Default",
     "allowedValues": [ "Default", "Redirect", "Proxy" ],
     "type": "string",
     "metadata": {
       "description": "SQL logical server connection type."
     }
   }
 },
 "variables": {
    "serverResourceGroupName": "[resourceGroup().name]",
    "subscriptionId": "[subscription().subscriptionId]",
    "uniqueStorage": "[uniqueString(variables('subscriptionId'), variables('serverResourceGroupName'), parameters('location'))]",
    "storageName": "[tolower(concat('sqlva', variables('uniqueStorage')))]",
    "uniqueRoleGuid": "[guid(resourceId('Microsoft.Storage/storageAccounts', variables('storageName')), variables('storageBlobContributor'), resourceId('Microsoft.Sql/servers', parameters('serverName')))]",
    "StorageBlobContributor": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'ba92f5b4-2d11-453d-a403-e96b0029c9fe')]"
  },
 "resources": [
   {
     "type": "Microsoft.Sql/servers",
     "apiVersion": "2018-06-01-preview",
     "name": "[parameters('serverName')]",
     "location": "[parameters('location')]",
     "identity": "[if(parameters('enableADS'), json('{\"type\":\"SystemAssigned\"}'), json('null'))]",
     "properties": {
       "administratorLogin": "[parameters('administratorLogin')]",
       "administratorLoginPassword": "[parameters('administratorLoginPassword')]",
       "version": "12.0"
     },
     "resources": [
       {
         "condition": "[parameters('allowAzureIPs')]",
         "type": "firewallRules",
         "apiVersion": "2018-06-01-preview",
         "name": "AllowAllWindowsAzureIps",
         "location": "[parameters('location')]",
         "dependsOn": [
           "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]"
         ],
         "properties": {
            "endIpAddress": "0.0.0.0",
           "startIpAddress": "0.0.0.0"
         }
       },
       {
         "condition": "[parameters('enableADS')]",
         "type": "securityAlertPolicies",
         "apiVersion": "2017-03-01-preview",
         "name": "Default",
         "dependsOn": [
           "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]"
         ],
         "properties": {
           "state": "Enabled",
           "emailAccountAdmins": true
         }
       },
       {
         "condition": "[parameters('enableADS')]",
         "type": "vulnerabilityAssessments",
         "apiVersion": "2018-06-01-preview",
         "name": "Default",
         "dependsOn": [
           "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]",
           "[resourceId('Microsoft.Sql/servers/securityAlertPolicies', parameters('serverName'), 'Default')]",
           "[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]"
         ],
         "properties": {
           "storageContainerPath": "[if(parameters('enableADS'), concat(reference(resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))).primaryEndpoints.blob, 'vulnerability-assessment'), json('null'))]",
           "recurringScans": {
             "isEnabled": true,
             "emailSubscriptionAdmins": true
           }
         }
       },
       {
         "type": "connectionPolicies",
         "apiVersion": "2014-04-01",
         "name": "Default",
         "dependsOn": [
           "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]"
         ],
         "properties": {
           "connectionType": "[parameters('connectionType')]"
         }
       }
     ]
   },
   {
     "condition": "[parameters('enableADS')]",
     "type": "Microsoft.Storage/storageAccounts",
     "apiVersion": "2019-06-01",
     "name": "[variables('storageName')]",
     "location": "[parameters('location')]",
     "sku": {
       "name": "Standard_LRS"
     },
     "kind": "StorageV2",
     "resources": [
       {
         "condition": "[parameters('enableADS')]",
         "type": "Microsoft.Storage/storageAccounts/providers/roleAssignments",
         "apiVersion": "2018-09-01-preview",
         "name": "[concat(variables('storageName'), '/Microsoft.Authorization/', variables('uniqueRoleGuid') )]",
         "dependsOn": [
           "[resourceId('Microsoft.Sql/servers', parameters('serverName'))]",
           "[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]"
         ],
         "properties": {
           "roleDefinitionId": "[variables('StorageBlobContributor')]",
           "principalId": "[reference(resourceId('Microsoft.Sql/servers', parameters('serverName')), '2018-06-01-preview', 'Full').identity.principalId]",
           "scope": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]",
           "principalType": "ServicePrincipal"
         }
       }
     ]
   }
 ]
}

该模板中定义了以下资源:These resources are defined in the template:

可以在 Azure 快速入门模板中找到更多 Azure SQL 数据库模板示例。More Azure SQL database template samples can be found in Azure Quickstart Templates.

部署模板Deploy the template

$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. chinaeast2)"
$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/101-sql-logical-server/azuredeploy.json" -administratorLogin $adminUser -administratorLoginPassword $adminPassword

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

验证部署Validate the deployment

若要查询数据库,请参阅查询数据库To query the database, see Query the database.

清理资源Clean up resources

如果希望转到后续步骤,请保留此资源组、数据库服务器和单一数据库。Keep this resource group, database server, and single database if you want to go to the Next steps. 后续步骤展示了如何使用各种方法连接和查询数据库。The next steps show you how to connect and query your database using different methods.

若要删除资源组,请执行以下操作:To delete the resource group:

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

后续步骤Next steps