使用 CREATE DATABASE 创建 Azure SQL 数据库时设置服务器默认值

问题描述 投票:0回答:1

知道 Azure 处理 SQL 数据库的方式与本地 SQL 不同,我很惊讶运行

CREATE DATABASE
SQL 查询实际上有效。但它提供的并不是我想要的。有没有办法定义用于创建数据库实例的资源? 例如,我想要一个无服务器供应。 我想看看我们是否可以将 Azure SQL DB 用于需要能够创建新数据库的软件,因此我需要一种方法来在服务器级别为新数据库设置默认资源。 我好像找不到这方面的资料。

azure-sql-database
1个回答
0
投票

您可以使用 ARM 模板 自定义 Azure SQL 数据库的创建。下面是示例模板:

{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "collation": {
        "type": "string"
    },
    "databaseName": {
        "type": "string"
    },
    "tier": {
        "type": "string"
    },
    "skuName": {
        "type": "string"
    },
    "maxSizeBytes": {
        "type": "int"
    },
    "serverName": {
        "type": "string"
    },
    "serverLocation": {
        "type": "string"
    },
    "sampleName": {
        "type": "string",
        "defaultValue": ""
    },
    "zoneRedundant": {
        "type": "bool",
        "defaultValue": false
    },
    "licenseType": {
        "type": "string",
        "defaultValue": ""
    },
    "readScaleOut": {
        "type": "string",
        "defaultValue": "Disabled"
    },
    "numberOfReplicas": {
        "type": "int",
        "defaultValue": 0
    },
    "minCapacity": {
        "type": "string",
        "defaultValue": ""
    },
    "autoPauseDelay": {
        "type": "string",
        "defaultValue": ""
    },
    "databaseTags": {
        "type": "object",
        "defaultValue": {}
    },
    "enableADS": {
        "type": "bool",
        "defaultValue": false
    },
    "enableVA": {
        "type": "bool",
        "defaultValue": false
    }
},
"resources": [
    {
        "condition": "[parameters('enableVA')]",
        "apiVersion": "2016-01-01",
        "type": "Microsoft.Storage/storageAccounts",
        "name": "[variables('storageName')]",
        "location": "[parameters('serverLocation')]",
        "sku": {
            "name": "Standard_LRS"
        },
        "kind": "Storage",
        "properties": {}
    },
    {
        "type": "Microsoft.Sql/servers/databases",
        "apiVersion": "2017-10-01-preview",
        "location": "[parameters('serverLocation')]",
        "tags": "[parameters('databaseTags')]",
        "name": "[concat(parameters('serverName'), '/', parameters('databaseName'))]",
        "properties": {
            "collation": "[parameters('collation')]",
            "maxSizeBytes": "[parameters('maxSizeBytes')]",
            "sampleName": "[parameters('sampleName')]",
            "zoneRedundant": "[parameters('zoneRedundant')]",
            "licenseType": "[parameters('licenseType')]",
            "readScale": "[parameters('readScaleOut')]",
            "readReplicaCount": "[parameters('numberOfReplicas')]",
            "minCapacity": "[parameters('minCapacity')]",
            "autoPauseDelay": "[parameters('autoPauseDelay')]"
        },
        "sku": {
            "name": "[parameters('skuName')]",
            "tier": "[parameters('tier')]"
        }
    },
    {
        "condition": "[parameters('enableADS')]",
        "apiVersion": "2017-03-01-preview",
        "type": "Microsoft.Sql/servers/securityAlertPolicies",
        "name": "[concat(parameters('serverName'), '/Default')]",
        "dependsOn": [
            "[concat('Microsoft.Sql/servers/', parameters('serverName'), '/databases/', parameters('databaseName'))]"
        ],
        "properties": {
            "state": "Enabled",
            "disabledAlerts": [],
            "emailAddresses": [],
            "emailAccountAdmins": true
        }
    },
    {
        "condition": "[parameters('enableVA')]",
        "apiVersion": "2018-06-01-preview",
        "type": "Microsoft.Sql/servers/vulnerabilityAssessments",
        "name": "[concat(parameters('serverName'), '/Default')]",
        "properties": {
            "storageContainerPath": "[if(parameters('enableVA'), concat(reference(variables('storageName'), '2018-02-01').primaryEndpoints.blob, 'vulnerability-assessment'), '')]",
            "storageAccountAccessKey": "[if(parameters('enableVA'), listKeys(variables('storageName'), '2018-02-01').keys[0].value, '')]",
            "recurringScans": {
                "isEnabled": true,
                "emailSubscriptionAdmins": true,
                "emails": []
            }
        },
        "dependsOn": [
            "[concat('Microsoft.Sql/servers/', parameters('serverName'), '/databases/', parameters('databaseName'))]",
            "[concat('Microsoft.Storage/storageAccounts/', variables('storageName'))]",
            "[concat('Microsoft.Sql/servers/', parameters('serverName'), '/securityAlertPolicies/Default')]"
        ]
    }
],
"variables": {
    "subscriptionId": "[subscription().subscriptionId]",
    "resourceGroupName": "[resourceGroup().name]",
    "uniqueStorage": "[uniqueString(variables('subscriptionId'), variables('resourceGroupName'), parameters('serverLocation'))]",
    "storageName": "[tolower(concat('sqlva', variables('uniqueStorage')))]"
}

}

下面您将找到模板的参数:

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "collation": {
        "value": "SQL_Latin1_General_CP1_CI_AS"
    },
    "databaseName": {
        "value": "MorilloServerless"
    },
    "tier": {
        "value": "GeneralPurpose"
    },
    "skuName": {
        "value": "GP_S_Gen5_1"
    },
    "maxSizeBytes": {
        "value": 34359738368
    },
    "sampleName": {
        "value": ""
    },
    "serverLocation": {
        "value": "eastus2"
    },
    "serverName": {
        "value": "morillo"
    },
    "zoneRedundant": {
        "value": false
    },
    "licenseType": {
        "value": "LicenseIncluded"
    },
    "readScaleOut": {
        "value": "Disabled"
    },
    "numberOfReplicas": {
        "value": 0
    },
    "minCapacity": {
        "value": "0.5"
    },
    "autoPauseDelay": {
        "value": "60"
    },
    "databaseTags": {
        "value": {}
    },
    "enableADS": {
        "value": false
    },
    "enableVA": {
        "value": false
    }
}

}

您也可以使用二头肌达到同样的目的。

@description('The name of the SQL logical server.')
param serverName string = uniqueString('sql', resourceGroup().id)

@description('The name of the SQL Database.')
param sqlDBName string = 'SampleDB'

@description('Location for all resources.')
param location string = resourceGroup().location

@description('The administrator username of the SQL logical server.')
param administratorLogin string

@description('The administrator password of the SQL logical server.')
@secure()
param administratorLoginPassword string

resource sqlServer 'Microsoft.Sql/servers@2022-05-01-preview' = {
  name: serverName
  location: location
  properties: {
    administratorLogin: administratorLogin
    administratorLoginPassword: administratorLoginPassword
  }
}

resource sqlDB 'Microsoft.Sql/servers/databases@2022-05-01-preview' = {
  parent: sqlServer
  name: sqlDBName
  location: location
  sku: {
    name: 'Standard'
    tier: 'Standard'
  }
}

您可以按照此处的说明从 C# 部署 ARM 模板。

© www.soinside.com 2019 - 2024. All rights reserved.