Set Up MongoDB with Security

January 18, 2015

First run mongodb instance:


In a secondary terminal, connect ad Add global admin:

use admin
      user: "myAdmin",
      pwd: "myPassword",
      roles: [ "root" ]

Run again the process with auth privileges:

mongod --auth

Connect with secondary terminal using your credentials:

mongo admin -u 'myAdmin' -p 'myPassword'

Create a new user for a specific database:

use myDatabase
      user: "myDatabaseAdmin",
      pwd: "myPassword",
      roles: [ {role: "readWrite", db: "myDatabase" }]

Shutdown the process. It’s moment to set the auth configuration in mongoDB configuration file instead to lift the process with flags.

Open the file /etc/mongod.conf and search the lines:

port = 23794 // Put a random free port. You can use portFinder (npm install -g port-finder)
bind_ip = // Use your IP to acces remotely
auth = true // enable security auth mode

Restart MongoDB process:

service mongod restart

Achievement unlocked! Security access activated!