Managing shards MongoDB
You can create a sharded cluster or enable sharding later. After that, you can add and configure shards.
Make sure your shards consist of at least three MONGOD
hosts for higher availability. We do not recommend sharding small collections: query processing is faster with a standard replica cluster.
Alert
After cluster sharding is enabled:
- There is no way to disable sharding. The cluster will always maintain a minimum number of
MONGOS
,MONGOCFG
, orMONGOINFRA
hosts depending on the sharding type. - To access databases, use only the
MONGOS
orMONGOINFRA
hosts that route queries to shards. For this purpose, edit host addresses in your application code accordingly.
Enabling sharding
The Managed Service for MongoDB interface lets you quickly create a MongoDB sharding infrastructure.
To learn how to directly shard your MongoDB database and collections, see Sharding collections.
To enable sharding, you need:
- At least 3
MONGOINFRA
hosts for standard sharding. - At least 2
MONGOS
and 3MONGOCFG
hosts for advanced sharding.
Note
Sharding is not supported for hosts of b1.medium and b2.medium classes. If you do not see the Shards tab, upgrade the cluster host class to the supported value.
-
Go to the folder page
and select Managed Service for MongoDB. -
Click the cluster name and open the Shards tab.
-
Click Enable.
-
Select a sharding type:
-
Standard: Based on
MONGOINFRA
hosts. -
Advanced: Based on
MONGOS
andMONGOCFG
hosts.For more information, see Sharding.
Warning
After you enable sharding, you can't change its type.
-
-
Set the parameters of the hosts that will provide access to the sharded data.
-
Click Enable sharding.
The cluster will start updating, with the requested hosts and first shard of the cluster created.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
-
To enable standard cluster sharding using
MONGOINFRA
hosts, run this command (the list of supported parameters in the example is not exhaustive):yc managed-mongodb cluster enable-sharding \ --cluster-name=<cluster_name> \ --host type=mongoinfra,` `zone-id=<availability_zone>,` `subnet-name=<subnet_name> \ --host type=mongoinfra,` `zone-id=<availability_zone>,` `subnet-name=<subnet_name> \ --host type=mongoinfra,` `zone-id=<availability_zone>,` `subnet-name=<subnet_name> \ --mongoinfra resource-preset=<host_class>,` `disk-size=<storage_size_in_GB>,` `disk-type=<disk_type>
Where:
--cluster-name
: Cluster name you can get with a list of clusters in the folder.--host
: Host parameters:type
: Type (MONGOINFRA
).zone-id
: Availability zone.subnet-name
: Subnet name.
--mongoinfra
:MONGOINFRA
host parameters:resource-preset
: Host class.disk-size
: Storage size in GB.disk-type
: Disk type.
-
To enable advanced cluster sharding using
MONGOS
andMONGOCFG
hosts, run this command (the list of supported parameters in the example is not exhaustive):yc managed-mongodb cluster enable-sharding \ --cluster-name=<cluster_name> \ --host type=mongos,` `zone-id=<availability_zone>,` `subnet-name=<subnet_name> \ --host type=mongos,` `zone-id=<availability_zone>,` `subnet-name=<subnet_name> \ --mongos resource-preset=<host_class>,` `disk-size=<storage_size_in_GB>,` `disk-type=<disk_type> \ --host type=mongocfg,` `zone-id=<availability_zone>,` `subnet-name=<subnet_name> \ --host type=mongocfg,` `zone-id=<availability_zone>,` `subnet-name=<subnet_name> \ --host type=mongocfg,` `zone-id=<availability_zone>,` `subnet-name=<subnet_name> \ --mongocfg resource-preset=<host_class>,` `disk-size=<storage_size_in_GB>,` `disk-type=<disk_type>
Where:
--cluster-name
: Cluster name you can get with a list of clusters in the folder.--host
: Host parameters:type
: Type (MONGOS
orMONGOCFG
).zone-id
: Availability zone.subnet-name
: Subnet name.
--mongos
:MONGOS
host parameters:resource-preset
: Host class.disk-size
: Storage size in GB.disk-type
: Disk type.
--mongocfg
:MONGOCFG
host parameters:resource-preset
: Host class.disk-size
: Storage size in GB.disk-type
: Disk type.
-
Make sure the provider version you are using is 0.90 or higher:
terraform version
Result example:
Terraform v1.4.6 on darwin_arm64 + provider registry.terraform.io/yandex-cloud/yandex v0.91.0
If the version is older
Update the provider version:
-
In the folder with the
.tf
configuration file, add the credentials to environment variables. -
In the same folder, initialize the provider with the updated version specified in the configuration file:
terraform init -upgrade
-
Check the provider version:
terraform version
-
-
Open the current Terraform configuration file with an infrastructure plan.
For information on how to create this file, see Creating a cluster.
-
Add additional resources to the configuration file.
For standard sharding of the cluster using
MONGOINFRA
hostsresources_mongoinfra { resource_preset_id = "<host_class>" disk_type_id = "<disk_type>" disk_size = <storage_size_in_GB> } host { zone_id = "<availability_zone>" subnet_id = "<subnet_ID>" type = "mongoinfra" } host { zone_id = "<availability_zone>" subnet_id = "<subnet_ID>" type = "mongoinfra" } host { zone_id = "<availability_zone>" subnet_id = "<subnet_ID>" type = "mongoinfra" }
For advanced sharding of the cluster using
MONGOS
andMONGOCFG
hostsresources_mongos { resource_preset_id = "<host_class>" disk_type_id = "<disk_type>" disk_size = <storage_size_in_GB> } resources_mongocfg { resource_preset_id = "<host_class>" disk_type_id = "<disk_type>" disk_size = <storage_size_in_GB> } host { zone_id = "<availability_zone>" subnet_id = "<subnet_ID>" type = "mongos" } host { zone_id = "<availability_zone>" subnet_id = "<subnet_ID>" type = "mongos" } host { zone_id = "<availability_zone>" subnet_id = "<subnet_ID>" type = "mongocfg" } host { zone_id = "<availability_zone>" subnet_id = "<subnet_ID>" type = "mongocfg" } host { zone_id = "<availability_zone>" subnet_id = "<subnet_ID>" type = "mongocfg" }
-
Make sure the settings are correct.
-
Using the command line, navigate to the folder that contains the up-to-date Terraform configuration files with an infrastructure plan.
-
Run the command:
terraform validate
If there are errors in the configuration files, Terraform will point to them.
-
-
Confirm updating the resources.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
-
For more information, see the Terraform provider documentation
To enable cluster sharding, use the enableSharding REST API method for the Cluster resource or the ClusterService/EnableSharding gRPC API call and provide the cluster ID in the clusterId
request parameter.
You can get the cluster ID with a list of clusters in the folder.
Listing shards in a cluster
- Go to the folder page
and select Managed Service for MongoDB. - Click the cluster name and open the Shards tab.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To get a list of shards in a cluster, run the following command:
yc managed-mongodb shards list --cluster-name <cluster_name>
Result:
+------+
| NAME |
+------+
| rs01 |
| rs02 |
+------+
You can request the cluster name with the list of clusters in the folder.
To get a list of cluster shards, use the listShards REST API method for the Cluster resource or the ClusterService/ListShards gRPC API call and provide the cluster ID in the clusterId
request parameter.
You can get the cluster ID with a list of clusters in the folder.
Creating a shard
The number of shards in Managed Service for MongoDB clusters is limited by the CPU and RAM quotas available to DB clusters in your cloud. To check the resources currently in use, open the Quotas
- Go to the folder page
and select Managed Service for MongoDB. - Click the cluster name and open the Shards tab.
- Click Create shard.
- Enter a name for the shard and add the number of hosts you need.
- Click Create shard.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To create a shard, run the command below (not all the supported parameters are listed):
yc managed-mongodb shards add <new_shard_name> \
--cluster-name=<cluster_name> \
--host zone-id=<availability_zone>,`
`subnet-name=<subnet_name>
Where:
--cluster-name
: Cluster name you can get with a list of clusters in the folder.--host
: Host parameters:zone-id
: Availability zone.subnet-name
: Subnet name.
-
Make sure the provider version you are using is 0.90 or higher:
terraform version
Result example:
Terraform v1.4.6 on darwin_arm64 + provider registry.terraform.io/yandex-cloud/yandex v0.91.0
If the version is older
Update the provider version:
-
In the folder with the
.tf
configuration file, add the credentials to environment variables. -
In the same folder, initialize the provider with the updated version specified in the configuration file:
terraform init -upgrade
-
Check the provider version:
terraform version
-
-
Open the current Terraform configuration file with an infrastructure plan.
For information on how to create this file, see Creating a cluster.
-
Add to the Managed Service for MongoDB cluster description the required number of
host
blocks withMONGOD
in thetype
parameter and shard name in theshard_name
parameter:resource "yandex_mdb_mongodb_cluster" "<cluster_name>" { ... host { zone_id = "<availability_zone>" subnet_id = "<subnet_ID>" type = "mongod" shard_name = "<shard_name>" } }
-
Make sure the settings are correct.
-
Using the command line, navigate to the folder that contains the up-to-date Terraform configuration files with an infrastructure plan.
-
Run the command:
terraform validate
If there are errors in the configuration files, Terraform will point to them.
-
-
Confirm updating the resources.
-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
-
For more information, see the Terraform provider documentation
To create a shard, use the addShard REST API method for the Cluster resource or the ClusterService/AddShard gRPC API call and provide the following in the request:
- Cluster ID in the
clusterId
parameter. - Shard name in the
shardName
parameter. - Shard host configuration in the array of
hostSpecs
parameters.
You can get the shard name with a list of cluster shards and the cluster ID with a list of clusters in the folder.
Deleting a shard
You can delete a shard from a MongoDB cluster if it is not the only shard there. To replace the only shard in a cluster, first create a new shard and then remove the old one.
Note
The removeShard
- Go to the folder page
and select Managed Service for MongoDB. - Click the cluster name and open the Shards tab.
- Click the
icon in the shard row and select Delete. - In the window that opens, click Remove.
If you do not have the Yandex Cloud command line interface yet, install and initialize it.
The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name
or --folder-id
parameter.
To delete a shard from the cluster, run:
yc managed-mongodb shards delete <shard_name> \
--cluster-name=<cluster_name>
You can request the shard name with a list of cluster shards and the cluster name with a list of clusters in a folder.
-
Open the current Terraform configuration file with an infrastructure plan.
For information on how to create this file, see Creating a cluster.
-
Delete all shard-related
host
blocks from the Managed Service for MongoDB cluster description. -
Make sure the settings are correct.
-
Using the command line, navigate to the folder that contains the up-to-date Terraform configuration files with an infrastructure plan.
-
Run the command:
terraform validate
If there are errors in the configuration files, Terraform will point to them.
-
-
Type
yes
and press Enter.-
Run the command to view planned changes:
terraform plan
If the resource configuration descriptions are correct, the terminal will display a list of the resources to modify and their parameters. This is a test step. No resources are updated.
-
If you are happy with the planned changes, apply them:
-
Run the command:
terraform apply
-
Confirm the update of resources.
-
Wait for the operation to complete.
-
-
For more information, see the Terraform provider documentation
To delete a shard, use the deleteShard REST API method for the Cluster resource or the ClusterService/DeleteShard gRPC API call and provide the following in the request:
- Cluster ID in the
clusterId
parameter. To find out the cluster ID, get a list of clusters in the folder. - Name of the shard you are deleting in the
shardName
parameter.