Database Reference
In-Depth Information
Adding shards to mongos
After connecting to the
mongos
instance, you can add shards to sharding. Basically, you
can add two types of endpoints to the
mongos
as a shard; a replica set or a standalone
mongod
instance.
MongoDB has a
sh
namespace and a function called
addShard(),
which is used to add
a new shard to an existing sharding network. Here is the example of a command to add a
new shard. This is shown in the following screenshot:
To add a replica set to
mongos
you should follow this scheme:
setname/server:port
For instance, if you have a replica set with the name of
rs1
, hostname
mon-
god1.replicaset.com
, and port number
27017
, the command will be as follows:
sh.addShard("rs1/mongod1.replicaset.com:27017")
Using the same function, we can add standalone
mongod
instances. So, if we have a
mon-
god
instance with the hostname
mongod1.sharding.com
listening on port
27017
,
the command will be as follows:
sh.addShard("mongod1.sharding.com:27017")
Note
You can use a secondary or primary hostname to add the replica set as a shard to the shard-
ing network. MongoDB will detect the primary and use the
primary
node to interact with
sharding.
Now, we add the replica set network that we made in the previous chapters to sharding us-
ing the following command:
sh.addShard("rs1/mongod1.replicaset.com:27017")