Home > Development | General > MongoDB Sharding

MongoDB Sharding

Today I had to create a MongoDB sharding setup on 4 VM servers.  One of the servers would be the primary shard and also contain the MongoS process and the MongoD configdb process. You can find out more about Mongo sharding here. There were some things that are not spelled out in the documentation and were not obvious to me so I figured I would list them here for reference.

The first step is to install mongo on all the servers you intend to use. For each server I keep a file that I use to start the server:

mongod --fork --logpath /var/log/mongodb.log --logappend -vvv

Assuming you have added the path to the mongo bin directory you can just execute that file to start the server. Do not create any databases yet.

The thing to remember is that you will be running three different Mongo processes on the primary server:

  1. Mongos handles routing client requests to the correct shard
  2. Mongod as a config server
  3. Mongod as the primary shard

For this to work properly all three processes need to have their own ports. Let the Mongos process keep the default 27017 port so that clients can just connect like normal. Here are the startup files for my primary server’s processes:

start-mongo-configsvr

mongod --configsvr --port 28017 --fork --logpath /var/log/mongodb.log --logappend -vvv

start-mongos

mongos --configdb 172.27.10.138 --port 27017 --fork --logpath /var/log/mongodb.log --logappend -vvv

start-mongod

mongod --port 27019 --fork --logpath /var/log/mongodb.log --logappend -vvv

Note the mongod process uses 27019 this seems to be the default. Start the processes in order.

Now you can connect to the mongos process by typing:

mongo

Now you want to create a database and a collection in that database. Then switch to the admin database and add the shards as shown:

> db.runCommand( { addshard : "<ip-address>:27019", maxSize:40000 } );
> db.runCommand( { addshard : "<ip-address>:27017", maxSize:40000 } );
> db.runCommand( { addshard : "<ip-address>:27017", maxSize:40000 } );
> db.runCommand( { addshard : "<ip-address>:27017", maxSize:40000 } );

Where the ip address is for each shard’s host. Remember that the port for the primary shard is not the default. You can test the shards exist with the following command:

> db.runCommand( { listshards : 1 } );

Now its time to enable sharding on the database

> db.runCommand({enablesharding : "database-name"});

And finally enable sharding on the collection

> db.runCommand({ shardcollection : "database-name.collection-name", key : {_id:1} });

And now its time to import some data and see what it does.
 

Comments are closed.

TOP