- Sharding >
- Sharded Cluster Tutorials >
- Sharded Cluster Deployment Tutorials >
- Convert a Replica Set to a Replicated Sharded Cluster
Convert a Replica Set to a Replicated 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. 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 databases and a
mongos
. See Deploy Config Databases 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.
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 servers.
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 .
The tutorial uses the default ports (e.g. 27017
and 27019
). To
use different ports, see Configuration File Options.
Considerations¶
In production deployments, use exactly three config servers. Each config server must be on a separate machine.
In development and testing environments, you can deploy a cluster with a single config server.
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
, specifying the replica set
name through the replSet
option. Include any other parameters
specific to your deployment. For replication-specific parameters, see
Replication Options.
Repeat this step for the other two members of the rs0
replica set.
Initiate the replica set.¶
From the mongo
shell, run rs.initiate()
to
initiate a replica set that consists of the current member.
Add the remaining members to the replica set.¶
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.
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 Databases and mongos
¶
This procedure deploys the three config servers and the
mongos
. The config servers use the following hosts:
mongodb7.example.net
, mongodb8.example.net
, and
mongodb9.example.net
; the mongos
uses
mongodb6.example.net
.
Start three config databases.¶
On each mongodb7.example.net
, mongodb8.example.net
, and
mongodb9.example.net
server, start the config server using
default data directory /data/configdb
and the default port
27019
:
To modify the default settings or to include additional options specific to your deployment, see Configuration File Options.
Start a mongos
instance.¶
On mongodb6.example.net
, start the mongos
specifying
the config servers. The mongos
runs on the default port
27017
.
This tutorial specifies a small --chunkSize
of 1 MB to test sharding with the test_collection
created earlier.
Note
In production environments, do not use a small chunkSize size.
Add Initial Replica Set as a Shard¶
The following procedure adds the initial replica set rs0
as a shard.
Connect a mongo
shell 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
.
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. Include any other parameters
specific to your deployment. For replication-specific parameters, see
Replication Options.
Repeat this step for the other two members of the rs1
replica set.
Initiate the replica set.¶
From the mongo
shell, run rs.initiate()
to
initiate a replica set that consists of the current member.
Connect a mongo
shell to the mongos
.¶
Add the shard.¶
In a mongo
shell connected to the mongos
, add
the shard to the cluster with the sh.addShard()
method:
Shard a Collection¶
Connect a mongo
shell to the mongos
.¶
Enable sharding for a database.¶
Before you can shard a collection, you must first enable sharding for the collection’s database. Enabling sharding for a database does not redistribute data but makes it possible to shard the collections in that database.
The following operation enables
sharding on the test
database:
The operation returns the status of the operation:
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.
Once you shard a collection with the specified shard key, you cannot change the shard key. For more information on shard keys, see Shard Keys and Considerations for Selecting Shard Keys.
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.
The method returns the status of the operation:
The balancer will redistribute
chunks of documents when it next runs. As clients insert additional
documents into this collection, the mongos
will route the
documents between the shards.
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
.