使用 Bicep 管理 Azure Cosmos DB for MongoDB 資源
適用於: MongoDB
在本文中,您將了解如何使用 Bicep 來部署和管理 MongoDB API、資料庫和集合的 Azure Cosmos DB 帳戶。
本文說明適用於 API for MongoDB 帳戶的 Bicep 範例。 您也可以找到適用於 SQL、Cassandra、Gremlin 和資料表 API 的 Bicep 範例。
重要
- 帳戶名稱限制為 44 個字元,全部小寫。
- 若要變更輸送量值,請使用已更新的 RU/秒重新部署範本。
- 您在新增或移除 Azure Cosmos DB 帳戶的位置時,無法同時修改其他屬性。 這些作業必須個別執行。
若要建立下列任何 Azure Cosmos DB 資源,請將下列範例複製到新的 Bicep 檔案中。 當您使用不同的名稱和值來部署相同資源的多個執行個體時,可以選擇建立參數檔案以供使用。 部署 Azure Resource Manager 範本的方式有很多種,包括 Azure CLI、Azure PowerShell 和 Cloud Shell。
具有自動調整佈建輸送量的 API for MongoDB
此範本會為 API for MongoDB (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
}
}
}
}
具有標準佈建輸送量的 API for MongoDB
為 API for MongoDB (3.2、3.6、4.0 或 4.2) 建立 Azure Cosmos DB 帳戶,其內含有兩個會在資料庫層級共用 400 RU/s 標準 (手動) 輸送量的集合。
@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 進行容量規劃嗎? 您可以使用現有資料庫叢集的相關資訊進行容量規劃。
- 如果您知道現有資料庫叢集中的虛擬核心和伺服器數目,請參閱使用虛擬核心或 vCPU 來估計要求單位
- 如果您知道目前資料庫工作負載的一般要求率,請參閱使用 Azure Cosmos DB 容量規劃工具來估計要求單位