Step X Step : Install and Configure MongoDB in Ubuntu.

Hi There,

Few months ago i have posted a Step X Step reference to Install and configure MongoDB on Windows Operation System. now i am doing the same but to Install and Configure MongoDB on Linux Server (am Using UBUNTU 14.04 LT version).

I. Lets Be Root Users

sudo -s

II. Prepare your System with Latest Updates (do this before any installation of any kind)

sudo apt-get update
sudo apt-get upgrade

note that apt-get update (Used to re-synchronize the package index files from their sources) and apt-upgrade (Used to install the newest versions of all packages currently installed on the system from the sources enumerated in/etc/apt/sources.list) [Reference Here for more details]

III. Import the public key used by the package management system.[Reference For more details]

sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10

IIII. Create a list file for MongoDB. [Reference For more details]

echo 'deb dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

V. Re-synchronize the package index files

sudo apt-get update

VI. Install the latest stable version of MongoDB

sudo apt-get install mongodb-org

At this point we done and we have mongo Installed at the server configured to port 27017 and accept only requests from local host.

Now Lets discover the mongod Config file and see how we can change it to enable remote access to MongoDB.

First : Issue the following comand on the Terminal as Root (sudo -s makes you root)

vim /etc/mongod.conf

Note that “vim” is a popular text editor based off of the venerable vi editor, if you don’t have it, and mostly you will have it , install it with the command

sudo apt-get install vim

Second : the vim will open the mongod.conf file inside your terminal, here we have two lines to Focus.

Line 1: #Port = 27017

This is the default port, and by default commented by ‘#’ remove the ‘#’ and change it and you will change the port that mongo listen to.

Line 2: bind_ip =

This line means that the mongodb engine just accept requests from localhost, comment the line with ‘#’ and you will make mongo accept requests from any IPs, or write the IP of interests, note that you can add more than one IP with ‘,’ separations.

To make any of the above changes, use the arrows in your keyboard up and down to the line of ineterst and use the ‘x’ to delete character and type what ever you want.

After that Typr [ESC] then [:w] then [:q]

:w  (will save the changes)

:q (will exit vim and get back to the treminal)

Finally : Lets Open the port 27017 to Accept requests, do this by the following.

Note: If you are using AZURE VM or Google Compute engine, the following steps are not necessary as you just need to open the Port in the FireWall Rule or EndPoints Manager.

For Any IPs:

iptables -A OUTPUT -p tcp --dport 27017 -j ACCEPT
iptables -A INPUT -p tcp --dport 27017 -j ACCEPT

For Specific IP:

iptables -A INPUT -s <ip-address> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

and the last thing if you have the UFW enabled , you will need to open the port in the firewall

sudo ufw akllow 27017
sudo ufw akllow 27017/tcp