Creating a shard cluster with MongoDB commands
You create a shard cluster by adding shard servers with the The MongoDB sh.addShard shell command or the db.runCommand command with the addShard syntax.
The shard servers must be prepared for sharding. See Preparing shard servers.
To create a shard cluster from the MongoDB shell:
A shard cluster is created with the specified shard servers. Each shard server is set up
with Enterprise Replication and assigned an Enterprise Replication group name in its
sqlhosts file. The default Enterprise Replication group name for a database server is the database
server name with a suffix of
g_. For example, the default Enterprise Replication group name for a database server that is named
myserver is g_myserver.Examples
- Add a server to a shard cluster with addShard
- The following command adds the database server that is at port 9202 of
myhost2.ibm.com to a shard
cluster:
> sh.addShard("myhost2.ibm.com:9202")
- Add a server to a shard cluster with db.runCommand and addShard
- The following command adds the database server that is at port 9204 of
myhost4.ibm.com to a shard cluster.
> db.runCommand({"addShard":"myhost4.ibm.com:9204"}) - Add multiple servers to a shard cluster
- This example adds the database servers that are at port 9205 of myhost5.ibm.com,
port 9206 of myhost6.ibm.com, and port 9207 of myhost7.ibm.com to a
shard cluster.
> db.runCommand({"addShard":["myhost5.ibm.com:9205", "myhost6.ibm.com:9206","myhost7.ibm.com:9207"]})