Wednesday 2 September 2015

Installing MongoDB on Ubuntu

Run the following command to import the MongoDB public GPG Key:
girdhar@dcsa:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Create a /etc/apt/sources.list.d/mongodb.list file using the following command.
girdhar@dcsa:~$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
Now issue the following command to update the repository:
girdhar@dcsa:~$ sudo apt-get update
Now install the MongoDB by using following command:
girdhar@dcsa:~$ sudo apt-get install mongodb-10gen=2.2.3
In the above installation 2.2.3 is currently released mongodb version. Make sure to install latest version always. Now mongodb is installed successfully.

Start MongoDB
girdhar@dcsa:~$ sudo service mongodb start
Stop MongoDB
girdhar@dcsa:~$ sudo service mongodb stop
Restart MongoDB
girdhar@dcsa:~$ sudo service mongodb restart
To use mongodb run the following command
girdhar@dcsa:~$ mongo
This will connect you to running mongod instance.



If It will not run (giving some error in locks), try running following: -
Step 1: Remove lock file.
girdhar@dcsa:~$ sudo rm /var/lib/mongodb/mongod.lock

Step 2: Repair mongodb.
girdhar@dcsa:~$ sudo mongod --repair

Step 3: start mongodb.
girdhar@dcsa:~$ sudo start mongodb
or
girdhar@dcsa:~$ sudo service mongodb start

Step 4: Check status of mongodb.
girdhar@dcsa:~$ sudo status mongodb
or
girdhar@dcsa:~$ sudo service mongodb status

Step 5: Start mongo console.
girdhar@dcsa:~$ mongo