Mariano Z Lopez
Mariano Z Lopez
Published in
4 min readAug 18, 2018

--

Getting started with MongoDB & how to enable security system

In this tutorial I’m going to do my best to show you a first steeps with MongoDB and enable the security system.

MongoDB is a free and open-source cross-platform document-oriented database. Classified as a NoSQL database, MongoDB uses JSON-like documents with schemata.

A NoSQL “non SQL, non-relational or not only SQL” database provides a mechanism for storage and retrieval of data that is modeled in means other than the tabular relations used in relational databases. The data structures used by NoSQL databases (e.g. key-value, wide column, graph, or document) are different from those used by default in relational databases, making some operations faster in NoSQL.

A document-oriented database store all information for a given object in a single instance in the database, and every stored object can be different from every other. This makes mapping objects into the database a simple task.

When compared to relational databases, NoSQL databases are more scalable and provide superior performance, and their data model addresses several issues that the relational model is not designed to address:

Large volumes of rapidly changing structured, semi-structured, and unstructured data

Agile sprints, quick schema iteration, and frequent code pushes

Object-oriented programming that is easy to use and flexible

Geographically distributed scale-out architecture instead of expensive, monolithic architecture

What will you learn?

Install MongoDB

Enable MongoDB authentication

Install management tool (Robo3t)

Manage connections in Robo3t

Basic use of Robo3t

Download MongoDB

Let’s start downloading MongoDB binaries from: https://www.mongodb.com/download-center, after download and unzip if you run “bin/mongod (the MongoDB service)” you will receive “NonExistentPath” exception.

Mongod NonExistentPath exception

Mongo needs a path to store data “by default is /data/db (linux), c:\data\db (windows)” but we need create it, so we can make it with “mkdir /data/db –p (linux), mkdir c:\data\db (windows)”.

If we run mongod again we will see “waiting for connections on port 27017” now our MongoDB service is running on port 27017

Mongod running successfully

MongoDB enable auth

By default mongo does not have security system enabled, open other shell/terminal “make sure you are in MongoDB bin directory” and run “mongo executable (in difference to mongod this is a mongo client shell)”, let’s create a root user and a specific database owner, you can read more about in: https://docs.mongodb.com/manual/tutorial/enable-authentication/ & https://docs.mongodb.com/manual/core/security-built-in-roles/

The above code uses the “admin” database and create the user “root” with password “mariano” and roles: userAdminAnyDatabase, dbAdminAnyDatabase and readWriteAnyDatabase. Obviously you can change the username and password is just an example.

userAdminAnyDatabase: Provides the same access to user administration operations as userAdmin on all databases except local and config, the role also indirectly provides superuser access.

dbAdminAnyDatabase: Provides the same read-only privileges as dbAdmin on all databases except local and config. The role also provides the listDatabases action on the cluster as a whole.

readWriteAnyDatabase: Provides the same read-only privileges as readWrite on all databases except local and config. The role also provides the listDatabases action on the cluster as a whole.

dbOwner: The database owner can perform any administrative action on the database. This role combines the privileges granted by the readWrite, dbAdmin and userAdmin roles.

Note: I created the “springboot” user & Books database because I’ll write a spring boot (application framework and inversion of control container for the Java platform) tutorial too.

Now we need to create a configuration file which tells mongo to enable authorization system. Create “/etc/mongod.conf (linux) <install directory>/bin/mongod.conf (windows)” file with the follow code:

At this point we configured mongo’s ip, port and security system, the last step is reboot our mongod service and use the “-f” flag to tell mongo where our configuration file are, stop the current mongod service and then run “<install directory>/bin/mongod -f /etc/mongod.conf”.

Robo3t is a shell-centric cross-platform MongoDB management tool, download: https://robomongo.org/download and run the executable file “robo3t.sh (linux) or robo3t.exe (windows)”

Create connections: File → Connect →Create

Connection tab: assign name, ip & port (default: localhost:27017)

Authentication tab: check perform authentication, assign the authentication database, username & password

Test the connection & click save

Test connection to localhost:27017 as root

Some operations that you can do with robo3t

Right click over connection
Right click over database collections
Right click over collection
Insert document
view documents

Hope this has helped you!, if you want to read more about mongo you can continue with https://docs.mongodb.com/manual/crud/

--

--