Convert a Replica Set to a Sharded Cluster
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 3.6. 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.
Each server must have a resolvable domain, hostname, or IP address within your system.
The tutorial uses the default data directories (e.g.
/data/configdb). Create the appropriate directories with appropriate permissions. To use different paths, see Configuration File Options .
This procedure creates the initial three-member replica set
rs0. The replica set members are on the following hosts:
For each member, start a
mongod instance with the following settings:
replication.replSetNameoption to the replica set name,
If your application connects to more than one replica set, each set should have a distinct name. Some drivers group replica set connections by replica set name.
net.bindIpoption to the ip or a comma-delimited list of ips, and
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|
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.
<ip address>, specify the ip address or hostname for your
mongod instance that remote clients (including the other members of the replica set) can use to connect to the instance.
In production deployments, you can configure a init script to manage this process. Init scripts are beyond the scope of this document.
mongo shell to one of the
From the same machine where one of the
mongod is running (in this tutorial,
mongodb0.example.net), start the
mongo shell. 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
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
Issue the following operations on the primary of the replica set:
For more information on deploying a replica set, see Deploy a Replica Set.
Changed in version 3.4: For MongoDB 3.4 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