- Sharding >
- Deploy a Sharded Cluster
Deploy a Sharded Cluster¶
On this page
The topics on this page present an ordered sequence of the tasks required to set up a sharded cluster. Before deploying a sharded cluster for the first time, consider the Sharded Cluster Overview and Sharded Cluster Architectures documents.
To set up a sharded cluster, complete the following sequence of tasks in the order defined below:
- Start the Config Server Database Instances
- Start the mongos Instances
- Add Shards to the Cluster
- Enable Sharding for a Database
- Enable Sharding for a Collection
Warning
Sharding and “localhost” Addresses
If you use either “localhost” or 127.0.0.1 as the hostname
portion of any host identifier, for example as the host argument
to addShard or the value to the
--configdb
run time option, then you must use “localhost” or
127.0.0.1 for all host settings for any MongoDB instances in
the cluster. If you mix localhost addresses and remote host address,
MongoDB will error.
Start the Config Server Database Instances¶
The config server processes are mongod instances that store
the cluster’s metadata. You designate a mongod as a config
server using the --configsvr option. Each
config server stores a complete copy of the cluster’s metadata.
In production deployments, you must deploy exactly three config server instances, each running on different servers to assure good uptime and data safety. In test environments, you can run all three instances on a single server.
Config server instances receive relatively little traffic and demand only a small portion of system resources. Therefore, you can run an instance on a system that runs other cluster components.
Create data directories for each of the three config server instances. By default, a config server stores its data files in the /data/configdb directory. You can choose a different location. To create a data directory, issue a command similar to the following:
Start the three config server instances. Start each by issuing a command using the following syntax:
The default port for config servers is
27019. You can specify a different port. The following example starts a config server using the default port and default data directory:For additional command options, see mongod or Configuration File Options.
Note
All config servers must be running and available when you first initiate a sharded cluster.
Start the mongos Instances¶
The mongos instances are lightweight and do not require data
directories. You can run a mongos instance on a system that
runs other cluster components, such as on an application server or a
server running a mongod process. By default, a
mongos instance runs on port 27017.
When you start the mongos instance, specify the hostnames of
the three config servers, either in the configuration file or as command
line parameters. For operational flexibility, use DNS names for the
config servers rather than explicit IP addresses. If you’re not using
resolvable hostname, you cannot change the config server names or IP
addresses without a restarting every mongos and
mongod instance.
To start a mongos instance, issue a command using the following syntax:
For example, to start a mongos that connects to config server
instance running on the following hosts and on the default ports:
cfg0.example.netcfg1.example.netcfg2.example.net
You would issue the following command:
Each mongos in a sharded cluster must use the same
configdb string, with identical host names listed in
identical order.
If you start a mongos instance with a string that does
not exactly match the string used by the other mongos
instances in the cluster, the mongos return a
config-database-string-error error and refuse to start.
Add Shards to the Cluster¶
A shard can be a standalone mongod or a
replica set. In a production environment, each shard
should be a replica set.
From a
mongoshell, connect to themongosinstance. Issue a command using the following syntax:For example, if a
mongosis accessible atmongos0.example.neton port27017, issue the following command:Add each shard to the cluster using the
sh.addShard()method, as shown in the examples below. Issuesh.addShard()separately for each shard. If the shard is a replica set, specify the name of the replica set and specify a member of the set. In production deployments, all shards should be replica sets.Optional
You can instead use the
addSharddatabase command, which lets you specify a name and maximum size for the shard. If you do not specify these, MongoDB automatically assigns a name and maximum size. To use the database command, seeaddShard.The following are examples of adding a shard with
sh.addShard():To add a shard for a replica set named
rs1with a member running on port27017onmongodb0.example.net, issue the following command:Changed in version 2.0.3.
For MongoDB versions prior to 2.0.3, you must specify all members of the replica set. For example:
To add a shard for a standalone
mongodon port27017ofmongodb0.example.net, issue the following command:
Note
It might take some time for chunks to migrate to the new shard.
Enable Sharding for a Database¶
Before you can shard a collection, you must enable sharding for the collection’s database. Enabling sharding for a database does not redistribute data but make it possible to shard the collections in that database.
Once you enable sharding for a database, MongoDB assigns a primary shard for that database where MongoDB stores all data before sharding begins.
From a
mongoshell, connect to themongosinstance. Issue a command using the following syntax:Issue the
sh.enableSharding()method, specifying the name of the database for which to enable sharding. Use the following syntax:
Optionally, you can enable sharding for a database using the
enableSharding command, which uses the following syntax:
Enable Sharding for a Collection¶
You enable sharding on a per-collection basis.
Determine what you will use for the shard key. Your selection of the shard key affects the efficiency of sharding. See the selection considerations listed in the Shard Key Selection.
Enable sharding for a collection by issuing the
sh.shardCollection()method in themongoshell. The method uses the following syntax:Replace the
<database>.<collection>string with the full namespace of your database, which consists of the name of your database, a dot (e.g..), and the full name of the collection. Theshard-key-patternrepresents your shard key, which you specify in the same form as you would anindexkey pattern.
Example
The following sequence of commands shards four collections:
In order, these operations shard:
The
peoplecollection in therecordsdatabase using the shard key{ "zipcode": 1, "name": 1 }.This shard key distributes documents by the value of the
zipcodefield. If a number of documents have the same value for this field, then that chunk will be splittable by the values of thenamefield.The
addressescollection in thepeopledatabase using the shard key{ "state": 1, "_id": 1 }.This shard key distributes documents by the value of the
statefield. If a number of documents have the same value for this field, then that chunk will be splittable by the values of the_idfield.The
chairscollection in theassetsdatabase using the shard key{ "type": 1, "_id": 1 }.This shard key distributes documents by the value of the
typefield. If a number of documents have the same value for this field, then that chunk will be splittable by the values of the_idfield.The
alertscollection in theeventsdatabase using the shard key{ "hashed_id": 1 }.This shard key distributes documents by the value of the
hashed_idfield. Presumably this is a computed value that holds the hash of some value in your documents and is able to evenly distribute documents throughout your cluster.