- Sharding >
- Sharded Cluster Administration >
- Convert a Replica Set to a Sharded Cluster
Convert a Replica Set to a Sharded Cluster¶
On this page
Overview¶
This tutorial converts a single three-member replica set to a sharded cluster with two shards. Each shard is an independent three-member replica set. This tutorial is specific to MongoDB . For other versions of MongoDB, refer to the corresponding version of the MongoDB Manual.
The procedure is as follows:
- Create the initial three-member replica set and insert data into a collection. See Set Up Initial Replica Set.
- Start the config servers and a
mongos. See Deploy Config Server Replica Set and mongos. - Add the initial replica set as a shard. See Add Initial Replica Set as a Shard.
- Create a second shard and add to the cluster. See Add Second Shard.
- Shard the desired collection. See Shard a Collection.
Considerations¶
Individual steps in these procedures note when downtime will occur.
Important
These procedures cause some downtime for your deployment.
Prerequisites¶
This tutorial uses a total of ten servers: one server for the
mongos and three servers each for the first replica
set, the second replica set, and the config server replica set.
Each server must have a resolvable domain, hostname, or IP address within your system.
The tutorial uses the default data directories (e.g. /data/db and
/data/configdb). Create the appropriate directories with
appropriate permissions. To use different paths, see
Configuration File Options .
Procedures¶
Set Up Initial Replica Set¶
This procedure creates the initial three-member replica set rs0.
The replica set members are on the following hosts:
mongodb0.example.net, mongodb1.example.net, and
mongodb2.example.net.
Start each member of the replica set with the appropriate options.¶
For each member, start a mongod instance with the
following settings:
- Set
replication.replSetNameoption to the replica set name. If your application connects to more than one replica set, each set must have a distinct name. - Set
net.bindIpoption to the hostname/ip or a comma-delimited list of hostnames/ips. - Set any other settings as appropriate for your deployment.
In this tutorial, the three mongod instances are
associated with the following hosts:
| Replica Set Member | Hostname |
|---|---|
| Member 0 | mongodb0.example.net |
| Member 1 | mongodb1.example.net |
| Member 2 | mongodb2.example.net |
The following example specifies the replica set name and the ip
binding through the --replSet and --bind_ip
command-line options:
Warning
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.
For <hostname(s)|ip address(es)>, specify the hostname(s) and/or
ip address(es) for your mongod instance that remote
clients (including the other members of the replica set) can use to
connect to the instance.
Alternatively, you can also specify the replica set name and the ip addresses in a configuration file:
To start mongod with a configuration file, specify the
configuration file’s path with the --config option:
In production deployments, you can configure a init script to manage this process. Init scripts are beyond the scope of this document.
Connect mongosh to one of the mongod instances.¶
From the same machine where one of the mongod is running
(in this tutorial, mongodb0.example.net), start
mongosh. To connect to the mongod
listening to localhost on the default port of 27017, simply issue:
Depending on your path, you may need to specify the path to the
mongosh binary.
If your mongod is not running on the default port, specify the
--port option for mongosh.
Initiate the replica set.¶
From mongosh, run rs.initiate() on
replica set member 0.
Important
Run rs.initiate() on just one and only one
mongod instance for the replica set.
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.
MongoDB initiates a replica set, using the default replica set configuration.
Create and populate a new collection.¶
The following step adds one million documents to the collection
test_collection and can take several minutes depending on
your system.
To determine the primary, use rs.status().
Issue the following operations on the primary of the replica set:
For more information on deploying a replica set, see Deploy a Replica Set.
Deploy Config Server Replica Set and mongos¶
This procedure deploys the three-member replica set for the config
servers and the
mongos.
- The config servers use the following hosts:
mongodb7.example.net,mongodb8.example.net, andmongodb9.example.net. - The
mongosusesmongodb6.example.net.
Deploy the config servers as a three-member replica set.¶
Start a config server on mongodb7.example.net,
mongodb8.example.net, and mongodb9.example.net. Specify the
same replica set name. The config servers use the default data
directory /data/configdb and the default port 27019.
Warning
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.
To modify the default settings or to include additional options specific to your deployment, see mongod or Configuration File Options.
Connect mongosh to one of the config servers and
run rs.initiate() to initiate the replica set.
Important
Run rs.initiate() on just one and only one
mongod instance for the replica set.
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.
Start a mongos instance.¶
On mongodb6.example.net, start the mongos specifying
the config server replica set name followed by a slash / and at least
one of the config server hostnames and ports.
Restart the Replica Set as a Shard¶
For sharded clusters, mongod instances for
the shards must explicitly specify its role as a shardsvr,
either via the configuration file setting
sharding.clusterRole or via the command line option
--shardsvr.
Note
Default port for mongod instances with the shardsvr
role is 27018. To use a different port, specify
net.port setting or --port option.
Determine the primary and secondary members.¶
Connect mongosh to one of the members and run
rs.status() to determine the primary and secondary members.
Restart secondary members with the --shardsvr option.¶
One secondary at a time, shut down
and restart each secondary
with the --shardsvr option.
Warning
This step requires some downtime for applications connected to
secondary members of the replica set. Applications connected to a
secondary may error with CannotVerifyAndSignLogicalTime after
restarting the secondary until you perform the steps in
Add Initial Replica Set as a Shard. Restarting your application will
also stop it from receiving CannotVerifyAndSignLogicalTime
errors.
To continue to use the same port, include the --port
option. Include additional options, such as --bind_ip, as
appropriate for your deployment.
Warning
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.
Include any other options as appropriate for your deployment. Repeat this step for the other secondary.
Step down the primary.¶
Connect mongosh to the primary and stepdown the primary.
Warning
This step requires some downtime. Applications may error with
CannotVerifyAndSignLogicalTime after stepping down the primary
until you perform the steps in Add Initial Replica Set as a Shard.
Restarting your application will also stop it from receiving
CannotVerifyAndSignLogicalTime errors.
Restart the primary with the --shardsvr option.¶
Shut down the primary and restart with the --shardsvr option.
To continue to use the same port, include the --port option.
Include any other options as appropriate for your deployment.
Add Initial Replica Set as a Shard¶
The following procedure adds the initial replica set rs0 as a shard.
Connect mongosh to the mongos.¶
Add the shard.¶
Add a shard to the cluster with the sh.addShard method:
Add Second Shard¶
The following procedure deploys a new replica set rs1 for the
second shard and adds it to the cluster. The replica set members are on
the following hosts: mongodb3.example.net,
mongodb4.example.net, and mongodb5.example.net.
For sharded clusters, mongod instances for
the shards must explicitly specify its role as a shardsvr,
either via the configuration file setting
sharding.clusterRole or via the command line option
--shardsvr.
Note
Default port for mongod instances with the shardsvr
role is 27018. To use a different port, specify
net.port setting or --port option.
Start each member of the replica set with the appropriate options.¶
For each member, start a mongod, specifying the replica
set name through the --replSet option
and its role as a shard with the
--shardsvr option. Specify additional
options, such as --bind_ip, as
appropriate.
Warning
Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.
For replication-specific parameters, see Replication Options.
Repeat this step for the other two members of the rs1 replica set.
Connect mongosh to a replica set member.¶
Connect mongosh to one member of the replica set
(e.g. mongodb3.example.net)
Initiate the replica set.¶
From mongosh, run rs.initiate() to
initiate a replica set that consists of the current member.
Important
Run rs.initiate() on just one and only one
mongod instance for the replica set.
Important
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.
Connect mongosh to the mongos.¶
Add the shard.¶
In mongosh, when connected to the
mongos, add the shard to the cluster with the
sh.addShard() method:
Shard a Collection¶
Connect mongosh to the mongos.¶
Determine the shard key.¶
For the collection to shard, determine the shard key. The shard key determines how MongoDB distributes the documents between shards. Good shard keys:
- have values that are evenly distributed among all documents,
- group documents that are often accessed at the same time into contiguous chunks, and
- allow for effective distribution of activity among shards.
For more information, see Choose a Shard Key.
This procedure will use the number field as the shard key for
test_collection.
Create an index on the shard key.¶
Before sharding a non-empty collection, create an index on the shard key.
Shard the collection.¶
In the test database, shard the test_collection,
specifying number as the shard key.
mongos uses "majority" for the
write concern of the
shardCollection command and its helper
sh.shardCollection().
The method returns the status of the operation:
The balancer redistributes
chunks of documents when it next runs. As clients insert additional
documents into this collection, the mongos routes the
documents to the appropriate shard.
Confirm the shard is balancing.¶
To confirm balancing activity, run db.stats() or
db.printShardingStatus() in the test database.
Example output of the db.stats():
Example output of the db.printShardingStatus():
Run these commands for a second time to demonstrate that chunks are migrating from rs0 to rs1.