Skip to content
This repository has been archived by the owner on Jul 14, 2024. It is now read-only.

MongoDB Quickstart

Marc Dutoo edited this page Jun 22, 2015 · 12 revisions

Installation :

Install on Linux Ubuntu or Debian :

On Ubuntu, as said at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ :

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

# then install 2.6.10 and pin (prevent upgrades) :
#sudo apt-get install mongodb-org=2.6.9
sudo apt-get install -y mongodb-org=2.6.10 mongodb-org-server=2.6.10 mongodb-org-shell=2.6.10 mongodb-org-mongos=2.6.10 mongodb-org-tools=2.6.10
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

On Debian, replace the first line by, as said at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/ :

echo 'deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

(should be already done) start :

sudo service mongodb start

This should display the following start trace (or look in /var/log/mongodb/mongodb.log) :

Mon Aug 26 19:27:50.152 [initandlisten] db version v2.4.6
Mon Aug 26 19:27:50.152 [initandlisten] git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673
Mon Aug 26 19:27:50.152 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686     BOOST_LIB_VERSION=1_49
Mon Aug 26 19:27:50.152 [initandlisten] allocator: system
Mon Aug 26 19:27:50.152 [initandlisten] options: { config: "/etc/mongodb.conf", dbpath: "/var/lib/mongodb", logappend: "true", logpath: "/var/log/mongodb/mongodb.log" }

To automate startup (required ??) :

update-rc.d mongodb defaults

Install on Linux Centos 6 :

As said at http://lancegatlin.org/tech/centos-6-install-mongodb , create /etc/yum.repos.d/10gen.repo :

[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64
gpgcheck=0

Then do :

# install
sudo yum install mongo-10gen mongo-10gen-server
# start
sudo service mongod start
# auto startup
sudo chkconfig mongod on

test :

mongo
db.test.save( { a: 1 } )
db.test.find()
exit

configuration :

Edit /etc/mongodb.conf

Journaling

Since version 2.0, journaling is enabled by default for 64-bit builds of mongo.

If you are running a 32-bit build, just add journal = true to your configuration file and restart mongod.

32 bit builds are limited to less than 2GB of data (or less with --journal). Note that journaling defaults to off for 32 bit and is currently off. See http://dochub.mongodb.org/core/32bit

Notes :

NB. which version : MongoDB is provided as (.deb) packages within distributions (Ubuntu...), however MongoDB provider 10gen provides more up to date packages, which are therefore chosen. (from http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ )

NB. in production, MongoDB 64 bits is required, which will be automatically chosen if you've got a 64 bit system. To make sure : uname -m

NB. to check your system : lsb_release -a

FAQ - can't start, "Unknown job: mongod":

Solution: must use sudo, even as root, because is an Upstart job http://stackoverflow.com/questions/23558116/unable-to-connect-mongodb-connection-refused

FAQ - "can't map file memory" error:

You're probably using a 32 bits MongoDB deployment (which is fine unless you're not in development environment) and the 4GB limit has been hit.

Solution: find out your biggest collections (ex. http://joey.aghion.com/listing-mongodb-collections-by-size/), drop some (db.myCollection.drop()), compact your database by stopping the server (only db.repairDatabase() in mongo shelle doesn't work) and doing a repair (see below), then check how much you've gained by db.stats(). More about space management here.

FAQ - server crash while doing big map/reduces:

Causes: either TODO known bug, or 32 bits MongoDB deployment (see above)

FAQ - recover from crash :

problem : no mongo process, even when trying to start it

in log /var/log/mongodb/mongodb.log :

**************
Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repair for recovery instructions.
*************
Tue Sep  3 13:59:24.291 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
...

As said at http://docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/ , solution is :

  • (for production deployments) part of a replica set (OR journaling ??) : restore from a backup or restart the mongod instance with an empty dbpath (that will be sync'd)
  • (for usual development deployments) not running as part of a replica set AND do not have journaling enabled : --repair optionally with --repairpath to write repaired files to

So to repair your development MongoDB, just do this :

su
rm /var/lib/mongodb/mongod.lock
# direct repair (with the right file rights)
sudo -u mongodb -g mongodb mongod -dbpath /var/lib/mongodb --repair
# ALT in production, to repair to : mongod -dbpath /var/lib/mongodb --repair --repairpath=REPAIRPATH
# and test : mongod -dbpath REPAIRPATH
# patch rights (not enough to do sudo -u mongodb -g mongodb...)
chown -R mongodb /var/lib/mongodb/
chgrp -R mongodb /var/lib/mongodb/
# restart (BEWARE doesn't work without sudo even as root !)
sudo service mongodb start