Install and Configure MongoDB on Ubuntu 14


MongoDB is one of the most popular open source NoSQL database solutions available. Unlike relational databases that store rows of data, document-oriented databases like this one store data as, you guessed it, documents. These types of databases are not constricted by rigid schemas. They also scale much easier and more efficiently than relational databases, meaning it is a lot easier to store once unthinkable amounts of application data.

MongoDB is able to achieve and maintain high performance – much greater than any relational database, such as Microsoft SQL – even while storing petabytes of data by offloading a lot of logic to your application. A relational database it is not, but this tradeoff gives our developers the flexibility they need to work with data in today’s high volume, big data world.

The tutorial will guide you through the installation through to managing your databases.

Installing MongoDB

  1. Import the public key used by the MongoDB package management system.
    sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10
  2. Create an aptitude source file for MongoDB, which is used to query MongoDB’s package management system.
    echo "deb trusty/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
  3. Refresh the local package database.
    sudo apt-get update
  4. Install the latest stable version of MongoDB.
    sudo apt-get install -y mongodb-org


Starting and Stopping MongoDB

By default on Ubuntu, MongoDB will automatically be started after installation. However, there will come a time when you’re going to have to either start or stop the database server.

  • Starting MongoDB
    service mongod start
  • Stopping MongoDB
    service mongod stop
  • Restarting MongoDB
    service mongod restart



Creating A Database

Databases can be created on-the-fly by our developers through their application. However, for security reasons, it may be best to create them beforehand, so that we may add some restrictions.

  1. Log into the MongoDB console
  2. As you probably just realized, we can access the server without any credentials. And even lacking this, we are able to create and manage databases. We’ll address security a little later in this tutorial.
  3. Using the use command, create your database. Use, you ask. But I haven’t created the database yet! If the database does not exist, the use command will create it.
    use mydatabase


Creating User Accounts

In order for us to begin securing our MongoDB server, we’ll need accounts. Some accounts will be for our applications to store and retrieve data, and others will be for operational work on the server.

Creating a Root Admin Account

You likely want at least one account that has full privileges to Mongo and all created databases.

  1. Access the Mongo console.
  2. Set the database to Admin
    use admin
  3. Run the following command, replacing user and password with your own. Ideally, this account should NOT be named root.

Creating your Application Service Account

Every application should have its own account, and each account should limited privileges – allowing on that which must be done for your application to run. Your application shouldn’t be able to create user accounts unless your application’s purpose is to be management front-end for MongoDB.

  1. If not already, log into the Mongo console.
  2. Select the application database you will be using. If it doesn’t already exist, it will automatically be created.
    use myapp
  3. Create the service account for your application, changing the user and pwd values for your environment.
       roles: [
          { role: "readWrite", db:"myapp"}