left-icon

MongoDB 3 Succinctly®
by Zoran Maksimovic

Previous
Chapter

of
A
A
A

CHAPTER 2

MongoDB Installation

MongoDB Installation


MongoDB is a cross-platform database, and the current version supports a variety of operating systems—pretty much all main Linux distributions, Microsoft Windows, and OSX.

As the idea of this book was to use Microsoft.NET and MongoDB, we will only briefly touch on Linux installation as a reference, and we will mainly concentrate on running and configuring MongoDB on Microsoft Windows. While all concepts apply equally on every platform, it’s not in the scope of this book to get into particular platform details.

For Mac and Windows platforms, you can download MongoDB and install it directly from the
project website.

In this book, we will be covering the MongoDB Community Server, which is a free version of MongoDB.

Installation on Windows (single node)

Examples in this book were tested in Microsoft Windows 10; the exact version downloaded and used is version 3.4.1 for “Windows Server 2008 R2 64-bit and later, with SSL support x64.”

MongoDB can run as a standalone application or as a Windows service. For learning purposes, it is interesting to see MongoDB running as the standalone application (console), as it outputs some debug information that can be useful. Otherwise, on production systems, it’s highly recommended to run it as a Windows service.

MongoDB installation

  1. Install the MongoDB by double-clicking on the downloaded file. Follow the installer instructions and choose the custom installation.
  1. Install MongoDB in the c:\mongodb folder. (You can choose any folder you like; for this book’s purpose, I’ve chosen this path.)
  2. Choose all the features available.
  1. We need to manually create the directory where the database data will reside. The default location where MongoDB will place the files is c:\data\db, so, let’s just keep these default settings and create the folder by running md c:\data\db on the command line. The location of this folder can be changed by using the --dbpath option when starting the database.
  2. The same thing applies to the application logs. The default place that MongoDB will be looking at is c:\data\log. So, let’s run the md c:\data\log on the command line. This option can be controlled by using the --logpath option. Let’s use the default and create this folder.

Running MongoDB as a standalone application

Starting the database is as simple as running the following command.

Code Listing 3: Starting MongoDB

C:\mongodb\bin>mongod

After running this command (which stands for “mongo daemon”) for the first time, you might be asked to open the firewall. Accept this option.

The output will be similar to the following in Figure 7, from which we may learn quite a few things (as highlighted).

  • MongoDB operates by default on port: 27017.
  • The database engine used by default is wiredTiger.
  • Logs are enabled.
  • Last message: “waiting for connections on port 27017” means that the database is ready to be used.

Starting MongoDB.

Figure 7: Starting MongoDB.

 

Installing MongoDB as a Windows service

By running the following command, where we specify explicitly where to place the logs, we will install MongoDB as a Windows Service. Make sure to run the command prompt with administrator rights.

In order to install MongoDB on Windows as a service, keeping the previously used settings is as simple as adding the --install parameter and the location of the log specified by the value of --logpath.

Code Listing 4: Installing MongoDB as a Windows service

C:\mongodb\bin>mongod --logpath c:\data\log\log.log –-install

If the installation is successful, you will see MongoDB in the Windows Services panel, as shown in Figure 8.

For more information about installing MongoDB on Windows with the full option list, see this tutorial.

MongoDB service after complete installation.

Figure 8: MongoDB service after complete installation.

Single node installation on Linux (Ubuntu)

Here we will demonstrate how to install MongoDB on Ubuntu (version 16.04 at the time of writing) by using the popular apt package management tool.

Using the terminal window, let’s follow the following steps:

  1. Import the public key used by the apt package manager.

Code Listing 5: Importing the public key

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

  1. Create a list file for MongoDB.

Code Listing 6: List file creation

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. Reload local package database.

Code Listing 7: Reload local package database

sudo apt-get update

  1. Install the MongoDB package.

At this step, after preparing the apt package manager, we can install the latest version of MongoDB by running:

Code Listing 8: Install MongoDB package

sudo apt-get install -y mongodb-org

Or, alternatively, install a specific version of MongoDB (in our case, version 3.4.10).

Code Listing 9: Installing a specific version of MongoDB

sudo apt-get install -y mongodb-org=3.4.10 mongodb-org-server=3.4.10 mongodb-org-shell=3.4.10 mongodb-org-mongos=3.4.10 mongodb-org-tools=3.4.10

  1. Make sure the file /lib/systemd/system/mongod.service is present and that it contains the following content:

Code Listing 10: MongoDB configuration file content

[Unit]

Description=High-performance, schema-free document-oriented database

After=network.target

Documentation=https://docs.mongodb.org/manual

[Service]

User=mongodb

Group=mongodb

ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]

WantedBy=multi-user.target

  1. At this point, we can start MongoDB and test that it works correctly.

Code Listing 11: Starting the MongoDB

sudo service mongod start

Let’s check to see if it’s possible to connect to the database:

Code Listing 12: Starting the client via terminal

mongo

This command should successfully connect to the “test” database.

What comes with the MongoDB installation?

We have just seen how to install MongoDB, together with the database itself. There are also a few extra components that are part of the installation package. In this chapter, we are going to go through the list and explain briefly what each of them does.

MongoDB installed utilities.

Figure 9: MongoDB installed utilities.

Don’t worry if you’re not familiar with some of the concepts (such as GridFS) that we haven’t touched yet—we’ll be looking into those in the next chapters.

Table 2: MongoDB installation files

Component

Category

Description

mongod  

Core Process

The core database process. It is responsible for running (hosting) the mongodb instance on a single machine. This process is responsible for handling connections, requests, data access, and any other operations typical of a database.

mongos

Core Process

Short for “MongoDB Shard,” mongos is a service that sits between the application and the MongoDBs. It communicates with the configuration server to determine where the requested data lives (on which shard). It then fetches, aggregates, and returns the data in JSON form.

mongo

Core Process

An interactive JavaScript shell interface to MongoDB that provides an interface for system administrators and allows any kind of operation against the MongoDB instance.

mongodump

Binary Import/Export

A utility for creating a binary export of the contents of a database. 

mongorestore 

Binary Import/Export

Writes data from a binary database dump created by mongodump to a MongoDB instance.

bsondump

Binary Import/Export

bsondump is a diagnostic tool for inspecting BSON files, and it converts BSON files into human-readable formats, including JSON, which is very useful for reading the output files generated by mongodump.

mongooplog

Binary Import/Export

A simple tool that polls operations from the replication oplog of a remote server, and applies them to the local server.

mongoimport

Data Import/Export

Imports content from a JSON, CSV, or TSV export created by mongoexport.

mongoexport

Data Import/Export

Produces a JSON or CSV export of data stored in a MongoDB instance.

mongostat

Diagnostics

Provides a quick overview of the status of a currently running mongod or mongos instance. It allows the monitoring of the MongoDB. It provides information such as the server status, database statistics, and collection statistics.

mongotop

Diagnostics

Another monitoring tool that provides the method to track the amount of time a MongoDB instance spends reading and writing data. It provides statistics per collection.

mongoreplay

Diagnostics

A traffic capture and replay tool for MongoDB that you can use to inspect and record commands sent to a MongoDB instance, and then replay those commands back onto another host at a later time.

mongoperf

Diagnostics

A utility that checks disk I/O performance independently of MongoDB.

mongofiles

GridFS

Manipulates files stored in the MongoDB instance in GridFSobjects from the command line. It is particularly useful, as it provides an interface between objects stored in your file system and GridFS.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.