Installing Multi-Node cluster in MongoDB

Hi MongoDBlearners,

This Article will give a clear idea about the installation of Distributed Multi-Node MongoDB cluster. Follow the steps clear and generate a MongoDB cluster by you own.

Mongodb_Multinode_cluster

Download the MongoDB. If you want to download the latest version of MongoDB then check its official web Site

1. Create three db and log path in each node for the respective mongo instances and edit your host file with the ip address of the three nodes as IP1, IP2, IP3 respectively.
sudo mkdir /data
sudo mkdir /data/db1
sudo mkdir /data/db1/log
sudo mkdir /data/db2
sudo mkdir /data/db2/log
sudo mkdir /data/db3
sudo mkdir /data/db3/log
sudo chmod -R 777 /data/*

2. Create three copies of mongodb.conf files in each machine with their respective db and log path along with their host addresses.
# mongodb1.conf
# Where to store the data.
dbpath=/data/db1
#where to log
logpath=/data/db1/log/mongodb.log
logappend=true
bind_ip = 192.168.10.15
#port = 27017
Similarly with the other two mongodb.conf as mongodb2.conf and mongodb3.conf

3. Start your mongod config Server after entering into your mongodb directory in your Node 1
$ nohup bin/mongod –configsvr –port 27017 –config mongodb1.conf &

4. Start your mongos query router by binding it with the config server in your node 2
$ nohup bin/mongos –configdb IP1:27017 –port 27017 –config mongodb1.conf &

5. Start your mongod shard server with their replica set respectively. Here Shard server 1 is started with replica set dowon1 and Shard server 2 with dowon2.
In Node 1:
$ nohup bin/mongod –replSet dowon01 –shardsvr –port 27018 –dbpath /data/db2 –config mongodb2.conf &

In Node 2:
$ nohup bin/mongod –replSet dowon02 –shardsvr –port 27018 –dbpath /data/db2 –config mongodb2.conf &
Let’s keep the above two mongod as primary replica of their replica set respectively.

6. Now to have the secondary copy of the replica set, start another mongod with the same replica set in different nodes other than your primary replica.
In Node 1:
$ nohup bin/mongod –replSet down02 –port 27019 –dbpath /data/db3 –config mongodb3.conf &

In Node 2:
$ nohup bin/mongod –replSet down01 –port 27019 –dbpath /data/db3 –config mongodb3.conf &

7. Enter into your mongo query router’s shell and then add the shards. To add the shard you should be in admin database.
In Node 2:
$ bin/mongo IP2:27017/admin
mongos> db.runCommand({addshard:’IP1:27018′})
{“shardAdded”: “shard0000”, “ok”: 1}
mongos> db.runCommand({addshard:’IP2:27018′})
{“shardAdded”: “shard0001”, “ok”: 1}

Check the shard status:
mongos> sh.status()

8. Enter into your mongod shard servers to enable primary replications
In Node 1:
$ bin/mongo IP1:27018
mongo> var config={_id:’dowon01′, members:[
{_id:0, host:'IP1:27018'},
{_id:1, host:'IP2:27019'}]
}
mongo> rs.initiate(config)
Press Enter Key (OR) Exit and Re-login mongo shell (like bin/mongo IP1:27018) to check for the primary enabled.
dowon01:PRIMARY> show dbs;

In Node 2:
$ bin/mongo IP2:27018
mongo> var config={_id:’dowon02′, members:[
{_id:0, host:'IP2:27018'},
{_id:1, host:'IP1:27019'}]
}
mongo> rs.initiate(config)
Press Enter Key (OR) Exit and Re-login mongo shell (like bin/mongo IP2:27018) to check for the primary enabled.
dowon02:PRIMARY> show dbs;

9. Enter into your mongo shell to check the secondary replications and enable them as slave.
In Node 1:
$ bin/mongo IP1:27019
mongos> dowon02:SECONDARY>rs.slaveOk()
mongos> dowon02:SECONDARY>show dbs

In Node 2:
bin/mongo IP2:27019
dowon01:SECONDARY>rs.slaveOk()
dowon01:SECONDARY>show dbs

10. Create a testShard database and some sample documents into the testShard
In Node 2:
$ bin/mongo IP2:27017
mongos> use testShard
mongos> db.patient.save({_id:1, age:11, name:’Name1′, sex:male});
mongos> db.patient.save({_id:2, age:22, name:’Name2′, sex:female});
mongos> db.patient.save({_id:3, age:33, name:’Name3′, sex:male});
mongos> db.patient.find();
{ “_id” : 1, “age” : 11, “name” : “Name1″, “sex” : male }
{ “_id” : 2, “age” : 22, “name” : “Name2″, “sex” : female }
{ “_id” : 3, “age” : 33, “name” : “Name3″, “sex” : male }

11. Go and check the primary and the secondary for the testShard created in them and enable sharding in the testShard database
Range Based Sharding:
mongos> use admin
mongos> db.runCommand({enablesharding:’testShard’});
mongos> db.runCommand({shardcollection:’testShard.patient’, key:{_id:1}});

Hash Based Sharding:
mongos> use testShard
mongos> db.patient.ensureIndex({ _id : “hashed” })
mongos> use admin
mongos> db.runCommand({enablesharding:’testShard’});
mongos> db.runCommand({shardcollection:’testShard.patient’, key:{_id:’hashed’}});

12. Go with the bulk insert and check the shards getting created and their respective replications presence.
mongos> for(var i=4 ; i<=100000 ; i++) { db.patient.save({_id:i, age:44, name:’Name’+i, sex:male}); } Once you verify the sharding and replication now we can add the node 3 to the cluster

13. Start the mongod for the another secondary copies of the replica sets In Node 3:
$nohup bin/mongod –replSet dowon01 –port 27019 –dbpath /data/db1 –config monogdb1.conf
$nohup bin/mongod –replSet dowon02 –port 27020 –dbpath /data/db2 –config monogdb2.conf

14. Go the primary replica of the shard server 1 and 2 and add the runtime third replica to their replication set.
In Node 1: dowon01:PRIMARY>rs.add(“IP3:27019″)
In Node 2: dowon02:PRIMARY>rs.add(“IP3:27019″)

15. Enter into the mongo shell in the node 3 to check for the another secondary replica
Replica Set 1
$ bin/mongo IP3:27019
dowon01:SECONDARY> rs.slaveOk()
dowon01:SECONDARY> show dbs;

Similarly for the next replica set replica set 2
$ bin/mongo IP3:27020
dowon02:SECONDARY> rs.slaveOk()
dowon02:SECONDARY> show dbs;

———————————-

Article written by DataDotz Team

DataDotz is a Chennai based BigData Team primarily focussed on consulting and training on technologies such as Apache Hadoop, Apache Spark , NoSQL(HBase, Cassandra, MongoDB), Search and Cloud Computing.

Note: DataDotz also provides classroom based Apache Kafka training in Chennai. The Course includes Cassandra , MongoDB, Scala and Apache Spark Training. For more details related to Apache Spark training in Chennai, please visit http://datadotz.com/training/