Software Development

MongoDB Installation – How to install MongoDB

This article is part of our Academy Course titled MongoDB – A Scalable NoSQL DB.

In this course, you will get introduced to MongoDB. You will learn how to install it and how to operate it via its shell. Moreover, you will learn how to programmatically access it via Java and how to leverage Map Reduce with it. Finally, more advanced concepts like sharding and replication will be explained. Check it out here!

1. Introduction

The NoSQL movement gave birth to a vast amount of different data stores to cover the needs of mostly any imaginable application: key/value stores, graph databases, document databases, … In this tutorial we are going to cover the distinguishing member of document data stores family – MongoDB (http://www.mongodb.org/). The quote is from the MongoDB site:

“MongoDB (from “humongous”) is an open-source document database, and the leading NoSQL database, written in C++.”

This brief and simple definition outlines the main design goal of the MongoDB as being storage of documents. With no surprise, document is just a set of fields (or properties) and their values, with no particular schema defined or required.

MongoDB is quite a mature product. With the latest release at the moment of writing being 2.6 (http://www.mongodb.org/downloads), MongoDB brings following key features:

  • Stores JSON-style documents with dynamic schemas.
  • Full index support: index on any document’s attribute.
  • Replication and high availability.
  • Auto-sharding for horizontal scaling.
  • Rich, document-based queries support.
  • Fast and atomic in-place document updates.
  • Out of the box Map/Reduce support for aggregation and data processing.
  • GridFS file system.

MongoDB is an excellent choice if it fits into your application architecture, data access patterns and storage requirements. In this part of the tutorial we are going to cover the MongoDB installation procedure on different operating systems.

2. License

MongoDB itself is distributed under Free Software Foundation’s GNU AGPL v3.0 license. The drivers for different programming languages which are supported by MongoDB are released under Apache License v2.0 license. For more details about licensing please refer to the official documentation.

3. Documentation

MongoDB team provides an excellent and up to date documentation available online: http://docs.mongodb.org/manual/. It is a great source to get the comprehensive overview of MongoDB and its features. Throughout the tutorial, we will refer a lot to the different sections of MongoDB documentation so you can jump quickly to the relevant topic.

4. Architecture Overview

At the top of MongoDB data model hierarchy is a database. Database is a physical container for collections. Each database gets its own set of files on the file system and a single MongoDB server can have multiple databases.

All documents are grouped into collections. A collection is the equivalent of an RDBMS table and exists within a single database. Collections do not enforce a schema and documents within a collection can have different fields. From data modeling prospective, it is recommended that all documents in a collection should have a similar structure or at least be somehow related.

A document forms a record in a MongoDB collection and is the basic unit of data in MongoDB. Documents are analogous to JSON objects but exist in the database in a more type-rich format known as BSON (binary JSON).

Although MongoDB is not an in-memory data store, it uses the technique called memory-mapped files to increase I/O performance.

5. Installing MongoDB

The one of the strongest point of MongoDB is a very easy installation procedure, no matter which platform you are targeting. It literally follows the download-and-run pattern, no additional configuration required. The installation packages are provided for all major operation systems: Windows, Linux and MacOS X. In this section we are going to cover MongoDB install procedure for all the platforms mentioned above.

For Windows and Linux operating systems both 32-bit and 64-bit version are provided, however the recommended platform for running MongoDB server processes is 64-bit.

Note: MongoDB also provides prebuilt binary distribution for the Solaris platform. We are not going to cover this operation system here but the installation procedure is very similar to the Linux one.

5.1. Installing MongoDB on Windows

On a Windows platform, there are two ways MongoDB could be installed: from the Microsoft Installer package (*.msi) or from the archive. We are going to cover both options starting from simplest one, the archive.

Whatever route you are going to pick, the resulting set of binary files will be the same so it is just a matter of a personal preference. Please notice that MongoDB does not support Windows XP or older Windows versions.

5.1.1 Installing from archive

To install MongoDB from the archive, it is enough to perform these two simple steps:

  1. Download the distribution archive
  2. Unpack the distribution archive
  3. Once downloaded, please extract the archive to the convenient location on your local hard drive.

For more advanced installation options please refer to official documentation.

5.1.2 Installing from MSI package

The procedure to install MongoDB from the prebuilt *.msi package is also very simple:

  1. Download the distribution *.msi package
  2. Run the downloaded *.msi package
  3. Follow the Installation Wizard steps (optionally, pick the convenient location on your local hard drive)

In case you have decided to perform the typical installation, the default destination location will be C:\Program Files\MongoDB 2.6 Standard.

For more advanced installation options please refer to official documentation.

5.2. Installing MongoDB on Linux

To install MongoDB on Linux, the respective binaries are already prebuilt and provided as the distribution archives. Let us walk through the installation process step by step.

  1. Download the distribution archive
  2. Unpack the distribution archive
    • For Linux 32-bit distribution archive: tar xf mongodb-linux-i686-2.6.0.tgz The mongodb-linux-i686-2.6.0 folder should be created containing all the binaries.
    • For Linux 64-bit distribution archive: tar xf mongodb-linux-x86_64-2.6.0.tgz The mongodb-linux-x86_64-2.6.0 folder should be created containing all the binaries.

One thing to notice is that the MongoDB distribution does not include control scripts (to be placed into /etc/init.d) to run the server component at system startup time. However, MongoDB provides own official packages which do deploy those scripts and could be installed using different package managers. The detailed installation instructions are available for Ubuntu, CentOS/Fedora and Debian.

For more advanced installation options and steps for different Linux distributions please refer to official documentation.

5.3. Installing MongoDB on MacOS X

The installation on MongoDB is very similar to the Linux version except the fact that only 64-bit distribution is provided. Let us take a closer look on the installation steps.

  1. Download the distribution archive: https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.6.0.tgz
  2. Unpack the distribution archive
    Once downloaded, please run the command:

    tar xf mongodb-osx-x86_64-2.6.0.tgz

    The mongodb-osx-x86_64-2.6.0 folder should be created containing all the binaries.

For more advanced installation options please refer to official documentation.

6. MongoDB Server and Tools

The MongoDB binary distribution consists of several executables, representing server process itself and set of tools on top of it. By default, all executables are located inside the bin folder of the installed distribution.

In this section we are going to look inside the distribution and go through every executable, covering both Linux and Windows versions.

Executable file nameDescription
mongod

mongod.exe

The primary server (daemon) process for the MongoDB system. It handles data requests, manages data format, and performs background management operations.

 

For more details and command line arguments please refer to official documentation.

mongo

mongo.exe

An interactive JavaScript shell interface to MongoDB, which provides a powerful interface for systems administrators as well as a way for developers to test queries and operations directly with the database. It also provides a fully functional JavaScript environment for use with a MongoDB.

 

For more details and command line arguments please refer to official documentation.

mongos

mongos.exe

A routing service for MongoDB shard configurations that processes queries from the application layer, and determines the location of this data in the sharded cluster, in order to complete these operations. From the perspective of the application, a mongos / mongos.exe instance behaves identically to any other MongoDB instance.

 

For more details and command line arguments please refer to official documentation.

mongodump

mongodump.exe

A powerful utility for creating a binary export of the contents of the databases, very useful for creating effectively backups. It could be used in conjunction with mongorestore / mongorestore.exe to restore databases. It can read data from either mongod / mongod.exe or mongos / mongos.exe instances, in addition to reading directly from MongoDB data files without an active mongod / mongod.exe running.

 

For more details and command line arguments please refer to official documentation.

mongorestore

mongorestore.exe

This tool writes data from a binary database dump created by mongodump / mongodump.exe to a MongoDB instance. It can create a new database or add data to an existing database. Similarly to mongodump / mongodump.exe, it can write data to either mongod / mongod.exe or mongos / mongos.exe instances, in addition to writing directly to MongoDB data files without an active mongod / mongod.exe running.

 

For more details and command line arguments please refer to official documentation.

mongoexport

mongoexport.exe

A utility that produces a JSON or CSV exports of data stored in a MongoDB instance. It works in conjunction with mongoimport / mongoimport.exe utility, which provides the importing capability.

 

Note: It is not recommended to use this utility for full-scale production backups because it may not reliably capture data type information. It is recommended to use mongodump / mongodump.exe instead.

 

For more details and command line arguments please refer to official documentation.

mongoimport mongoimport.exeA utility that provides an ability to import content from a JSON, CSV, or TSV exports created by mongoexport / mongoexport.exe, or potentially, another third-party export tool. It works in conjunction with mongoexport / mongoexport.exe utility, which provides the exporting capability.

 

Note: It is not recommended to use this utility for full-scale production backups because it may not reliably capture data type information. It is recommended to use mongorestore / mongorestore.exe instead.

 

For more details and command line arguments please refer to official documentation.

mongotop

mongotop.exe

It is an important diagnostic utility which provides a method to track the amount of time a MongoDB instance spends reading and writing data. The statistics is provided on a per-collection level. By default, mongotop / mongotop.exe returns the values every second.

 

For more details and command line arguments please refer to official documentation.

mongostat

mongostat.exe

A useful diagnostic utility which provides a quick overview of the status of a currently running mongod / mongod.exe or mongos / mongos.exe instance. By default, it connects to the instance running on the local host but it can connect to any accessible remote instance as well.

 

For more details and command line arguments please refer to official documentation.

mongofiles

mongofiles.exe

This utility makes it possible to manipulate files stored in MongoDB instance in GridFS objects from the command line. It is particularly useful as it provides an interface between objects stored in your file system and GridFS. In addition, the tool can access MongoDB data files directly without an active mongod / mongod.exe running.

 

For more details and command line arguments please refer to official documentation.

mongooplog

mongooplog.exe

Although pretty low-level, it is a simple tool that polls operations from the replication oplog of a remote server, and applies them to the local server. This capability supports certain classes of real-time migrations that require that the source server remain online and in operation throughout the migration process.

 

For more details and command line arguments please refer to official documentation.

mongoperf

mongoperf.exe

A diagnostic utility which allows checking disk I/O performance independently of MongoDB. It can provide quite important insights about the expected performance characteristics of MongoDB instance running on this host.

 

For more details and command line arguments please refer to official documentation.

bsondump

bsondump.exe

This is a diagnostic tool which allows to convert BSON (MongoDB primary document storage format) files into human-readable formats, including JSON. It is useful for reading the output files generated by mongodump / mongodump.exe.

 

For more details and command line arguments please refer to official documentation.

7. Running MongoDB

Once installed, MongoDB can be run from command line using mongod / mongod.exe executable. The only required parameter is the location to store database files which could be set using –dbpath command line argument.

On Windows, assuming we are inside installed/unpacked MongoDB distribution location, we can run the server using following steps:

  1. Create a data folder (we need to do that only once): mkdir data
  2. Run MongoDB server: bin\mongod.exe –dbpath data

MongoDB server has started successfully on Windows.

To stop a running MongoDB server instance, it is enough to type CTRL-C.

On Linux, the sequence of commands is exactly the same. Assuming we are inside unpacked MongoDB distribution location, let us do the following:

  1. Create a data folder (we need to do that only once): mkdir data
  2. Run MongoDB server: bin/mongod –dbpath data

MongoDB server has started successfully on Linux.

Similarly, to stop a running MongoDB server instance, it is enough to type CTRL-C.

The easiest way to verify that MongoDB server is up and running is to spawn the MongoDB shell. Let us see that on example by executing: bin/mongo (or bin\mongo.exe on Windows). By default, the shell tries to connect to MongoDB server instance on local machine and database with name test.

MongoDB server using interactive shell and issuing db.stats() command.

Please don’t worry if db.stats() command and its output confuse you. We are going to spend much more time on MongoDB shell in the next part, for now we are only checking that server is really ready to accept connections and to execute commands.

8. What’s next

This part gives us a first look on MongoDB and its features, introducing a couple of new terms specific to it: collections and documents. We also have seen how easy it is to install and run MongoDB on major operating systems, a big advantage for beginners to start using it.

In the next part we are going to work closely with server internals, databases, collections and documents using MongoDB shell and rich set of commands and queries.

Andrey Redko

Andriy is a well-grounded software developer with more then 12 years of practical experience using Java/EE, C#/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button