Skip to content

Instantly share code, notes, and snippets.

@JBaczuk
Last active April 10, 2019 18:19
Show Gist options
  • Save JBaczuk/40667354a12ade9c02756adc94b1025e to your computer and use it in GitHub Desktop.
Save JBaczuk/40667354a12ade9c02756adc94b1025e to your computer and use it in GitHub Desktop.
How to set up authentication on mongodb

Mongo Authentication

  • Start mongod without auth enabled Make sure this is commented or missing from /etc/mongod.conf
#security:
#  authorization: enabled

The service should be running:

$ sudo service mongod start
  • Create admin user and db users
$ use admin
$ db.createUser({user: 'admin', pwd: 'super secret password', roles: [{role: 'root', db: 'admin'}]})
$ use newDatabase
$ db.createUser({user: 'newDatabaseUser', pwd: 'super secret password', roles: [{role: 'readWrite', db: 'newDatabase'}]})
...
  • Enable Auth Add the following to /etc/mongod.conf:
security:
  authorization: enabled
  • Restart mongod
$ sudo service mongod restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment