Skip to main content

ReplicaSet In Mongodb

Replica Set


    A replica set is a group of mongod instances that host the same data set. In a replica, one node is primary node that receives all write operations. All other instances, such as secondaries, apply operations from the primary so that they have the same data set.


Creating the mongod instances:

First we need to create a mongod isntance using below command. before creating the instance make a directory with any name ex: db1 and give this path in the below command Use any port you want 

  • mongod --port 20017 --dbpath C:\Users\michel\db1\ --replSet rs0


Create a another mongod isntance using the different terminal by using the same command but give different path and port number is diffferent

  • mongod --port 20018 --dbpath C:\Users\michel\db2\ --replSet rs0


Creating the mongo shell terminals:

Now use the new terminal to open mongo terminal using below command

  • mongo --port 20017


then initiate the replica set using below command

  • rs.initiate()


you will see info like 

info2" : "no configuration specified. Using a default configuration for the set", after seeing the  rs0:SECONDARY> then press enter then you will notice SECONDARY will replaces with PRIMARY. You can check weather the replica set node is created or not using rs.status().

Adding the node2 to primary node:

In the open terminal of mogo --port 20017 enter the below command to add the port 20018.
  • rs.add("localhost:20018");
check for the status weather the 2 nodes are appearing or not

then create collection and insert some dictionary
  • use testdb
  • db.testdb.posts.insert({"postname":"mongo replica set"})
check if it is inserted or not using 

  • db.testdb.posts.find({})

Checking weather same collection created in replica set or not 20018 port:

Open a new terminal and enter mongo --port 20018 command
then enter rs.slaveOk()
  • mongo --port 200018
  • rs.slaveOk()
Check the same collection exist or not 

  • db.testdb.posts.find({})
even if the main mongod instance i.e 20017 is stopped also you can read the data from mongo --port 20018

Comments

Popular posts from this blog

MQTT set up Flask

 MQTT Introduction: It is message broker which holds the data when ever a consumer will connected to the topic then he will receive the data . mqtt is a machine to machine message broker, mainly used in IOT application, the default port is 1883   Installation: sudo apt-get update sudo apt-get install mosquitto Command line execution: Producer mosquitto_pub -h 127.0.0.1 -t 'topic_name' -m '{'name':'Sony'}' Subscriber mosquitto_sub -h 127.0.0.1 -t 'topic_name' -h : it is the host address -t : topic name -m : message payload QOS in MQTT: QOS(quality of services) there are mainly 3 services are available in mqtt  qos=0 qos=1 qos=2 QOS (0,0): At most once service, publisher will send a message to broker at most once, the broker passes a message to subscriber one time     

Making a Geographic Heatmap with Python

      We have a requirement like need to create a map where user is traveled.There are many libraries available in python to draw a map if you have list of co-ordinates with you like(longitude and latitude)  To name few packages : vincent, bokeh, plot.ly and matplotlib and folium   Things to consider while choosing the library: 1. Open source 2. Painless to use 3. built in support for heat-maps 4. actively maintained Folium is the lib which will satisfy above all use cases Requirements: pip install folium pip install geopands (for adding other geo json files) pip install pands (to read csv of excel files which are having the co-ordinates data) Creating the map: import pandas as pd import folium from folium.plugins import HeatMap map_df = pd.read_csv('map.csv', sep='\t') max_amount = float(map_df['Amount'].max()) hmap = folium.Map(location=[42.5, -75.5], zoom_start=7, ) hm_wide = HeatMap( list(zip(map_df.lat.values,         ...

CommandLine Args In Python

 Command line args in Python :      When the program comes to Python , it's all about libraries .Argparser is the library used for the taking inputs from the command line. Here you can validate the arguments which are entered by the user without writing the another function to validate from the if conditions. suppose give a filename like cmdprocess.py You can give the expecting arguments like below cmdprocess.py --help      it will give the total arguments with respective commands to represent Required =True :           When the required is true. You have to enter the argument.   Type = function name:          Type is very useful to validate the input entered by the user. so that you can send a message like user need to enter the defined arguments only. This function always expects the input argument . Custom-Exceptions In Python:    There are multiple exception...