Overview

Unlike other databases, MongoDB can be started without any authentication, meaning that everyone CAN connect from outside. In order to limit that and enable authentication, we should basically do 2 things:

Create all powerful user

Enable Authentication

Create all powerful User

The user creation is rather simple in Mongo as you can see below:

This command will create you a user called adminDBA in database: admin (important) and grant him all rights.

Enable Authentication

Authentication in Mongo is disabled by default. Meaning everyone can connect to the database who has access to the server. Furthermore to enable authentication it depends on the version.

To enable authentication ⇐ 2.6: you should start the Mongo in special way, as follows:

For Version 2.6 or less

Or you can enable it in the configuration file:

For versions 2.8 or higher

To enable authentcation > 2.6 You can edit the /etc/mongod.conf file as follows:

P.S. Don't forget to have username and password beforehand :)

Connection

In order to connect, we have to enter the username and password as follows: