Skip to main content

MongoDB Installation & Getting Started

MongoDB is one of the most popular open-source document NoSQL databases. In this tutorial we will learn the following things:

  1. Installation of MongoDB
  2. Creating data directory
  3. Starting and Stopping MongoDB
  4. Running Shell Commands

Installation of MongoDB

OSX installation

We can install MongoDB on OSX (Mac OS) in two ways:

  1. Using HomeBrew Installer
  2. Manual Installation

HomeBrew Installation

Reader system should have HomeBrew Installed and brew command available on PATH.

Step 1: Update the HomeBrew Installation Package Repository

$ brew update

Step 2: Install MongoDB

(a) Install Binaries

$ brew install mongodb

(b) Install MongoDB from source with TLS/SSL support

$ brew install mongodb --with-openssl

(c) Install MongoDB edge releases (Still in development, can have bugs)

$ brew install mongodb --devel

Manual Installation

Step 1: Download Mac Binaries from the MongoDB download page.

 MongoDB Download Web Page as of 18 June 2015

You can also download the binaries using the given curl command:

curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.0.4.tgz

You can change the MongoDB version 3.0.4 with relevant one.

Step 2: Unzip (Extract) compressed package in a folder

Unzipped MongoDB App Package


Step 3: Copy the extracted folder at some location where you wanted to keep your MongoDB and rename it to MongoDB (You are free to choose a folder name you like)

 Renamed MongoDB App Package

Step 4: MongoDB binaries executable like mongo etc should be on PATH so we can access that from any directory

Please read this tutorial for setting up the PATH on Mac OSX.

This ends the MongoDB installation.

Data storage, starting and stopping MongoDB

mongod command in bin directory is used to run the MongoDB server. mongod takes several parameters to configure the behavior of the server according to our needs.

Important: MongoDB has two different types of database storage engines.

  1. mmapv1 (default)
  2. wiredTiger

One data directory belongs to only one storage engine. If we force to use a different storage engine (--storageEngine option) for the data directory, the server will refuse to start.

$ mongod

By default data directory is /data/db/ and storage engine is mmapv1. Hence above command is equivalent to:

$ mongod --dbpath "/data/db/" --storageEngine "mmapv1"

We can change the data directory (say /data/mongodb/db/) as well as a storage engine to wiredTiger, using startup command:

$ mongod --dbpath "/data/mongodb/db/" --storageEngine "wiredTiger"



2015-06-19T11:53:05.906+0530 I JOURNAL  \[initandlisten\] journal dir=/data/db/journal

2015-06-19T11:53:05.907+0530 I JOURNAL  \[initandlisten\] recover : no journal files present, no recovery needed

2015-06-19T11:53:05.920+0530 I JOURNAL  \[durability\] Durability thread started

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\] MongoDB starting : pid=31488 port=27017 dbpath=/data/db 64-bit host=GEEKMJ-RT-MBP.local

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\] \*\* WARNING: You are running this process as the root user, which is not recommended.

2015-06-19T11:53:05.920+0530 I JOURNAL  \[journal writer\] Journal writer thread started

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\]

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\]

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\] \*\* WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\] db version v3.0.3

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\] git version: b40106b36eecd1b4407eb1ad1af6bc60593c6105

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\] build info: Darwin bs-osx108-7 12.5.0 Darwin Kernel Version 12.5.0: Sun Sep 29 13:33:47 PDT 2013; root:xnu-2050.48.12~1/RELEASE\_X86\_64 x86\_64 BOOST\_LIB\_VERSION=1\_49

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\] allocator: system

2015-06-19T11:53:05.920+0530 I CONTROL  \[initandlisten\] options: {}

2015-06-19T11:53:06.008+0530 I NETWORK  \[initandlisten\] waiting for connections on port 27017

In simple mongod command run MongoDB server with default values for all configuration options. MongoDB server is running in the foreground (interactive mode).

To stop the MongoDB server, we should press command + c, it will gracefully stop the server.

Running MongoDB server in the background (as daemon or fork) and stopping

Starting MongoDB in background as Daemon (or fork):

$ mongod --fork --logpath "/var/log/mongodb.log"

For graceful shutdown of MongoDB server (use anyone):

$ mongod --shutdown

Note: Above command only works in the Linux environment.

$ mongo

MongoDB shell version: 3.0.3

connecting to: test

> use admin

switched to db admin

> db.shutdownServer()



$ kill <mongodb-process-id>

Note Never use :

$ kill -9 <mongodb-process-id>

It terminates the MongoDB process and you may have an unexpected outcome.

mongod options

We can see all the options and sub-command available for mongod by running the command

$ mongod --help



Options:



General options:

  -h \[ --help \]               show this usage information

  --version                   show version information

  -f \[ --config \] arg         configuration file specifying additional options

  -v \[ --verbose \] \[=arg(=v)\] be more verbose (include multiple times for more

                              verbosity e.g. -vvvvv)

  --quiet                     quieter output

  --port arg                  specify port number - 27017 by default

  --bind\_ip arg               comma separated list of ip addresses to listen on

                              - all local ips by default

  --ipv6                      enable IPv6 support (disabled by default)

  --maxConns arg              max number of simultaneous connections - 1000000

                              by default

  --logpath arg               log file to send write to instead of stdout - has

                              to be a file, not directory

  --syslog                    log to systems syslog facility instead of file

                              or stdout

  --syslogFacility arg        syslog facility used for mongodb syslog message

  --logappend                 append to logpath instead of over-writing

  --logRotate arg             set the log rotation behavior (rename|reopen)

  --timeStampFormat arg       Desired format for timestamps in log messages.

                              One of ctime, iso8601-utc or iso8601-local

  --pidfilepath arg           full path to pidfile (if not set, no pidfile is

                              created)

  --keyFile arg               private key for cluster authentication

  --setParameter arg          Set a configurable parameter

  --httpinterface             enable http interface

  --clusterAuthMode arg       Authentication mode used for cluster

                              authentication. Alternatives are

                              (keyFile|sendKeyFile|sendX509|x509)

  --nounixsocket              disable listening on unix sockets

  --unixSocketPrefix arg      alternative directory for UNIX domain sockets

                              (defaults to /tmp)

  --filePermissions arg       permissions to set on UNIX domain socket file -

                              0700 by default

  --fork                      fork server process

  --auth                      run with security

  --noauth                    run without security

  --jsonp                     allow JSONP access via http (has security

                              implications)

  --rest                      turn on simple rest api

  --slowms arg (=100)         value of slow for profile and console log

  --profile arg               0=off 1=slow, 2=all

  --cpu                       periodically show cpu and iowait utilization

  --sysinfo                   print some diagnostic system information

  --noIndexBuildRetry         dont retry any index builds that were

                              interrupted by shutdown

  --noscripting               disable scripting engine

  --notablescan               do not allow table scans



Replication options:

  --oplogSize arg       size to use (in MB) for replication op log. default is

                        5% of disk space (i.e. large is good)



Master/slave options (old; use replica sets instead):

  --master              master mode

  --slave               slave mode

  --source arg          when slave: specify master as

  --only arg            when slave: specify a single database to replicate

  --slavedelay arg      specify delay (in seconds) to be used when applying

                        master ops to slave

  --autoresync          automatically resync if slave data is stale



Replica set options:

  --replSet arg           arg is \[/\]

  --replIndexPrefetch arg specify index prefetching behavior (if secondary)

                          \[none|\_id\_only|all\]



Sharding options:

  --configsvr           declare this is a config db of a cluster; default port

                        27019; default dir /data/configdb

  --shardsvr            declare this is a shard db of a cluster; default port

                        27018



Storage options:

  --storageEngine arg (=mmapv1) what storage engine to use

  --dbpath arg                  directory for datafiles - defaults to /data/db

  --directoryperdb              each database will be stored in a separate

                                directory

  --noprealloc                  disable data file preallocation - will often

                                hurt performance

  --nssize arg (=16)            .ns file size (in MB) for new databases

  --quota                       limits each database to a certain number of

                                files (8 default)

  --quotaFiles arg              number of files allowed per db, implies --quota

  --smallfiles                  use a smaller default file size

  --syncdelay arg (=60)         seconds between disk syncs (0=never, but not

                                recommended)

  --upgrade                     upgrade db if needed

  --repair                      run repair on all dbs

  --repairpath arg              root directory for repair files - defaults to

                                dbpath

  --journal                     enable journaling

  --nojournal                   disable journaling (journaling is on by default

                                for 64 bit)

  --journalOptions arg          journal diagnostic options

  --journalCommitInterval arg   how often to group/batch commit (ms)



WiredTiger options:

  --wiredTigerCacheSizeGB arg           maximum amount of memory to allocate

                                        for cache; defaults to 1/2 of physical

                                        RAM

  --wiredTigerStatisticsLogDelaySecs arg (=0)

                                        seconds to wait between each write to a

                                        statistics file in the dbpath; 0 means

                                        do not log statistics

  --wiredTigerJournalCompressor arg (=snappy)

                                        use a compressor for log records

                                        \[none|snappy|zlib\]

  --wiredTigerDirectoryForIndexes       Put indexes and data in different

                                        directories

  --wiredTigerCollectionBlockCompressor arg (=snappy)

                                        block compression algorithm for

                                        collection data \[none|snappy|zlib\]

  --wiredTigerIndexPrefixCompression arg (=1)

                                        use prefix compression on row-store

                                        leaf pages

Checking mongoDB version:

$ mongod --version

db version v3.0.3

git version: b40106b36eecd1b4407eb1ad1af6bc60593c6105

MongoDB mongod configurations in the external file

For better management of the MongoDB server, we need an external configuration file for all the options which we provide to mongod command as parameters.

A YAML format configuration file is used.

MongoDB mongod and mongos YAML based configuration same given below:

systemLog:

   verbosity: 0

   quiet: false

   traceAllExceptions: false

   syslogFacility: false

   path: "/var/log/mongodb.log"

   logAppend: false

   #logRotate can be either "rename" or "reopen"

   logRotate: "rename"

   destination: "file"

   timeStampFormat: "iso8601-local"

net:

   bindIp: 127.0.0.1

   port: 27017

   maxIncomingConnections: 65536

storage:

   dbPath: "/data/db"

   engine" "wiredTiger"

setParameter:

   enableLocalhostAuthBypass: false

...

Starting server with configuration: (Say config file is at /conf/mogodb.cnf)

$ mongod --config "conf/mongodb.cnf"

or

$ mongod -f "conf/mongodb.cnf"

Interacting with MongoDB using the mongo shell

We can connect to interactive mongo shell without any hassle if the MongoDB server is running on localhost.

$ mongo

It results in an interactive shell where we can issue commands.

MongoDB shell version: 3.0.3

connecting to: test

>

Basic mongo shell commands

1. Display the current database name

\> db

test

2. Display the list of databases name

\> show dbs

test

3. Switching & Creating a new database and creating a collection

There is no explicit command to create MongoDB database. It will be created when we save a document first time in a collection.

>show dbs

local  0.000GB

>use db geekmjdb

> use geekmjdb

switched to db geekmjdb

> show dbs

local  0.000GB

> db.testCollection.save ({col1:"col1val",col2:"col2val"})

WriteResult({ "nInserted" : 1 })

> db.testCollection.find()

{ "_id" : ObjectId("55845fdff7febc6f192ddcc9"), "col1" : "col1val", "col2" : "col2val" }

> show dbs

geekmjdb  0.000GB

local     0.000GB

We had seen changing database using use geekmjdb doesn't created database. It got created when we added our first record (document) to a collection.

Collection is also created implicitly when the first document is added.

Though there is an explicit method for creating capped collection.

Comments

Popular posts from this blog

Working with request header in Jersey (JAX-RS) guide

In the  previous post , we talked about, how to get parameters and their values from the request query string. In this guide learn how to get request header values in Jersey (JAX-RS) based application. We had tested or used the following tools and technologies in this project: Jersey (v 2.21) Gradle Build System (v 2.9) Spring Boot (v 1.3) Java (v 1.8) Eclipse IDE This is a part of  Jersey (JAX-RS) Restful Web Services Development Guides series. Please read Jersey + Spring Boot getting started guide . Gradle Build File We are using Gradle for our build and dependency management (Using Maven rather than Gradle is a very trivial task). File: build.gradle buildscript { ext { springBootVersion = '1.3.0.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' a

Ajax Cross Domain Resource Access Using jQuery

Some time back in our project we faced a problem while making an Ajax call using jQuery. Chrome Browser console had given some weird error message like below when we try to access one of our web pages: When we try to access the same web page in the Firefox browser, it doesn't give any error in the console but some parsing error occurred. In our case, we were accessing XML as an Ajax request resource. I was curious to check if the non-XML cross-domain resource was successfully loading or not. But finally, I realized that it is not going through. jersey-spring-boot-quick-starter-guide In our Ajax call, requesting domain was not the same as the requested URL domain. $.ajax({ url: "https://10.11.2.171:81/xxxxxx/xxxxxxx.xml" , type : "get" , success: function (response) { alert( "Load was performed." ); }, error : function (xhr, status) {

FastAPI first shot

Setup on my Mac (Macbook Pro 15 inch Retina, Mid 2014) Prerequisite Python 3.6+ (I used 3.7.x. I recently reinstalled OS after cleaning up disk, where stock Python 2.7 was available. I installed Pyenv and then used it to install 3.7.x). I already had a git repo initialized at Github for this project. I checked that out. I use this approach to keep all the source code safe or at a specific place 😀. I set the Python version in .python-version file. I also initialize the virtual environment using pyenv in venv folder. I started the virtual environment. FastAPI specific dependencies setup Now I started with basic pip commands to install dependency for the project. I saved dependencies in requirements.txt  the file. Minimal viable code to spin an API Server FastAPI is as cool as NodeJS or Go Lang (?) to demonstrate the ability to spin an API endpoint up and running in no time. I had the same feeling for the Flask too, which was also super cool. app/main.py: from typing i