Install MongoDB on Ubuntu

by LauCyun Nov 24,2017 13:37:38 21,163 views

Use this tutorial to install MongoDB on LTS Ubuntu Linux systems from .deb packages. While Ubuntu includes its own MongoDB packages, the official MongoDB packages are generally more up-to-date.

1 Packages

MongoDB provides officially supported packages in their own repository. This repository contains the following packages:

Package Name Description
mongodb-org metapackage that will automatically install the four component packages listed below.
mongodb-org-server Contains the mongod daemon and associated configuration and init scripts.
mongodb-org-mongos Contains the mongos daemon.
mongodb-org-shell Contains the mongo shell.
mongodb-org-tools Contains the following MongoDB tools: mongoimport bsondumpmongodumpmongoexportmongofilesmongooplogmongoperfmongorestoremongostat, and mongotop.

The mongodb-org-server package provides an initialization script that starts mongod with the /etc/mongod.conf configuration file.

These packages conflict with the mongodbmongodb-server, and mongodb-clients packages provided by Ubuntu.

2 Install MongoDB

MongoDB only provides packages for 64-bit LTS (long-term support) Ubuntu releases. For example, 12.04 LTS (precise), 14.04 LTS (trusty), 16.04 LTS (xenial), and so on. These packages may work with other Ubuntu releases, however, they are not supported.

2.1 Import the public key used by the package management system.

The Ubuntu package management tools (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys. Issue the following command to import the MongoDB public GPG Key:

sudo apt-key adv --keyserver hkp:// --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

2.2 Create a list file for MongoDB

Create the /etc/apt/sources.list.d/mongodb-org-3.6.list list file using the command appropriate for your version of Ubuntu:

  • Ubuntu 12.04

    echo "deb [ arch=amd64 ] precise/mongodb-org/testing multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
  • Ubuntu 14.04

    echo "deb [ arch=amd64 ] trusty/mongodb-org/testing multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
  • Ubuntu 16.04

    echo "deb [ arch=amd64,arm64 ] xenial/mongodb-org/testing multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

2.3 Reload local package database.

Issue the following command to reload the local package database:

sudo apt-get update

2.4 Install the MongoDB packages.

Install the latest stable version of MongoDB.

Issue the following command:

sudo apt-get install -y mongodb-org

3 Run MongoDB

The MongoDB instance stores its data files in /var/lib/mongodb and its log files in /var/log/mongodbby default, and runs using the mongodb user account. You can specify alternate log and data file directories in /etc/mongod.conf

If you change the user that runs the MongoDB process, you must modify the access control rights to the /var/lib/mongodb and /var/log/mongodb directories to give this user access to these directories.

3.1 Start MongoDB.

Issue the following command to start mongod:

sudo service mongod start

3.2 Run MongoDB On boot.

sudo systemctl enable mongod

3.3 Verify that MongoDB has started successfully.

Verify that the mongod process has started successfully by checking the contents of the log file at/var/log/mongodb/mongod.log for a line reading

[initandlisten] waiting for connections on port <port>

where <port> is the port configured in /etc/mongod.conf27017 by default.

or issue the following command to see mongod status:

sudo systemctl status mongod

3.4 Stop MongoDB.

As needed, you can stop the mongod process by issuing the following command:

sudo service mongod stop

3.5 Restart MongoDB.

Issue the following command to restart mongod:

sudo service mongod restart

4 Settings MongoDB

4.1 Start the mongo Shell

To start the mongo shell and connect to your MongoDB instance running on localhost with default port:

  1. At a prompt in a terminal window (or a command prompt for Windows), go to your <mongodbinstallation dir>:

    cd <mongodb installation dir>
  2. Type ./bin/mongo to start mongo:


    If you have added the <mongodb installation dir>/bin to the PATH environment variable, you can just type mongo instead of ./bin/mongo.

4.2 Create the user administrator.

MongoDB login authentication is turned off by default, any user of the local system can access the database, and have full access. To improve database security, let's turn on authentication and create an administrator user.

In the admin database, add a user with the userAdminAnyDatabase role. For example, the following creates the user myUserAdmin in the admin database:

> use admin
> db.createUser(
    user: "admin",
    pwd: "12345678",
    roles: [
       { role: "userAdminAnyDatabase", db: "admin" }

Later, press Control+C in the terminal to disconnect the mongo shell.

Detail to see:

4.3 Re-start the MongoDB instance with access control

Add the security.authorization setting to the config file:

  authorization: enabled

It may look like this:

# mongod.conf

# for documentation of all options, see:

# Where and how to store data.
  dbPath: /var/lib/mongodb
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
  port: 27017

# how the process runs
  timeZoneInfo: /usr/share/zoneinfo

  authorization: "enabled"

save and restart the daemon without the --auth.

4.4 Connect to database instance with superAdmin access

Issue the following command:

mongo -u admin -p --authenticationDatabase admin

5 Reference