Horizontal Scaling with Sharding
Distribute data across multiple shards for horizontal scaling using MongoDB sharded clusters with config servers and mongos routers.
Prerequisites
- -MongoDB 6+ installed
- -Config server replica set running
- -At least 2 shard replica sets
Steps
Start the config server replica set
Launch config server instances that store cluster metadata and routing information.
In production, run 3 config server members for redundancy. A single member works for testing.
Initialize config server replica set
Connect to the config server and initiate the replica set.
Start a mongos router
Launch the query router that applications connect to for routing queries to the correct shards.
Add shards to the cluster
Register shard replica sets with the cluster through the mongos router.
Enable sharding on a database and shard a collection
Choose a shard key and enable sharding for a specific collection.
The shard key cannot be changed after sharding. Choose carefully based on your query patterns and data distribution.
Check sharding status
View the overall sharding topology, chunk distribution, and balancer status.
Full Script
FAQ
Discussion
Loading comments...