Deploy a Sharded Cluster in MongoDB 3.0

Share us

Hello Guys, Welcome back again..
I am writing this article to deploy a shard in MongoDB 3.0, I’m assuming you have basic knowledge of mongodb stand alone & replica set server.

Install mongoDB 3.0 in your Linux machine. ( I’m skipping the Installation process, you can also visit mongodb website for the same or click here.

Don’t start MongoDB instance now, we will configure mongo shard with 1 primary, 2 secondary, 2 config servers & 1 mongos router.

Create config file of mongod1.conf (Primary Instance), mongod2.conf, mongod3.conf (Secondary Instance), mongodconfigdb1.conf, mongodconfigdb2.conf ( configdb config file), mongods.conf ( mongos config file) with different port no and log/data path & pid file name.

##### Replica Setup
1. Setup replica in mongo 3.0 with below config- ( Change db/log path, port)

 $ vi /etc/mongod1.conf
  systemLog:
  destination: file
  logAppend: true
  path: /home/mongodb/mongod1/logs/mongod.log
  logRotate: reopen
  timeStampFormat: iso8601-local

# Where and how to store data.
storage:
  dbPath: /home/mongodb/mongod1/data
  journal:
    enabled: true
  engine: wiredTiger

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /home/mongodb/mongod1/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27017
#  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

replication:
  replSetName: srs01

Start the mongod instance with different port number & host name.

$ mongod -f /etc/mongod1.conf
$ mongod -f /etc/mongod2.conf
$ mongod -f /etc/mongod3.conf


$ mongo --port 27017

rs.initiate()
rs.add("hostname:27018");
rs.add("hostname:27019");

2. Setup ConfigDB in mongo 3.0 with below config- ( Change db/log path, port)

 vi /etc/mongodconfig1.conf
#### Config DB Setup
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/mongodb/configdb/logs/mongod.log
  logRotate: reopen
  timeStampFormat: iso8601-local

# Where and how to store data.
storage:
  dbPath: /home/mongodb/configdb/data
  journal:
    enabled: true
  engine: wiredTiger

processManagement:
  fork: true  # fork and run in background
  pidFilePath: /home/mongodb/configdb/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27020

sharding:
  clusterRole: configsvr

$ mongod -f /etc/mongodconfig1.conf
$ mongod -f /etc/mongodconfig2.conf

3. Setup MongoS in mongo 3.0 with below config- ( Change db/log path, port)

 $ vi /etc/mongods1.conf

##### MongoS Setup

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /home/mongodb/mongos/logs/mongod.log
  logRotate: reopen
  timeStampFormat: iso8601-local

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /home/mongodb/mongos/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27022
#  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

replication:
  localPingThresholdMs: 15

sharding:
  autoSplit: true
  configDB: config_db_hostname:27019
  chunkSize: 64

$ mongos -f /etc/mongods1.conf
$ mongos -f /etc/mongods1.conf

Now, we have to connect mongos with specified port and add the shard of replica set name.

#### Connect to mongos host with port

 $ mongo --host hostname --port 27020

mongos> sh.addShard("replSetName");
mongos> sh.addShard("srs01")
mongos> sh.getBalancerState()
false
mongos> sh.startBalancer()
mongos>
mongos> sh.getBalancerState()
true

If you follow above steps correctly, you will have sharded mongodb, you have to mention different db/log/pid path & port no, You can ask if you are facing any issue while setting up the sharding.

Thanks for going through the article, any suggestions or questions are welcome.

4 Commentsto Deploy a Sharded Cluster in MongoDB 3.0

  1. When someone writes an post he/she maintains the image of a user
    in his/her mind that how a user can know it. Thus that’s why this post is perfect.
    Thanks!

  2. Nice post. I was checking constantly this blog and I’m impressed! Extremely useful information specifically the last part 🙂 I care for such information much. I was seeking this certain information for a very long time. Thank you and good luck.|

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.