Skip to content

Instantly share code, notes, and snippets.

@xeptore
Created March 27, 2019 02:34
Show Gist options
  • Save xeptore/127ee5927d29750fd610fbd4e87f7854 to your computer and use it in GitHub Desktop.
Save xeptore/127ee5927d29750fd610fbd4e87f7854 to your computer and use it in GitHub Desktop.
enabling mongodb authentication

Enabling Authentication on MongoDB

Securing

  1. Execute following queries in Mongo Shell:
use admin;
db.createUser({
  user: 'some username for admin',
  pwd: 'a super secure password for administraton, e.g. 123',
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
});
  1. Add (un-comment) following lines in /etc/mongod.conf:
security:
    authorization: "enabled"
  1. Create users for databases in Mongo Shell
db.createUser({
  user: 'some username',
  pwd: 'some password for user',
  roles: [ { role: "readWrite", db: "test" } ]
});

Logging In

To log into test database in Mongo Shell, run following commands after starting mongod:

use test;
db.auth('some username', 'some password');

returning 1 from mongod means youre successfully logged in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment