June 5, 2012

Beginner

How To Install MongoDB on Ubuntu 12.04

Tagged In: Mongo Db, Ubuntu

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

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

30 Comments

Write Tutorial
  • Gravatar freakyash_02 over 1 year

    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

  • Gravatar Etel Sverdlov over 1 year

    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.

  • Gravatar aarthiraj561 over 1 year

    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:~$

  • Gravatar Etel Sverdlov over 1 year

    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.

  • Gravatar jeffmsmithdev over 1 year

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

  • Gravatar matthias_thiele about 1 year

    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!

  • Gravatar tonybrown67 11 months

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

  • Gravatar tony.brown.357 11 months

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

  • Gravatar Gianfranco 10 months

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

  • Gravatar my66fr 9 months

    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.

  • Gravatar dmwest 8 months

    Run the lines of the bash script seperately and then run sudo service start mongodb.

  • Gravatar info 7 months

    Hi, thanks for this tutorial! I run this: "sudo service mongodb start" and mongo is started after I wanted to use the mongo shell so I run "mongo" but the answer was: "MongoDB shell version: 2.4.6 connecting to: test Sat Sep 14 19:24:36.797 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145 exception: connect failed" so I suppose I should to change ip. but where?

  • Gravatar Kamal Nasser 7 months

    @info: What's the output of this command?

    sudo netstat -plutn | grep 27017

  • Gravatar nanhe4it 7 months

    Simple Step of installation there is no need to know more about detail if you only want installation copy and paste on terminal sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list sudo apt-get update sudo apt-get install mongodb-10gen

  • Gravatar Asterix 7 months

    I have disabled the root user after I created my "newuser" and can therefore not transfer any files to the /home/ directory, only to /home/newuser/. Can I run $ sudo bash ./mongo_install.bash from /home/newuser/ instead? Just like $ sudo bash .newuser/mongo_install.bash Or how shall I do?

  • Gravatar Asterix 7 months

    And if I create a new VPS user I still want to be able to access mongodb from that user. Shall I just install as nanhe4it mention above as I can't transfer the install script with filezilla to root (as it's disabled)? And if yes, do I need to do anything additional to make mongodb start right back if I need to reboot my Droplet etc?

  • Gravatar Kamal Nasser 6 months

    @Asterix: It doesn't matter which user you install MongoDB as, it's installed as a system service and can be accessed by all users once it's installed. You only need to run sudo bash ./mongo_install.bash as a user with sudo access.

  • Gravatar Asterix 6 months

    Ok, I now uploaded the install script to /home/newuser/ (I can't upload the script to /home/ as it's owned by the root user which is disabled) I then ran sudo bash ./mongo_install.bash And I got following errors: Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret -keyring /tmp/tmp.QTCMmtuUqA --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/ apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver keyserver.ubu ntu.com --recv 7F0CEB10 " not a key ID: skipping deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen E: Invalid operation update Reading package lists... Done Building dependency tree Reading state information... Done ' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extensio n ' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extensio n E: Unable to locate package mongodb-10gen

  • Gravatar Kamal Nasser 6 months

    @Asterix: What's the output of cat /path/to/mongo_install.bash | curl -F 'sprunge=?

  • Gravatar Asterix 6 months

    Hmm, I got nothing. I ran newuser@servername:~$ cat /path/to/mongo_install.bash | curl -F 'sprunge=? > Well all I got was the ">"

  • Gravatar Asterix 6 months

    I mean I ran cat /home/newuser/mongo_install.bash | curl -F 'sprunge=? but no output except for the ">" And I can see my install script mongo_install.bash in /home/newuser/ It has file permission 664 though owned by newuser,

  • Gravatar Asterix 6 months

    When I followed http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ it suddenly seems to work but I get following: "' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension" at the end. What is that? Setting up mongodb-10gen (2.4.6) ... Adding system user `mongodb' (UID 105) ... Adding new user `mongodb' (UID 105) with group `nogroup' ... Not creating home directory `/home/mongodb'. Adding group `mongodb' (GID 108) ... Done. Adding user `mongodb' to group `mongodb' ... Adding user mongodb to group mongodb Done. mongodb start/running, process 12529 ' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension ' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension ' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extension newuser@servername:~$

  • Gravatar Asterix 6 months

    When I run sudo service mongodb start I get: start: Job is already running: mongodb When I run newuser@servername:~$ mongo I get MongoDB shell version: 2.4.6 connecting to: test but when I run $ps -ef | grep mongo I get: -bash: -ef: command not found

  • Gravatar Asterix 6 months

    Ah now it finally worked!! ps -ef | grep mongo mongodb 709 1 0 05:10 ? 00:01:02 /usr/bin/mongod --config /etc/mo ngodb.conf 1000 2083 1942 0 11:21 pts/0 00:00:00 grep --color=auto mongo

  • Gravatar Kamal Nasser 6 months

    @Asterix: My bad, the second half of the command got stripped out because of the filtering system. Is it all fine now?

  • Gravatar grega.vrbancic 6 months

    Using Debian 3.2.41-2+deb7u2 x86_64 GNU/Linux I got following error. ... electing previously unselected package mongodb-10gen. (Reading database ... 26311 files and directories currently installed.) Unpacking mongodb-10gen (from .../mongodb-10gen_2.4.7_amd64.deb) ... Processing triggers for man-db ... locale: Cannot set LC_CTYPE to default locale: No such file or directory locale: Cannot set LC_MESSAGES to default locale: No such file or directory locale: Cannot set LC_ALL to default locale: No such file or directory Setting up mongodb-10gen (2.4.7) ... Adding system user `mongodb' (UID 105) ... Adding new user `mongodb' (UID 105) with group `nogroup' ... Not creating home directory `/home/mongodb'. Adding group `mongodb' (GID 111) ... Done. Adding user `mongodb' to group `mongodb' ... Adding user mongodb to group mongodb Done. invoke-rc.d: unknown initscript, /etc/init.d/mongodb not found. dpkg: error processing mongodb-10gen (--configure): subprocess installed post-installation script returned error exit status 100 Errors were encountered while processing: mongodb-10gen E: Sub-process /usr/bin/dpkg returned an error code (1) Any suggestion?

  • Gravatar Kamal Nasser 6 months

    @grega.vrbancic: This article does not work on Debian. See http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/

  • Gravatar me 6 months

    Great tutorial, step 3 seems to be superfluous on ubuntu 12 though. Got a > mongodb start/running, process 6315 after running > sudo apt-get -y install mongodb-10gen

  • Gravatar fabio.europe 2 days

    I created the bash script, but upon running it I get the following error: Package mongodb-10gen is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source E: Package 'mongodb-10gen' has no installation candidate I then tried using 'sudo apt-get install mongodb-org' (with or without -y) but it didn't work either (E: Unable to locate package mongodb-org). Maybe the package mongodb-10gen has been obsoleted?

  • Gravatar fabio.europe 2 days

    whoops, I got it. it must have been a typo or something like that.

Leave a Comment

Create an account or login:
Ajax-loader