Tutorial

How To Install MongoDB on Ubuntu 12.04

Published on June 5, 2012
How To Install MongoDB on Ubuntu 12.04
Not using Ubuntu 12.04?Choose a different version or distribution.
Ubuntu 12.04

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

Install MongoDB on Ubuntu 12.04

MongoDB is a document database used commonly in modern web applications. This tutorial should help you setup a virtual private server to use as a dedicated MongoDB server for a production application environment.

Step 1 -- Create a Droplet

This one's easy. Once you're done, go ahead and `ssh` in.

N.B. :: It is recommended that you configure `ssh` and `sudo` like this

Step 2 -- Create the Install Script

The MongoDB install process is simple enough to be completed with a Bash script. Copy the following into a new file named `mongo_install.bash` in your home directory:

apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | tee -a /etc/apt/sources.list.d/10gen.list
apt-get -y update
apt-get -y install mongodb-10gen

Here's an explanation of each line in the script:

  • The `apt-key` call registers the public key of the custom 10gen MongoDB aptitude repository
  • A custom 10gen repository list file is created containing the location of the MongoDB binaries
  • Aptitude is updated so that new packages can be registered locally on the Droplet
  • Aptitude is told to install MongoDB

TIP: At any time, to change to your home directory, simply execute `cd`

Step 3 -- Run the Install Script

Execute the following from your home directory:

$ sudo bash ./mongo_install.bash

If everything is successful, you should see the output contain a PID of the newly started MongoDB process:

mongodb start/running, process 2368

Step 4 -- Check It Out

By default with this install method, MongoDB should start automatically when your Droplet is booted. This means that if you need to reboot your Droplet, MongoDB will start right back up.

To start learning about the running `mongod` process, run the following command:

$ ps aux | grep mongo

One line of the output should look like the following:

mongodb    569  0.4  6.4 627676 15936 ?        Ssl  22:54   0:02 /usr/bin/mongod --config /etc/mongodb.conf

We can see the...

  • User: `mongodb`
  • PID: `569`
  • Command: `/usr/bin/mongod --config /etc/mongodb.conf`
  • Config File: `/etc/mongodb.conf`

Resources

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

cool! this works for me after I install mongodb and i get this error after updating:

W: GPG error: http://downloads-distro.mongodb.org dist Release: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 9ECBEC467F0CEB10

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
September 6, 2012

You may be reaching this error because the GPG key, which is specified in the first line of the script, is transferred through port 11371. It is possible that this port may be blocked in your firewall setup.

I would recommend looking at your IP tables and, if it applies, unblocking port 11371. You can see your IP Table rules with the command “iptables -L” Let us know if you have further issues.

I have done this step by step… I even got the same results as you specified!!.. but whenever I say aarti@aarti-Vostro-1540:~$ mongo MongoDB shell version: 2.2.0 connecting to: test Thu Sep 13 11:38:30 Error: couldn’t connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91 exception: connect failed aarti@aarti-Vostro-1540:~$

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
September 13, 2012

It may be that Mongo is not turned on.

Before using the “mongo” command, go ahead and start mongo running with “sudo service mongodb start”

Let me know if that works.

I ran sudo service mongodb start and it said it was already running. I typed mongo after that and everything was peachy. Very strange.

I believe it only creates the data directory on the first start but then exits. So, once you restart it once it works like a charm!

I followed the docs manual off mongodb.org works like a charm!

if you are getting an error trying to install a recent version on mongodb, check this out: https://jira.mongodb.org/browse/SERVER-6910

PLEASE add the fact to setup iptables!! otherwise your mongodb server will be open to the world! (default port and default auth)

apparently, when when I finish the step two, the mongo db has already created and ready to go. No step 3 is needed. Also looks like I need to replace user name and password immediately after creating the mongo db.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.