NoSQL Basics and Installing MongoDB 3 on Ubuntu

NoSQL Basics and Installing MongoDB 3 on Ubuntu

MongoDB is currently the most popular NoSQL database system and fourth in the overall ranking of database systems! The growth of MongoDB has been impressive over the last few years as a document-oriented database system that uses JSON-like objects for describing the data model.

We’ll cover installing MongoDB from standard Ubuntu packages, as well as the official MongoDB packages, and also discuss using MongoDB, covering the basics of configuration and security.

Non-Relational Databases for Scalability

In a sense, the non-relational database has been rediscovered in the last few years. Many leading tech companies like Google, Facebook, and Amazon were already using non-relational databases in their large production clusters, when in 2009 the term “NoSQL” was first coined to give the various non-SQL database system implementations a common name.

From that point, the use of NoSQL databases among many other companies (especially startups) and open-source projects has been growing ever since.

The NoSQL database systems are particularly well-suited for web services that need to scale (by using simpler data replication across a cluster of machines) and also to adapt to quickly-changing data models (by using dynamic schemas). The NoSQL data structures used by the various database implementations can be: key-value stores, graphs, or documents.

Install MongoDB via Standard Ubuntu Packages

Ubuntu includes its own MongoDB packages. However, the official MongoDB packages are generally more up-to-date.

We recommend using the official MongoDB packages, especially for production services, but if you want, you can use the following command to install the MongoDB packages that are included in Ubuntu.

This will install the MongoDB server and the related client tools (see the next section for the list).

Install MongoDB via Official MongoDB Packages

Installing MongoDB with the official MongoDB packages is recommended for production.

Step 1: Import the MongoDB Public GPG Key

Step 2: Add the MongoDB Repository to Your Package Sources

Step 3: Update the Package List

Step 4: Install the MongoDB Packages

The output of this command will show the list of MongoDB packages that will be installed:

During the install, a new user named mongodb will be created, and the mongodb server will be started automatically.

After the install, the following MongoDB executables should be available:

  • mongo – Interactive JavaScript shell interface to MongoDB to test queries
  • mongod – The primary daemon process for the MongoDB system
  • mongodump – Creates a binary export of the contents of a database
  • mongoexport – Produces a JSON or CSV export of MongoDB data
  • mongofiles – Manipulates files stored in MongoDB as GridFS objects
  • mongoimport – Imports content from JSON, CSV, or TSV
  • mongooplog – Polls operations from the replication oplog of a remote server
  • mongoperf – Checks disk I/O performance independently of MongoDB
  • mongorestore – Writes data from a binary database dump to a MongoDB instance
  • mongos –  A routing service for determining the location of data in a sharded cluster
  • mongostat – Provides a quick overview of a currently running mongod or mongos instance
  • mongotop – Tracks the amount of time a MongoDB instance spends reading and writing data

Now we can check what version of MongoDB we just installed:

That is the latest stable MongoDB version. Now we’ll see how we can use MongoDB.

Using MongoDB

Now we’ll discuss a couple details regarding utilizing MondoDB in general — from server management to security maintenance, and more.

Controlling the MongoDB server

Note that the upstart-scripts (which were just installed with our MongoDB package) will automatically start and stop the MongoDB server for you during bootups and shutdowns. However, sometimes you need to stop and start the server manually.

Stopping the server (as mongod should already be running now):

Starting the server:

Restarting the server (useful when the configuration files have been changed and need reloading):

MongoDB Configuration and Logs

The MongoDB server configuration file is located in /etc/mongod.conf. The options of this file are described here.

Normally, the default configuration setup is fine to begin with, but as the next section will make clear, you will need to edit this file for various security measures. If you have made changes to this file, you will need to restart your MongoDB server.

By default, MongoDB stores its data in /data/db/ and the log file is located at /var/log/mongodb/mongod.log.

MongoDB Security and Access Control

Before you begin your exploration of this great NoSQL database, it’s important to also think about the security of your MongoDB installation. As NoSQL databases are most often used in a web service and cluster-computing context, good security measures are critical.

Recently there was a major security issue with tens of thousands of vulnerable MongoDB installs on the Internet. The issue was not due to MongoDB itself, but due to the fact that the users of MongoDB were not applying proper access controls to their server configuration.

MongoDB has excellent documentation on how to apply the right security measures. At the very least, you should implement the relevant items on this security checklist before connecting your production MongoDB server to the Internet.

Check out these links for more on MongoDB and security:

Getting Started with MongoDB

Now we are ready to start doing MongoDB database operations. The official MongoDB beginners tutorial will quickly guide you into understanding how these operations are done.

The tutorial starts by executing the mongo command line tool we just installed:

As you can see, we are being connected to the test database by default.

Final Thoughts on MongoDB

From here, you can just follow the beginners tutorial and the manual for more help with MongoDB.

Consider also keeping an eye on the official blog to learn about new releases, best practices, and how others are using MongoDB.

We hope you had fun getting to know more about this popular NoSQL database system. It looks like MongoDB is only going to grow in relevance, so it’s definitely a resource worth understanding.

Roberto Sanchez

Questions or Comments? Ask Roberto!

Ask a question and Roberto will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.