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.
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.
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
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:
TIP: At any time, to change to your home directory, simply execute `cd`
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
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.confWe can see the...
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
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
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:~$
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.
Run the lines of the bash script seperately and then run sudo service start mongodb.
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?
@info: What’s the output of this command?
<pre>sudo netstat -plutn | grep 27017</pre>
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
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?
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?
@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.
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
@Asterix: What’s the output of <code>cat /path/to/mongo_install.bash | curl -F ‘sprunge=<-’ http://sprunge.us</code>?
Hmm, I got nothing. I ran newuser@servername:~$ cat /path/to/mongo_install.bash | curl -F 'sprunge=?
Well all I got was the “>”
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,
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 groupnogroup' ... Not creating home directory
/home/mongodb’. Adding groupmongodb' (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:~$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
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
@Asterix: My bad, the second half of the command got stripped out because of the filtering system.
Is it all fine now?
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 groupnogroup' ... Not creating home directory
/home/mongodb’. Adding groupmongodb' (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?
@grega.vrbancic: This article does not work on Debian. See <a href=“http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/”>http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/</a>
Great tutorial, step 3 seems to be superfluous on ubuntu 12 though. Got a
after running
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?
whoops, I got it. it must have been a typo or something like that.
If you are getting the following error when running the bash script:
“WARNING: The following packages cannot be authenticated!”
Then you simply need to modify the last two lines of the script to:
apt-get -y --force-yes update apt-get -y --force-yes install mongodb-10gen
Everything should work fine after that.
Thanks for this, but I just tried it and got v2.4.1 installed, not the latest.
The instructions from 10gen here work.
Looks like they changed the name of their package for the latest release.
I concur - it would save time to have the last comment appear first.
Every now and than it looks like mongodb stops running.
When I
sudo service mongod start
It starts again.
Is there a way to auto start or check and auto start mongodb?