Installing MongoDB on Windows or Linux ( Ubuntu ) environment

Posted By : Akash Sharma | 29-Jan-2013

Brief Introduction of MongoDB

MongoDB (from "humongous" means enormous) is an open source document-oriented database system developed and supported by 10gen. Instead of storing data in tables as is done in a "classical" relational database, MongoDB stores structured data as JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.
10gen began Development of MongoDB in October 2007. The database is used by MTV Networks, CraigsList, FourSquare and UIDAI Aadhaar. MongoDB is the most popular NoSQL database management system

Question : What is document oriented database?
Answer:

It is a computer designed for storing, retrieving, and managing document-oriented information.The central concept of a document-oriented database is the notion of a Document.
While each document-oriented database implementation differs on the details of this definition, in general, they all assume documents encapsulate and encode data (or information) in some standard formats or encodings. Encodings in use include XML, JSON, and  BSON.
Unlike a relational database where each record would have the same set of fields and unused fields might be kept empty, there are no empty 'fields' in either document (record) in this case.


Question : What is NoSQL database?
Answer:

NoSQL (commonly interpreted as "not only SQL”) , NoSQL databases are not built primarily on tables, and generally do not use SQL for data manipulation.
It is highly optimized for retrieval and appending operations when working with a huge quantity of data.
The data can be structured, but NoSQL is used when what really matters is the ability to store and retrieve great quantities of data, not the relationships between the elements.Usage examples might be to store millions of key-value pair in one or a few associative arrays or to store millions of data records.

  • Does not use SQL as its query language.
  • Developed to manage large volumes of data that do not necessarily follow a fixed schema.
  • NoSQL has a distributed, fault-tolerant architecture. In this way, the system can easily scale out by adding more servers, and failure of a server can be tolerated. This type of database typically scales horizontally.

Question:What is a document and a collection in mongodb?

Answer:

An element of data is called a document, and documents are stored in collections. One collection may have any number of documents.Compared to relational databases, we could say collections are like tables, and documents are like records. But there is one big difference: every record in a table has the same fields (with, usually, differing values) in the same order, while each document in a collection can have completely different fields from the other documents. The only schema requirement mongo places on documents (aside from size limits) is that they must contain an '_id' field with a unique, non-array value.

Note: "_id" field is obligatory, automatically created by MongoDB; it's a unique index which identifies the document. The user may specify any non-array value for _id as long as the value is unique).

Question : What is JSON like document ?
Answer :

MongoDB stores structured data as JSON-like documents, using dynamic schemas (called BSON), rather than predefined schemas.
In a document, new fields can be added or existing ones suppressed, modified or renamed at any moment. There is no predefined schema. A document structure is very simple: it follows the JSON format, and consists of a series of key-value pairs.The key of the key-value pair is the name of the field, the value in the key-value pair is the field's content. The key and value are separated by ":" .

 

Question: What is BSON ?

Answer:

A serialization format used to store documents and make remote procedure calls in MongoDB. “BSON” is a portmanteau of the words “binary” and “JSON”. Think of BSON as a binary representation of JSON (JavaScript Object Notation) documents. For a detailed spec, see bsonspec.org.

for more info:

http://docs.mongodb.org/manual/reference/glossary/#term-bson

 

Question:Advantages and disadvantages of Mongodb over classical relational database?

Answer:

  • highly optimized for retrieve and append operations.
  • manage large volumes of data that do not necessarily follow a fixed schema.
  • MongoDB can run over multiple servers, balancing the load and/or duplicating data to keep the system up and running in case of hardware failure. Automatic configuration is easy to deploy and new machines can be added to a running database.
  • This is a rich data structure capable of holding arrays and other documents. This means you can often represent in a single entity a construct what would require several tables to properly represent in a relational db.
  •  Deep query-ability: MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
  • In some cases a non-relational database is not better than a relational one. If your database have a lot of relations and normalization, it might makes little sense to use something like mongodb. It's all about finding the right tool for the job.
  • MongoDB is fast but not ACID, it has no transactions.

 


Mongodb setup in windows:

Download MongoDB for Windows
Download the latest production release of MongoDB from http://www.mongodb.org/downloads .
Note : Always download the correct version of MongoDB for your Windows system. The 64-bit versions of MongoDB will not work with 32-bit Windows.
Extract the archive to C:\ by right clicking on the archive and selecting Extract All and browsing to C:\ .
The folder name will be either:
C:\mongodb-win32-i386-[version]
or
C:\mongodb-win32-x86_64-[version]
In both examples, replace [version] with the version of MongoDB downloaded.


Set up the Environment
Open a command prompt with “Administrative Privileges” and issue the following command
cd \
move C:\mongodb-win32-* C:\mongodb
MongoDB requires a data folder to store its files.
Issue the following command sequence:
md data
md data\db


Start MongoDB
To start MongoDB, execute from the Command Prompt:
C:\mongodb\bin\mongod.exe
This will start the main MongoDB database process.
Connect to MongoDB using the mongo.exe shell. Open another Command Prompt and issue the following command:
C:\mongodb\bin\mongo.exe
The mongo.exe shell will connect to mongod.exe running on the localhost interface and port 27017 by default.
At the mongo.exe prompt, issue the following two commands to insert a record in the test collection of the default test database and then retrieve that record:
> db.test.save( { a: 1 } )
> db.test.find()


MongoDB as a Windows Service
Setup MongoDB as a Windows Service, so that the database will start automatically following each reboot cycle.
You should specify two options when running MongoDB as a Windows Service: a path for the log output (i.e. logpath) and a configuration file.
Create a specific directory for MongoDB log files:
md C:\mongodb\log
Create a configuration file for the logpath option for MongoDB in the Command Prompt by issuing this command:
echo logpath=C:\mongodb\log\mongo.log > C:\mongodb\mongod.cfg


Install and Run the MongoDB Service
Run all of the following commands in Command Prompt with “Administrative Privileges:”

To install the MongoDB service:
C:\mongodb\bin\mongod.exe --journal --config C:\mongodb\mongod.cfg --install
Modify the path to the mongod.cfg file as needed. For the --install option to succeed, you must specify a logpath setting or the --logpath run-time option.


--journal will enable journal option.For more info go to http://docs.mongodb.org/manual/administration/journaling/

 

How to start or stop mongodb server on windows:
net start MongoDB
net stop MongoDB


Mongodb setup on ubuntu:


(1)sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

(2)Create a /etc/apt/sources.list.d/10gen.list file
sudo vi /etc/apt/sources.list.d/10gen.list
and add the following line in this file.
deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

(3)sudo apt-get update

(4)sudo apt-get install mongodb-10gen

How to start or stop mongodb server on ubuntu:
sudo service mongodb start
sudo service mongodb stop


 

In case mongodb service get crashed in windows:
If your mongodb service get crashed, start mongoDB with the
"mongod --repair"
flag, but after that you need to restart it.

For more information go to following link :

 http://docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/

 

In case mongodb service get crashed in ubuntu:

If your mongodb service get crashed , remove file "mongod.lock".

File location could be /srv/db/mongodb/mongod.lock

Then restart MongoDB service
/etc/init.d/mongodb start

 

 

Akash Sharma

[email protected]

http://oodlestechnologies.com/

 

About Author

Author Image
Akash Sharma

Akash is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Akash loves playing Cricket and Tennis

Request for Proposal

Name is required

Comment is required

Sending message..