Introducing Dragonfly Cloud! Learn More

Question: How to enable sharding in MongoDB?

Answer

Sharding in MongoDB is a method for distributing data across multiple servers. It is used to scale horizontally by adding more servers to manage growing data and workload. Here's a comprehensive guide on how to enable sharding in MongoDB:

Step 1: Start MongoDB Instances

First, you need to start multiple mongod instances. For a basic sharding setup, you'll need at least one config server, two shard servers, and one query router (mongos).

Config Server:

mongod --configsvr --dbpath /data/configdb --port 27019 --replSet configReplSet

Replace /data/configdb with the directory where you want to store the configuration data. The --replSet option specifies the replica set name for the config servers.

Shard Servers:

mongod --shardsvr --dbpath /data/shard1db --port 27018 mongod --shardsvr --dbpath /data/shard2db --port 27020

Replace /data/shard1db and /data/shard2db with the directories for your shard data.

Note:

In production environments, each shard and config server should be a replica set for high availability.

Step 2: Initialize the Replica Set for Config Servers

Connect to one of the config servers and initiate the replica set.

mongo --port 27019
rs.initiate({ _id: 'configReplSet', configsvr: true, members: [{_id: 0, host: 'localhost:27019'}] })

Add other config servers to the replica set if available.

Step 3: Start the Query Router (mongos)

The mongos process routes queries from your application to the appropriate shard(s).

mongos --configdb configReplSet/localhost:27019 --port 27017

Replace localhost:27019 with your config server addresses.

Step 4: Add Shards to the Cluster

Connect to mongos and add your shards.

mongo --port 27017
sh.addShard('localhost:27018') sh.addShard('localhost:27020')

Replace localhost:27018 and localhost:27020 with your shard server addresses.

Step 5: Enable Sharding for a Database

Choose a database to shard.

sh.enableSharding('myDatabase')

Step 6: Shard a Collection

Decide on a shard key and shard the collection.

sh.shardCollection('myDatabase.myCollection', {'myKey': 1})

This command tells MongoDB how to distribute the collection's documents across the shards.

Conclusion

Sharding in MongoDB allows you to distribute your data across multiple machines to handle larger datasets than would fit on a single machine and to distribute the load. Careful planning of the shard key and architecture is crucial for optimal performance and scalability.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.