How to Install MongoDB on Ubuntu 14

Try it in our public cloud & Get $5 Credit
CLAIM NOW

MongoDB is a document-oriented, NoSQL database. It features easy sharding for high availability, a variety of index types ranging from geospatial to full-text, an expressive query language, and a JSON superset for serializing records. MongoDB is used by a variety of large companies in mission-critical roles, but is still easy to start with and develop against. Its flexibility and features make MongoDB a database that will grow with you, accommodating your requirements as your application develops. In this guide we will install MongoDB on an Ubuntu 14.04 server, giving you MongoDB’s reliability along with Ubuntu’s stability and long-term support.

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running Ubuntu 14.

When we’re done, MongoDB will be running and ready to integrate into your app or service.

Tutorial

Let’s get started. Begin by updating your packages and package cache. Not only does this prevent download errors later, but it also applies all available bugfixes and security patches. Perform this step regularly on this server to keep it secure and performant.

apt-get update && apt-get upgrade

MongoDB is shipped in package repositories for a variety of distributions and versions. We’ll fetch the GPG key for their repositories so the packages can be verified on installation.

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

Now we’ll actually add the MongoDB repositories to your sources.list file so apt-get can retrieve the package list and install Mongo.

echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Having added the new repository, we’ll next need to update the list of packages.

apt-get update

Having done the update, we now install the MongoDB server and its command line tools.

apt-get install -y --allow-unauthenticated mongodb-org

In order for MongoDB to be launched on boot, we’ll need to create an init script.

First, create the file using the command:

nano /etc/init.d/mongod

The file should have the following content:

#! /bin/sh
### BEGIN INIT INFO
# Provides: mongodb
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Mongodb startup script
# Description: Mongodb start stop daemon sends SIGINT to terminate
# say man signal to see details
# Please check the startup params and replication options
#Mongo db usage:
# run run db
# msg [msg] [port] send a request to the db server listening on port (or default)
# msglots send many test messages, and then wait for answer on the last one
# longmsg send a long test message to the db server
# quicktest just check basic assertions and exit
# test2 run test2() - see code
#
#Options:
# --help show this usage information
# --port specify port number, default is 27017
# --dbpath directory for datafiles, default is /data/db/
# --quiet quieter output
# --cpu show cpu+iowait utilization periodically
# --noauth run without security
# --auth run with security
# --verbose
# -v+ increase verbose level -v = --verbose
# --objcheck inspect client data for validity on receipt
# --quota enable db quota management
# --appsrvpath root directory for the babble app server
# --nocursors diagnostic/debugging option
# --nohints ignore query hints
# --nohttpinterface disable http interface
# --nojni
# --oplog 0=off 1=W 2=R 3=both 7=W+some reads
# --oplogSize custom size if creating new replication operation log
# --sysinfo print some diagnostic system information
#
#Replication:
# --master
# --slave
# --source when a slave, specifies master
# --only when a slave, only replicate db
# --pairwith
# --autoresync
### END INIT INFO
# Author: Kunthar
#
# Do NOT "set -e"
# Check the paths and data dir with additional options at startup...
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/opt/mongodb
DESC="Mongodb Service"
NAME=mongod
MONGOPATH=/opt/mongodb
DAEMON=$MONGOPATH/bin/$NAME
#Please do not forget to give correct path
DBPATH=$MONGOPATH/data/
#If security matters, please change arg to --auth
DAEMON_ARGS="--dbpath $DBPATH --noauth"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
echo -e "Starting $DESC \n"
start-stop-daemon -Sbm -p $PIDFILE --exec $DAEMON -- $DAEMON_ARGS
echo -e "\n started"
}
#
# Function that stops the daemon/service
#
do_stop()
{
echo -e "Stopping $DESC by sending ctrl+c \n"
#Be nice send ctrl+c to mongod daemon
start-stop-daemon --stop --signal 2 -q --pidfile $PIDFILE --name $NAME
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
echo -e "\n stopped"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
}
case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop}" >&2
exit 3
;;
esac
:

Now start MongoDB, and enable it to launch on boot.

service mongod start
update-rc.d mongod defaults

MongoDB should now be running. Let’s check the status of the daemon.

service mongod status

mongod start/running, process 4921

Let’s also check Mongo’s stats as recorded in its internal system database.

Mongostat

insert query update delete getmore command % dirty % used flushes vsize res qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:02Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:03Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:04Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:05Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:06Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:07Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:08Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:09Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:10Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 245.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:11Z
insert query update delete getmore command % dirty % used flushes vsize res qr|qw ar|aw netIn netOut conn time
*0 *0 *0 *0 0 1|0 0.0 0.0 0 246.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:12Z
*0 *0 *0 *0 0 1|0 0.0 0.0 0 246.0M 69.0M 0|0 0|0 79b 18k 1 2016-07-25T21:09:13Z

Conclusion

MongoDB is now installed, running successfully, and configured to launch on boot. You can now integrate it into a local application, or build out several more servers using this guide and cluster them together into a highly-available database setup. If this guide was helpful to you, kindly share it with others who may also be interested.