Posts

MongoDB Setup

Install MongoDB -> Community Server

Prerequisites : Knowledge about mongodb

Navigate to bin folder after installation mongodb/bin

Single Node setup (Windows)

    Open cmd and navigate mongodb bin folder

    mkdir data/db -> any pathname, either we can create or use the existing empty folder

    mongod --dbpath data/db --port 27017

Command explanation

mongod : It the primary daemon process for the MongoDB system. It handles data requests, manages data access, and performs background management operations.

In simple words we can say, using mongd we can configure, initiate ,start and manage monogdb servers in the form of Single node server or  Replica sets of Config servers/Shard servers

--dbpath data/db : The path for dbserver must be mentioned so that it will put all the related files for the server like server configs, engine configs, database details, collection details, logs... 

Note: mongod will not create the dbpath folder automatically, we have to manually create it otherwise the servers will exit with unhandled exception

--port 27017 : The available port in the machine to run the mongodb server

Execute the command, the MongoDB server will start and we can start using it.

Connect using mongo in local or use tools like MongoShell

   Replica Set Setup(Windows)

mongod --dbpath data/rep1 --replSet rps --port 27017

mongod --dbpath data/rep2 --replSet rps --port 27018

mongod --dbpath data/rep3 --replSet rps --port 27019

The above commands starts the server as individual replica

--replSet rps : --replSet is key command to consider this server as one of the individual replica and rps is the replica name(we can give any name). The replicas in a single set should have the same replica name. 

Connect any one server

Execute this command : rs.initiate() (rs stands for replicaset)

This will initiate this server as one of the replica and then add other servers using the command : rs.add("localhost:27018") 

Execute this as many times to add the replica servers for the replica set with the respective host and port

Command : rs.status() : This will show the status of the primary and secondary nodes details


Sharded Cluster Setup(Windows)

mongod --configsvr --dbpath data/config1 --replSet cfs --port 26000

mongod --configsvr --dbpath data/config1 --replSet cfs --port 26001

Start config servers replica set 

--configsvr : To start server as config servers

mongod --shardsvr --dbpath data/shard1 --replSet sds --port 27017

mongod --shardsvr --dbpath data/shard1 --replSet sds --port 27018

Start shard servers replica set - First shard

mongod --shardsvr --dbpath data/shard2 --replSet sds2 --port 27019

mongod --shardsvr --dbpath data/shard2 --replSet sds2 --port 27020

Start shard servers replica set - Second shard

.

.

.

Horizontal scaling of server (n shard)

The above commands starts the server as individual shards

--shardsvr : To start server as a shard 

mongos --configdb "cfs/localhost:26000,localhost:26001" --port 260060

Start monogs server

    mongos : Provides the interface between the client applications and the sharded cluster. It behaves identically to any other MongoDB instance.

--configdb "cfs/localhost:26000,localhost:26001" 

--configdb : To make connection between mongos and config servers for accessing meta info of shards

cfs/localhost:26000,.. : cfs is the config servers replica set name and the servers host and port.

Connect to mongos and execute below command to add shards

sh.addShard("sds/localhost:27017")    

sh.addShard("sds/localhost:27018")

sh.addShard("sds2/localhost:27019")    

sh.addShard("sds2/localhost:27020")    

Connect each server using tools or through drivers 

Command : sh.status() Show the status of the shard servers

For tools - Ref


Comments