Saturday, 22 December 2012

MongoDB Replica Set


How to create Replicate Set in MongoDB using three Ubuntu Virtual Machine
For creating Replica Set we need 3 MongoDB running instance on different or same system.In this example we have taken 3 different ubuntu virtual machine.
Now before we actually see how to make a replica set we need to check the connectivity between all the machine.
So in our case we have three ubuntu machine running on virtual box
1.      All three machine must be able to access the other two machine by SSH. If they cannot they we need to install SSH. We can do that by:

sudo apt-get install openssh-server

2    Next we need to check that all three machine runs on different “Adapter” in Vitual Box .We can check this by opening Virtual Box Window and going to “setting” option for each machine and then to “Network” option and choosing different Adapter for each machine.Also select “Bridged Adapter” in “attached to” option in “Adapter” option for all machine.
3    Now all the three machine should ping each other.
4.   All the three must have different IPs.

Now we are ready to create our Replica Set with three member.
First of all execute under line commands on each of the mongodb instance machine.
sudo nano /etc/mongodb.conf
on executing this a file opens do the following entries in the file

#enable rest
rest =true

adding these lines would turn on REST.Now we move at the bottom of the file and uncomment the replset option and define our replica set name.
replSet = set
Now we save the file.
Now we restart all  the three mongodb instance on different machines.
sudo service mongodb restart
Now on the mongodb instance which we want to make primary open mongodb shell and execute following command
mongo
>rs.initiate()
{
    "info2" : "no configuration explicitly specified -- making one",
    "me" : "dbserver1:27017",
    "info" : "Config now saved locally.  Should come online in about a minute.",
    "ok" : 1
}

Now for adding secondary to the replicate set execute
>rs.add(“ip_of_secondary:port”)
Eg.
 >rs.add(“172.26.32.150:27017”)
    { “ok” : 1 }
Do this for both the secondary mongodb instance machine.
You are done with creation of Replica Set with one primary and two secondary.you can check the status by running command
set:primary>rs.status()
Last and most important thing do not forget to make the entry of each machine ip and hostname in other machines “host file” .Host file is at  /etc/host.

MongoDB Installion on Ubuntu 12.04


How to Install MongoDB on Ubuntu 12.04
Go to the terminal in Ubuntu and type
      sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
      sudo echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | sudo tee -a /etc/apt/sources.list.d/10gen.list
      sudo apt-get -y update
      sudo apt-get -y install mongodb-10gen

Here's an explanation of each :
  • The `apt-key` call registers the public key of the custom 10gen MongoDB repository.
  • A custom 10gen repository list file is created in /etc/source.list.d/ folder which contains the location of the MongoDB binaries.
  • Repository is updated so that new packages can be registered locally.
  • Aptitude is told to install MongoDB.
After all these command your MongoDB starting running. You can type
mongo
on the terminal to start mongo shell for executing your mongodb commands.

Saturday, 8 December 2012

What is MongoDB ?


NOSQL

NoSQL (commonly interpreted as "not only SQL") is a broad class of database management systems, that are not primarily built on tables, and generally do not use SQL for data manipulation.
NoSQL database systems are often highly optimized for retrieve and append operations and often offer little functionality beyond record storage (e.g. key–value stores).
These next generation database system address some of the points :being non-relational, distributed, open-source, horizontally scalable, consistency and availability. The logic of validation , access control, conflict resolution, maintaining integrity constraints etc are removed from the database layer.Thus the database engine can focus on exceptional performance and scalability.


MongoDB


MongoDB is a open source document-oriented database system developed and supported by 10gen. It is a type of NoSQL database. In MongoDB data is not stored in tables as in relational database in this data is saved in collections in the form of BSON.It is has a dynamic scheme.

10gen began Development of MongoDB in October 2007. The database is used by MTV Networks, Craigslist, Foursquare and UIDAI Aadhaar. MongoDB is the one of the most popular NoSQL database management system.
Collection: A namespace within database ,that contains documents. Collection do not enforce a scheme.
Document: It is the basic unit of data in MongoDB. A document is a record in MongoDB collection. They are analogous to JSON objects but exist in the database in a more type-rich format known as BSON.
BSON: Short for binary JSON, is a binary-encoded serialization of JSON. BSON supports embedding of documents and arrays.BSON is designed to be efficient in space.

Why MongoDB ?

  • Document-oriented
    • Documents (objects) map nicely to programming language data types
    • No joins and no multi-document transactions for high performance and easy scalability
  • High performance
    • No joins and embedding makes reads and writes fast.
    • Indexes including indexing of keys from embedded documents and arrays.
  • High availability
    • Replicated servers with automatic master failover
  • Easy scalability
    • Automatic sharding (auto-partitioning of data across servers) 
    • Reads and writes are distributed over shards.
    • Eventually-consistent reads can be distributed over replicated servers
  • Rich query language