使用 Bicep 管理 Azure Cosmos DB for MongoDB 资源
适用对象: MongoDB
本文介绍如何使用 Bicep 部署和管理 MongoDB API、数据库和集合的 Azure Cosmos DB 帐户。
本文介绍 MongoDB API 帐户的 Bicep 示例。 你还可以查找 SQL、Cassandra、Gremlin 和表 API 的 Bicep 示例。
重要
- 帐户名称限制为 44 个字符,全部小写。
- 若要更改吞吐量值,请用更新的 RU/s 重新部署模板。
- 当你在 Azure Cosmos DB 帐户中添加或删除位置时,无法同时修改其他属性。 必须单独执行这些操作。
若要创建以下任何 Azure Cosmos DB 资源,请将以下示例复制到新的 Bicep 文件中。 可以选择创建一个要在使用不同名称和值部署同一资源的多个实例时使用的参数文件。 可以通过多种方式部署 Azure 资源管理器模板,包括使用 Azure CLI 和 Azure PowerShell。
具有自动缩放预配吞吐量的 MongoDB API
此模板会创建 MongoDB API(3.2、3.6、4.0 或 4.2)的 Azure Cosmos DB 帐户,其中包含两个在数据库级别共享自动缩放吞吐量的集合。
@description('Cosmos DB account name')
param accountName string = 'mongodb-${uniqueString(resourceGroup().id)}'
@description('Location for the Cosmos DB account.')
param location string = resourceGroup().location
@description('The primary replica region for the Cosmos DB account.')
param primaryRegion string
@description('The secondary replica region for the Cosmos DB account.')
param secondaryRegion string
@description('Specifies the MongoDB server version to use.')
@allowed([
'3.2'
'3.6'
'4.0'
'4.2'
])
param serverVersion string = '4.2'
@description('The default consistency level of the Cosmos DB account.')
@allowed([
'Eventual'
'ConsistentPrefix'
'Session'
'BoundedStaleness'
'Strong'
])
param defaultConsistencyLevel string = 'Eventual'
@description('Max stale requests. Required for BoundedStaleness. Valid ranges, Single Region: 10 to 2147483647. Multi Region: 100000 to 2147483647.')
@minValue(10)
@maxValue(2147483647)
param maxStalenessPrefix int = 100000
@description('Max lag time (seconds). Required for BoundedStaleness. Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400.')
@minValue(5)
@maxValue(86400)
param maxIntervalInSeconds int = 300
@description('The name for the Mongo DB database')
param databaseName string
@description('Maximum autoscale throughput for the database shared with up to 25 collections')
@minValue(1000)
@maxValue(1000000)
param sharedAutoscaleMaxThroughput int = 1000
@description('The name for the first Mongo DB collection')
param collection1Name string
@description('The name for the second Mongo DB collection')
param collection2Name string
@description('Maximum dedicated autoscale throughput for the orders collection')
@minValue(1000)
@maxValue(1000000)
param dedicatedAutoscaleMaxThroughput int = 1000
var consistencyPolicy = {
Eventual: {
defaultConsistencyLevel: 'Eventual'
}
ConsistentPrefix: {
defaultConsistencyLevel: 'ConsistentPrefix'
}
Session: {
defaultConsistencyLevel: 'Session'
}
BoundedStaleness: {
defaultConsistencyLevel: 'BoundedStaleness'
maxStalenessPrefix: maxStalenessPrefix
maxIntervalInSeconds: maxIntervalInSeconds
}
Strong: {
defaultConsistencyLevel: 'Strong'
}
}
var locations = [
{
locationName: primaryRegion
failoverPriority: 0
isZoneRedundant: false
}
{
locationName: secondaryRegion
failoverPriority: 1
isZoneRedundant: false
}
]
resource account 'Microsoft.DocumentDB/databaseAccounts@2022-05-15' = {
name: toLower(accountName)
location: location
kind: 'MongoDB'
properties: {
consistencyPolicy: consistencyPolicy[defaultConsistencyLevel]
locations: locations
databaseAccountOfferType: 'Standard'
enableAutomaticFailover: true
apiProperties: {
serverVersion: serverVersion
}
capabilities: [
{
name: 'DisableRateLimitingResponses'
}
]
}
}
resource database 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases@2022-05-15' = {
parent: account
name: databaseName
properties: {
resource: {
id: databaseName
}
options: {
autoscaleSettings: {
maxThroughput: sharedAutoscaleMaxThroughput
}
}
}
}
resource collection1 'Microsoft.DocumentDb/databaseAccounts/mongodbDatabases/collections@2022-05-15' = {
parent: database
name: collection1Name
properties: {
resource: {
id: collection1Name
shardKey: {
user_id: 'Hash'
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
{
key: {
keys: [
'$**'
]
}
}
{
key: {
keys: [
'product_name'
'product_category_name'
]
}
}
]
}
}
}
resource collection2 'Microsoft.DocumentDb/databaseAccounts/mongodbDatabases/collections@2022-05-15' = {
parent: database
name: collection2Name
properties: {
resource: {
id: collection2Name
shardKey: {
company_id: 'Hash'
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
{
key: {
keys: [
'$**'
]
}
}
{
key: {
keys: [
'customer_id'
'order_id'
]
}
}
]
}
options: {
autoscaleSettings: {
maxThroughput: dedicatedAutoscaleMaxThroughput
}
}
}
}
具有标准预配吞吐量的 MongoDB API
创建 MongoDB API(3.2、3.6、4.0 或 4.2)的 Azure Cosmos DB 帐户,其中包含两个在数据库级别共享 400 RU/秒标准(手动)吞吐量的集合。
@description('Cosmos DB account name')
param accountName string = 'mongodb-${uniqueString(resourceGroup().id)}'
@description('Location for the Cosmos DB account.')
param location string = resourceGroup().location
@description('The primary replica region for the Cosmos DB account.')
param primaryRegion string
@description('The secondary replica region for the Cosmos DB account.')
param secondaryRegion string
@allowed([
'Eventual'
'ConsistentPrefix'
'Session'
'BoundedStaleness'
'Strong'
])
@description('The default consistency level of the Cosmos DB account.')
param defaultConsistencyLevel string = 'Eventual'
@allowed([
'3.2'
'3.6'
'4.0'
'4.2'
])
@description('Specifies the MongoDB server version to use.')
param serverVersion string = '4.2'
@minValue(10)
@maxValue(2147483647)
@description('Max stale requests. Required for BoundedStaleness. Valid ranges, Single Region: 10 to 2147483647. Multi Region: 100000 to 2147483647.')
param maxStalenessPrefix int = 100000
@minValue(5)
@maxValue(86400)
@description('Max lag time (seconds). Required for BoundedStaleness. Valid ranges, Single Region: 5 to 84600. Multi Region: 300 to 86400.')
param maxIntervalInSeconds int = 300
@description('The name for the Mongo DB database')
param databaseName string
@minValue(400)
@maxValue(1000000)
@description('The shared throughput for the Mongo DB database, up to 25 collections')
param sharedThroughput int = 400
@description('The name for the first Mongo DB collection')
param collection1Name string
@description('The name for the second Mongo DB collection')
param collection2Name string
@minValue(400)
@maxValue(1000000)
@description('The dedicated throughput for the orders collection')
param dedicatedThroughput int = 400
var consistencyPolicy = {
Eventual: {
defaultConsistencyLevel: 'Eventual'
}
ConsistentPrefix: {
defaultConsistencyLevel: 'ConsistentPrefix'
}
Session: {
defaultConsistencyLevel: 'Session'
}
BoundedStaleness: {
defaultConsistencyLevel: 'BoundedStaleness'
maxStalenessPrefix: maxStalenessPrefix
maxIntervalInSeconds: maxIntervalInSeconds
}
Strong: {
defaultConsistencyLevel: 'Strong'
}
}
var locations = [
{
locationName: primaryRegion
failoverPriority: 0
isZoneRedundant: false
}
{
locationName: secondaryRegion
failoverPriority: 1
isZoneRedundant: false
}
]
resource account 'Microsoft.DocumentDB/databaseAccounts@2022-05-15' = {
name: toLower(accountName)
location: location
kind: 'MongoDB'
properties: {
consistencyPolicy: consistencyPolicy[defaultConsistencyLevel]
locations: locations
databaseAccountOfferType: 'Standard'
enableAutomaticFailover: true
apiProperties: {
serverVersion: serverVersion
}
capabilities: [
{
name: 'DisableRateLimitingResponses'
}
]
}
}
resource database 'Microsoft.DocumentDB/databaseAccounts/mongodbDatabases@2022-05-15' = {
parent: account
name: databaseName
properties: {
resource: {
id: databaseName
}
options: {
throughput: sharedThroughput
}
}
}
resource collection1 'Microsoft.DocumentDb/databaseAccounts/mongodbDatabases/collections@2022-05-15' = {
parent: database
name: collection1Name
properties: {
resource: {
id: collection1Name
shardKey: {
user_id: 'Hash'
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
{
key: {
keys: [
'$**'
]
}
}
{
key: {
keys: [
'product_name'
'product_category_name'
]
}
}
]
}
}
}
resource collection2 'Microsoft.DocumentDb/databaseAccounts/mongodbDatabases/collections@2022-05-15' = {
parent: database
name: collection2Name
properties: {
resource: {
id: collection2Name
shardKey: {
company_id: 'Hash'
}
indexes: [
{
key: {
keys: [
'_id'
]
}
}
{
key: {
keys: [
'$**'
]
}
}
{
key: {
keys: [
'customer_id'
'order_id'
]
}
}
]
}
options: {
throughput: dedicatedThroughput
}
}
}
后续步骤
下面是一些其他资源:
- Bicep 文档
- 安装 Bicep 工具
- 尝试为迁移到 Azure Cosmos DB 进行容量规划? 可以使用有关现有数据库群集的信息进行容量规划。
- 如果只知道现有数据库群集中的 vCore 和服务器数量,请阅读使用 vCore 或 vCPU 估算请求单位
- 若知道当前数据库工作负载的典型请求速率,请阅读使用 Azure Cosmos DB 容量计划工具估算请求单位