- 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 4.4. 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.
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.replSetName
option 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.
Set
net.bindIp
option to the hostname/ip or a comma-delimited list of hostnames/ips, andSet 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 a mongo
shell to one of the mongod
instances.¶
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
mongo
binary.
Initiate the replica set.¶
From the mongo
shell, run rs.initiate()
on
replica set member 0.
Important
Run rs.initiate()
on just one and only one
mongod
instance for the replica set.
Tip
When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. The use of logical DNS hostnames avoids configuration changes due to ip address changes.
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.
Restart the Replica Set as a Shard¶
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
--shardsvr
.
Determine the primary and secondary members.¶
Connect a mongo
shell 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, restart each secondary with the --shardsvr
option. 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.
Restart the primary with the --shardsvr
option.¶
Restart the primary with the --shardsvr
option.
To continue to use the same port, include the --port
option.
Include any other options as appropriate for your deployment.
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
mongos
usesmongodb6.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 a mongo
shell 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.
Tip
When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. The use of logical DNS hostnames avoids configuration changes due to ip address changes.
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
.
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
--shardsvr
.
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.
Initiate the replica set.¶
From the mongo
shell, 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.
Tip
When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. The use of logical DNS hostnames avoids configuration changes due to ip address changes.
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:
mongos
uses
"majority"
for the
enableSharding
command and its helper
sh.enableSharding()
.
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.
For more information, see Choosing 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
.