Operasi throughput (RU/s) dengan Azure CLI untuk database atau grafik untuk Azure Cosmos DB untuk MongoDB
BERLAKU UNTUK: MongoDB
Skrip dalam artikel ini membuat database MongoDB dengan throughput bersama dan kumpulan dengan throughput khusus, lalu memperbarui throughput untuk keduanya. Skrip kemudian bermigrasi dari throughput standar ke autoscale kemudian membaca nilai throughput autoscale setelah dimigrasikan.
Jika Anda tidak memiliki Langganan Azure, buat Akun gratis Azure sebelum memulai.
Prasyarat
Gunakan lingkungan Bash di Azure Cloud Shell. Untuk informasi selengkapnya, lihat Mulai Cepat untuk Bash di Azure Cloud Shell.
Jika Anda lebih suka menjalankan perintah referensi CLI secara lokal, instal Azure CLI. Jika Anda menjalankan Windows atau macOS, pertimbangkan untuk menjalankan Azure CLI dalam kontainer Docker. Untuk informasi lebih lanjut, lihat Cara menjalankan Azure CLI di kontainer Docker.
Jika Anda menggunakan instalasi lokal, masuk ke Azure CLI dengan menggunakan perintah login az. Untuk menyelesaikan proses autentikasi, ikuti langkah-langkah yang ditampilkan di terminal Anda. Untuk opsi masuk lainnya, lihat Masuk dengan Azure CLI.
Saat Anda diminta, instal ekstensi Azure CLI pada penggunaan pertama. Untuk informasi selengkapnya tentang ekstensi, lihat Menggunakan ekstensi dengan Azure CLI.
Jalankan versi az untuk menemukan versi dan pustaka dependen yang diinstal. Untuk meningkatkan ke versi terbaru, jalankan peningkatan az.
- Artikel ini memerlukan versi 2.30 atau yang lebih baru. Jalankan
az --version
untuk menemukan versinya. Jika Anda perlu memasang atau meningkatkan, lihat Memasang Azure CLI. Jika menggunakan Azure Cloud Shell, versi terbaru sudah terinstal.
Sampel skrip
Meluncurkan Azure Cloud Shell
Azure Cloud Shell adalah shell interaktif gratis yang dapat Anda gunakan untuk menjalankan langkah-langkah dalam artikel ini. Shell ini memiliki alat Azure umum yang telah dipasang sebelumnya dan dikonfigurasi untuk digunakan dengan akun Anda.
Untuk membuka Cloud Shell, cukup pilih Cobalah dari sudut kanan atas blok kode. Anda juga dapat meluncurkan Cloud Shell di tab browser terpisah dengan membuka https://shell.azure.com.
Saat Cloud Shell terbuka, verifikasi bahwa Bash dipilih untuk lingkungan Anda. Sesi berikutnya akan menggunakan Azure CLI dalam lingkungan Bash, Pilih Salin untuk menyalin blok kode, tempelkan ke Cloud Shell, lalu tekan Enter untuk menjalankannya.
Masuk ke Azure
Cloud Shell diautentikasi secara otomatis dengan akun awal yang digunakan untuk masuk. Gunakan skrip berikut untuk masuk menggunakan langganan lain, mengganti subscriptionId dengan ID langganan Azure Anda.
Jika Anda tidak memiliki Langganan Azure, buat Akun gratis Azure sebelum memulai.
subscription="subscriptionId" # Set Azure subscription ID here
az account set -s $subscription # ...or use 'az login'
Untuk informasi selengkapnya, lihat mengatur langganan aktif atau masuk secara interaktif.
Jalankan skrip
# Throughput operations for a MongoDB API database and collection
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-cosmosdb-rg-$randomIdentifier"
tag="throughput-mongodb-cosmosdb"
account="msdocs-account-cosmos-$randomIdentifier" #needs to be lower case
database="msdocs-db-mongo-cosmos"
collection="collection1"
originalThroughput=400
updateThroughput=500
# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag
# Create a Cosmos account for MongoDB API
echo "Creating $account"
az cosmosdb create --name $account --resource-group $resourceGroup --kind MongoDB
# Create a MongoDB API database
echo "Creating $database with $originalThroughput"
az cosmosdb mongodb database create --account-name $account --resource-group $resourceGroup --name $database --throughput $originalThroughput
# Define a minimal index policy for the collection
printf '[ {"key": {"keys": ["_id"]}} ]' > idxpolicy-$randomIdentifier.json
# Create a MongoDB API collection
az cosmosdb mongodb collection create --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --shard "user_id" --throughput $originalThroughput --idx @idxpolicy-$randomIdentifier.json
# Clean up temporary index policy file
rm -f "idxpolicy-$randomIdentifier.json"
# Throughput operations for MongoDB API database
# Read the current throughput
# Read the minimum throughput
# Make sure the updated throughput is not less than the minimum
# Update the throughput
# Migrate between standard (manual) and autoscale throughput
# Read the autoscale max throughput
# Retrieve the current provisioned database throughput
az cosmosdb mongodb database throughput show --resource-group $resourceGroup --account-name $account --name $database --query resource.throughput -o tsv
# Retrieve the minimum allowable database throughput
minimumThroughput=$(az cosmosdb mongodb database throughput show --resource-group $resourceGroup --account-name $account --name $database --query resource.minimumThroughput -o tsv)
echo $minimumThroughput
# Make sure the updated throughput is not less than the minimum allowed throughput
if [ $updateThroughput -lt $minimumThroughput ]; then
updateThroughput=$minimumThroughput
fi
# Update database throughput
echo "Updating $database throughput to $updateThroughput"
az cosmosdb mongodb database throughput update --account-name $account --resource-group $resourceGroup --name $database --throughput $updateThroughput
# Migrate the database from standard (manual) throughput to autoscale throughput
az cosmosdb mongodb database throughput migrate --account-name $account --resource-group $resourceGroup --name $database --throughput-type 'autoscale'
# Retrieve current autoscale provisioned max database throughput
az cosmosdb mongodb database throughput show --account-name $account --resource-group $resourceGroup --name $database --query resource.autoscaleSettings.maxThroughput -o tsv
# Throughput operations for MongoDB API collection
# Read the current throughput
# Read the minimum throughput
# Make sure the updated throughput is not less than the minimum
# Update the throughput
# Migrate between standard (manual) and autoscale throughput
# Read the autoscale max throughput
# Retrieve the current provisioned collection throughput
az cosmosdb mongodb collection throughput show --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --query resource.throughput -o tsv
# Retrieve the minimum allowable collection throughput
minimumThroughput=$(az cosmosdb mongodb collection throughput show --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --query resource.minimumThroughput -o tsv)
echo $minimumThroughput
# Make sure the updated throughput is not less than the minimum allowed throughput
if [ $updateThroughput -lt $minimumThroughput ]; then
updateThroughput=$minimumThroughput
fi
# Update collection throughput
echo "Updating collection throughput to $updateThroughput"
az cosmosdb mongodb collection throughput update --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --throughput $updateThroughput
# Migrate the collection from standard (manual) throughput to autoscale throughput
az cosmosdb mongodb collection throughput migrate --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --throughput 'autoscale'
# Retrieve the current autoscale provisioned max collection throughput
az cosmosdb mongodb collection throughput show --account-name $account --resource-group $resourceGroup --database-name $database --name $collection --query resource.autoscaleSettings.maxThroughput -o tsv
Membersihkan sumber daya
Gunakan perintah berikut untuk menghapus grup sumber daya dan semua sumber daya yang terkait dengannya menggunakan perintah az group delete - kecuali Anda masih memiliki kebutuhan untuk sumber daya ini. Beberapa sumber daya ini mungkin membutuhkan beberapa waktu untuk dibuat dan dihapus.
az group delete --name $resourceGroup
Referensi sampel
Skrip ini menggunakan perintah berikut. Setiap perintah dalam tabel ditautkan ke dokumentasi spesifik perintah.
Perintah | Catatan |
---|---|
az group create | Membuat grup sumber daya tempat semua sumber daya disimpan. |
buat az cosmosdb | Membuat akun Azure Cosmos DB. |
buat database az cosmosdb mongodb | Membuat database API MongoDB Azure Cosmos DB. |
membuat koleksi az cosmosdb mongodb | Membuat koleksi API MongoDB Azure Cosmos DB. |
pembaruan throughput database az cosmosdb mongodb | Perbarui RU untuk database API MongoDB Azure Cosmos DB. |
pembaruan throughput koleksi az cosmosdb mongodb | Perbarui RU untuk koleksi API MongoDB Azure Cosmos DB. |
migrasi throughput database az cosmosdb mongodb | Melakukan migrasi throughput untuk database. |
migrasi throughput koleksi az cosmosdb mongodb | Migrasikan throughput untuk koleksi. |
hapus grup az | Menghapus grup sumber daya termasuk semua sumber daya berlapis. |
Langkah berikutnya
Untuk informasi selengkapnya tentang Azure Cosmos DB CLI, lihat Dokumentasi CLI Azure Cosmos DB.