Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

20MIA1061(YASH NAIR)_NOSQL DA - 1

Set up the MongoDB cluster on a single machine or multi-node machine


and show the shard configuration and replication set concept with a
different dataset. Also, do the performance analysis of the sharded
architecture and image set for your data set. Any of the students should
not use the same data set. Discuss the precise description of distributed
Mongo DB with sharded and replication set configuration for your chosen
dataset.

Dataset Used :

https://www.kaggle.com/datasets/bravehart101/sample-supermarket-dataset

Replication Set :

Step 1: Initiate MongoDB in three ports.

Primary : mongod --replSet rs0 --port 27017 --dbpath "C:\Users\yashn\db1"


Secondary: mongod --replSet rs0 --port 27027 --dbpath "C:\Users\yashn\db2"

Tertiary : mongod --replSet rs0 --port 27037 --dbpath "C:\Users\yashn\db3"

Step 2: Convert Standalone to Cluster.

use admin
switched to db admin
db.adminCommand({shutdown:1,comment:"Convert to cluster"})
db.adminCommand({replSetGetStatus:1})
MongoDB Compass :
Step 3: Initiate the Replication.

rs.initiate()

Step 4: Adding Secondary nodes to the replication.

rs.add("localhost:27027")
rs.add("localhost:27037")

Step 5: Checking the status of the replication.

rs.status()
Step 6: Create a database and insert a record in the collection.

show dbs
use truly
db.users.insert({"name":"Hello"})
db.users.find()

Step 7: Import the CSV file via Compass. Since it’s a replication set, all three
sets will have the same data stored i.e. 10.0k documents in 27017, 27027, and
27037 ports.
MongoDB Sharding

Step 1: Initialise Mongodb config server, cfg0 in port 26050.

mongod —configsvr —dbpath cfg0 -–port 26050 –replSet cfg

Step 2: Initiate the cfg replica set in port 26050.

mongosh -–port 26050


rs.initiate()
Step 3: Initialize the shard servers, a and b.

mongod --shardsvr --replSet a --dbpath a0 --port 26000


mongod --shardsvr --replSet a --dbpath a1 --port 26001
mongod --shardsvr --replSet a --dbpath a2 --port 26002
mongod --shardsvr --replSet b --dbpath b0 --port 26100
mongod --shardsvr --replSet b --dbpath b1 --port 26101
mongod --shardsvr --replSet b --dbpath b2 --port 26102

Step 4: Add the shards to the respective server.

mongosh —port 26000


rs.initiate()
rs.add(“localhost:26001”)
rs.add(“localhost:26002”)
mongosh —port 26100
rs.initiate()
rs.add(“localhost:26101”)
rs.add(“localhost:26102”)
Step 5: Initiate Mongo shell at port 26061.

mongos —configdb “cfg/localhost:26050” —port 26061

Step 6: Start the mongos service at 26061 and add the shards, a and b.

mongosh —port 26061


sh.addShard(“a/localhost:26000”)
sh.addShard(“b/localhost:26100”)
sh.status()
Step 7: Shard the database mydb.

use mydb
sh.enableSharding(“mydb”)
Step 8: Shard the collection test1 using the shardkey “runtime” as hashed.

sh.shardCollection(“mydb.test1”,{“runtime”:”hashed”})

Step 9: Import the csv file in the collection test1 in mydb database
If we take a look at the Compass for all different ports,
● 26061 - Config server having all the 10.0k documents

You might also like