Managing shards MongoDB
You can create a sharded cluster or enable sharding later. After that, you can add and configure shards.
Make sure that your shards consist of at least three MONGOD
hosts to ensure higher availability. We do not recommend sharding small collections: query processing is faster with a standard replica cluster.
Alert
After cluster sharding is enabled:
- You can't disable sharding: the cluster will always maintain a minimum number of
MONGOS
,MONGOCFG
, orMONGOINFRA
hosts depending on the sharding type. - For accessing the databases, only use 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 MongoDB.
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: Using a
MONGOINFRA
host. -
Advanced: Using
MONGOS
andMONGOCFG
hosts.For more information, see Sharding in Managed Service for MongoDB.
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 the 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_GB>,` `disk-type=<disk_type>
Where:
--cluster-name
: Cluster name that you can request with a list of clusters in the folder.--host
: Host parameters:type
: Host type (MONGOINFRA
).zone-id
: Availability zone.subnet-name
: Name of the subnet.
--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 the 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_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_GB>,` `disk-type=<disk_type>
Where:
--cluster-name
: Cluster name that you can request with a list of clusters in the folder.--host
: Host parameters:type
: The type (MONGOS
orMONGOCFG
).zone-id
: Availability zone.subnet-name
: Name of the subnet.
--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 olderUpdate 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 MongoDB cluster.
-
Add additional resources to the configuration file.
For standard sharding of the cluster usingMONGOINFRA
hostsresources_mongoinfra { resource_preset_id = "<host_class>" disk_type_id = "<disk_type>" disk_size = <storage_size_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 usingMONGOS
andMONGOCFG
hostsresources_mongos { resource_preset_id = "<host_class>" disk_type_id = "<disk_type>" disk_size = <storage_size_GB> } resources_mongocfg { resource_preset_id = "<host_class>" disk_type_id = "<disk_type>" disk_size = <storage_size_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 a 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.
Adding 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 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 Add 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 add a shard to a cluster, 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 that you can request with a list of clusters in the folder.--host
: Host parameters:zone-id
: Availability zone.subnet-name
: Name of the subnet.
-
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 olderUpdate 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 MongoDB cluster.
-
Add to the Managed Service for MongoDB cluster description the required number of
host
blocks of theMONGOD
type set in thetype
parameter and the shard name specified 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 add a shard to a cluster, 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. - Name of the shard in the
shardName
parameter. - In the array of
hostSpecs
parameters, the shard host configuration.
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 provided that it's not the only shard in it. 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 required shard row and select Remove. - 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 MongoDB cluster.
-
Delete all shard-related
host
sections 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