Skip to content

MongoDB Setup for Production

Adrian Matei edited this page Jul 1, 2017 · 8 revisions

This wiki page presents how MongoDB (v3.2) is configured in production on Ubuntu 16.04 Xenial to support www.codingmarks.org

Add the MongoDB Repository

Because the Ubuntu repositories don’t contain a current version, we’ll need to use the MongoDB repository.

  1. Import the MongoDB public GPG key for package signing

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://keyserver.ubuntu.com:80 --recv EA312927
  1. Create a list for MongoDB Add the MongoDB repository to your sources.list.d directory:
$ echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
  1. Update your repositories. This allows apt to read from the newly added MongoDB repo:
sudo apt-get update

Install MongoDB

Now that the MongoDB repository has been added, we’re ready to install the latest stable version (at that time was 3.2) of MongoDB:

$ sudo apt-get install mongodb-org

This command installs mongodb-org, a meta-package that includes the following:

  • mongodb-org-server - The standard MongoDB daemon, and relevant init scripts and configurations
  • mongodb-org-mongos - The MongoDB Shard daemon
  • mongodb-org-shell - The MongoDB shell, used to interact with MongoDB via the command line
  • mongodb-org-tools - Contains a few basic tools to restore, import, and export data, as well as a variety of other functions.

These packages provide a good base that will serve most use cases, and we recommend installing them all. However, if you want a more minimal installation, you can selectively install packages from the above list rather than using the mongodb-org metapackage.

For more information on the installation process and options, refer to the official MongoDB installation tutorial.

Run MongoDB community edition

To start, restart, or stop the MongoDB service, issue the appropriate command from the following:

$ sudo systemctl start mongod
$ sudo systemctl restart mongod
$ sudo systemctl stop mongod

You can also enable MongoDB to start on boot:

$ sudo systemctl enable mongod

Configure MongoDB

The configuration file for MongoDB is located at /etc/mongod.conf, and is written in YAML format. Most of the settings are well commented within the file. We’ve outlined the default options below:

  • dbPath indicates where the database files will be stored (/var/lib/mongodb by default)
  • systemLog specifies the various logging options, explained below:
    • destination tells MongoDB whether to store the log output as a file or syslog
    • logAppend specifies whether to append new entries to the end of an existing log when the daemon restarts (as opposed to creating a backup and starting a new log upon restarting)
    • path tells the daemon where to send its logging information (/var/log/mongodb/mongod.log by default)
  • net specifies the various network options, explained below:
    • port is the port on which the MongoDB daemon will run
    • bindIP specifies the IP addresses MongoDB to which binds, so it can listen for connections from other applications

We strongly recommend uncommenting the security section and adding the following:

File excerpt: /etc/mongod.conf

security:
  authorization: enabled

The authorization option enables [role-based access control]https://docs.mongodb.com/manual/core/authorization/) for your databases. If no value is specified, any user will have the ability to modify any database. We’ll explain how to create database users and set their permissions later in this guide.

For more information on how to customize these and other values in your configuration file, refer to the official MongoDB configuration tutorial.

After making changes to the MongoDB configuration file, restart the service as shown above.

Clone this wiki locally