MongoDB (part 1): installation on Windows, macOS, Raspberry Pi

MongoDB is an Open Source oriented document database. Unlike a relational database classic in which the data is organized according to a schema (tables, fields, data types) and put into relation with each other, it stores the data in a MongoDB database in a document in the JSON style, IE in the form key:value.

Why MongoDB ?

The main advantage of the NoSQL databases is the absence of schema . There is no need to define the tables, fields of data (or data type) and the relationships between the tables (fields). A collection (the equivalent of a table) can contain different documents. Each document can have a number of field, a size, a different content from one document to the other. This architecture for many other advantages:

  • The structure of a document can change , the structure is not fixed. Just to update a document to add / remove information. It is much more flexible than to modify fields in a relational database.
  • There not links between documents.
  • MongoDB is able to complex searches as simply and effectively as a SQL query.
  • MongoDB is designed to support the scalability . MongoDB is suitable for very large it projects. To learn more about performance, read this extensive article published on Ippon Technology blog .

How to install MongoDB on Windows

Windows version tested : 10 (32-bit)

before you download the version that matches your system, run this command

C:\>wmic os get osarchitecture
OSArchitecture
64-bit
C:\>

The 32 bit version of MongoDB support only the databases less than 2 GB. That’s enough to make tests or small personal developments.

Download the latest version of MongoDB from this address https://www.mongodb.org/downloads since the Community Server tab.

Launch the installer

mongodb windows installation

You can install everything (Complete)

 mongodb windows installation complete

Or only the desired components (Custom). In view of the necessary size (322MB), so everything install. Nothing prevents you to choose another directory for installation. Since the Custom option, click on Browse… to indicate (and create if needed) a different installation folder.

Note. The subdirectory bin is created automatically at installation.

mongodb windows installation partielle

Installation starts
mongodb windows installation progression

Installation is complete, exit the installer by clicking Finish.

mongodb windows installation terminee

The files are installed in a directory MongoDB in programs (Program Files). A subdirectory is created for each version. [AllprogramsMongotofindforexampleatthefollowingpath:

 C:Program FilesMongoDBServer3.2bin

now to add this path in environment variables. Open the menu started and enter variable. In the list of results, open “Edit the system environment variables”.

mongodb windows variable environnement path data db

In the dialog box that opens, add a semicolon followed the path to the binary. Validate by pressing OK.

mongodb windows variable environnement path data db

Now, open the computer and create a directory data to the root then a db inside directory. You can also run this command in the command prompt. We want to create a directory for the logs.

md \data\db
md \data\log

Start MongoDB

Run the following command (don’t forget to fix the path that matches your configuration) to start the MongoDB server. Need to set the path in quotes if it contains spaces. It must specify the path to the database (on the first launch).

Attention. For 32-bit Windows, it is necessary to add an extra option – storageEngine = mmapv1

"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe" --dbpath C:\data\db --storageEngine=mmapv1

At the first launch of MongoDB, you will need to allow access from Windows Firewall.

mongodb windows start mongod firewall autoriser acces

The database is started and ready to receive connections from clients on the port 27017 by default.

mongodb windows start mongod --dbpath --storeEngine=mmapv1

Create a configuration mongod.cfg file

One can also go even further and create a configuration file mongod.cfg in Bin .

Open a text editor and paste this basic configuration (Customize the paths and the storageEnegine which corresponds to your configuration).

Save UTF-8 to format. If you don’t have administrator rights, save the file in documents and then move it manually in the MongoDB Bin folder.

Once everything is ready, run mongod like this.

"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe"

To know in detail all the options available, go to this page https://docs.mongodb.com/v3.0/reference/configuration-options/

Run MongoDB as a service

Now, we would like to although MongoDB runs automatically when Windows starts. ” It is very easy, just install the service.

Open a command prompt in Administrator

 mongodb invite commande administrateur

Create the service using sc.exe

The first method is to use sc.exe. Run the command

sc.exe create MongoDB binPath= "C:\mongodb\bin\mongod.exe --service --config=\"C:\mongodb\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

[sc] message createService success (s) is returned if the service was successfully added. ”

mongodb-windows-sc-exe-create

Remove the service with sc.exe

The following command to delete the service

sc.exe delete <service_name>

which gives for example in our case

 sc.exe delete MongoDB

Create the service with the install option

The commandr mongod accepts the option —install for installing MongoDB as a Windows service or –remove to uninstall the service as well as

–install: starts installing MongoDB as a Windows service

–remove: removes the Windows service

–serviceName : Windows (only of alphanumeric characters without space) service name

–serviceDisplayName <nom-affiché>: (optional) name displayed in the list of services. Put in quotation marks if the name contains spaces.

–serviceDescription : (optional) brief description of the service. Put in quotes if there are spaces.

–logpath: (required for install) path to the journal

–journal (recommended): activates the log

What gives for example

C:\MongoDB\bin\mongod.exe --install --serviceName "MongoDB" --serviceDisplayName "MongoDB" --serviceDescription "MongoDB sur le port 27017" --journal --logpath c:\data\log\mongo.log

2016-11-03_12h03_20

Remove the MongoDB service

To remove the service just run this command.

Started menu, find and launch Services Manager.

 mongodb windows rechercher services

The MongoDB service (according to the assigned name) is present in the list but it does not start automatically after installation.

 mongodb service windows

For this made right click then start

mongodb demarrer manuellement service

Now it is running

mongodb service demarre

Now you can start with the command client mongo .

mongodb client connecte

Install MongoDB on macOS

Version macOS tested : 10.11.6 (El Captain).

MongoDB is installed on macOS using Brew. BREW is a package manager for Mac (project official page here).

node-red mongodb brew

Open a Terminal and paste this command

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Answer the questions to complete the installation of Brew on your Mac.

Once installed, run this command to install MongoDB.

 brew install mongodb

It’s done.

Now you need to create a directory for data

mkdir -p /data/db

Go to the finder and also give rights to reading and writing (for all users) to the file data. Cmd + I then identify yourself by pressing the lock and finally to change rights.

Now, start with the option –dbpath (at the first start)

mongod --dbpath /data/db

How to install MongoDB on Raspberry PI

On Raspberry Pi, MongoDB is still limited to the 32-bit version, and so as a result to a maximum 2GB database. It will be therefore reserved tests, learning or small applications

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mongodb-server

Binary files located in the directory /usr/bin/.  The data files are in the folder /var/lib/mongodb/

The MongoDB service starts as soon as the end of the installation. To check if it is started, run the command

sudo service --status-all

A more brackets means that the service is well underway.

raspbian sudo --service-all mongodb runing

Or sudo service mongodb status

pi@raspberrypi:~ $ sudo service mongodb status
● mongodb.service - An object/document-oriented database
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled)
   Active: inactive (dead) since jeu. 2016-11-03 14:59:52 CET; 2min 10s ago
     Docs: man:mongod(1)
  Process: 458 ExecStart=/usr/bin/mongod --config /etc/mongodb.conf (code=exited, status=0/SUCCESS)
 Main PID: 458 (code=exited, status=0/SUCCESS)

nov. 03 14:50:19 raspberrypi systemd[1]: Started An object/document-oriented database.
nov. 03 14:50:22 raspberrypi mongod[458]: all output going to: /var/log/mongodb/mongo...og
nov. 03 14:59:52 raspberrypi systemd[1]: Stopping An object/document-oriented database...
nov. 03 14:59:52 raspberrypi systemd[1]: Stopped An object/document-oriented database.
Hint: Some lines were ellipsized, use -l to show in full.

If it is not started, run this command

sudo service start mongodb

To interrupt service

 sudo service mongodb stop

You can now access the database from the client mongo.

Discover the MongoDB environnement

MongoDB consists of a server (mongod) and a client (mongo) that we will discover more in detail in the second part of this series of articles. Several utilities are also available (monitoring, import/export, performance…).

program (command) function
mongod the MongoDB database server
mongo Client. Allows to run all Mongo since a Shell commands
mongostat, mongotop monitoring utilities
mongodump , mongorestore , mongoexport , mongoimport utility to import / export
bsondump , mongofiles , mongooplog , mongoperf utilities

Sources

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

We will be happy to hear your thoughts

Leave a Reply

DIY Projects
%d bloggers like this: