CentOS: Install and Configure MongoDB Sharded Cluster

In this post I am going to deploy a MongoDB sharded cluster. MongoDB is an open-source NoSQL, document-oriented database designed for ease of development and scaling.  I am going to use 3 servers, and all the /etc/hosts definition would be as below:

192.168.0.41        mongo1 mongo1.cluster.local
192.168.0.42        mongo2 mongo2.cluster.local
192.168.0.43        mongo3 mongo3.cluster.local

All servers running CentOS 6.3 64bit with firewall and SElinux turned off. All steps must be executed in all servers unless specified.

Install MongoDB

1. Install EPEL repo:

$ rpm -Uhv http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

2. Install MongoDB and all required components:

$ yum install mongodb* -y --enablerepo=epel

 

Config Servers

1. Create config database directory. By default, MongoDB will use /data/configdb:

$ mkdir -p /data/configdb

2. Default port for config server is 27019. Start config servers:

$ mongod --configsvr --fork --logpath /var/log/mongodb.log --logappend

You should see following output:

forked process: 5464
all output going to: /var/log/mongodb.log
child process started successfully, parent exiting

 

Routing Servers

1. By default, mongos will listen on port 27017. Start mongos as below:

$ mongos --configdb mongo1,mongo2,mongo3 --fork --logpath /var/log/mongodb.log --logappend

You should see following output:

forked process: 5534
all output going to: /var/log/mongodb.log
child process started successfully, parent exiting

Shard Servers

1. Create default data directory. By default, MongoDB will use /data/db:

$ mkdir -p /data/db

2. By default, mongod with –shardsvr option will listen on port 27018. Start mongod as below:

$ mongod --shardsvr --fork --logpath /var/log/mongodb.log --logappend

You should see following output:

forked process: 5675
all output going to: /var/log/mongodb.log
child process started successfully, parent exiting

 

MongoDB Sharding

1. Verify that MongoDB services are listening to correct ports:

$ netstat -tulpn | grep mongo
 
tcp     0    0      0.0.0.0:27017     0.0.0.0:*     LISTEN    5534/mongos
tcp     0    0      0.0.0.0:27018     0.0.0.0:*     LISTEN    5675/mongod
tcp     0    0      0.0.0.0:27019     0.0.0.0:*     LISTEN    5464/mongod
tcp     0    0      0.0.0.0:28017     0.0.0.0:*     LISTEN    5534/mongos
tcp     0    0      0.0.0.0:28018     0.0.0.0:*     LISTEN    5675/mongod
tcp     0    0      0.0.0.0:28019     0.0.0.0:*     LISTEN    5464/mongod

2. SSH into mongo1 and type mongo to access the mongos console:

$ mongo

3. Use admin database to list the sharding status:

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

You should get this reply:

{ "shards" : [ ], "ok" : 1 }

4. Add the sharded servers by specifying the hostname and MongoDB shard service port:

mongos> sh.addShard( "mongo1:27018");
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard( "mongo2:27018");
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.addShard( "mongo3:27018");
{ "shardAdded" : "shard0002", "ok" : 1 }

5. Download this JSON example file and import into database mydb:

$ wget http://media.mongodb.org/zips.json
$ mongoimport --db mydb --collection zip --file zips.json
connected to: 127.0.0.1
Mon Mar 25 06:22:35 imported 29470 objects

6. Enable sharding for mydb:

mongos> sh.enableSharding ("mydb");
{ "ok" : 1 }

7. Check sharding status:

mongos> sh.status()
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "mongo1:27018" }
{ "_id" : "shard0001", "host" : "mongo2:27018" }
{ "_id" : "shard0002", "host" : "mongo3:27018" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "mydb", "partitioned" : true, "primary" : "shard0000" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0001" }

You can see database mydb has been partitioned by MongoDB with value true.

6 thoughts on “CentOS: Install and Configure MongoDB Sharded Cluster

  1. when i execute this command :
    <>
    this error appear: <>

    please help me to find a solution
    thank you

    Reply

    1. I could not see the error. You might need to paste it some where or using the code tags when commenting.

      Reply

  2. Just curious… I wanted to make sure I didn’t miss something. In the beginning you said that all the “steps must be executed in all servers unless specified”. I did not notice where any specific servers were “specified” in the commands. Which would mean that all the commands would need to be executed on all the servers listed at the beginning?

    Reply

    1. Yes, most of the commands need to be executed on all nodes except under MongoDB Sharding section, where I emphasized something like ‘SSH into mongo1’. Thus, step 2 to 7 under that section need to be executed only on mongo1.

      Reply

      1. Thanks… it was a little too subtle for my walnut-sized brain to see. I was looking for something more obvious like “These steps are done on this server only…”. 😉

        Thanks again….

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *