Skip to content

Instantly share code, notes, and snippets.

@dredshep
Last active February 10, 2021 01:21
Show Gist options
  • Save dredshep/447852cd9cf6e473f6d7b0e089644fd7 to your computer and use it in GitHub Desktop.
Save dredshep/447852cd9cf6e473f6d7b0e089644fd7 to your computer and use it in GitHub Desktop.
Mongodb authentication setup instructions (Ubuntu 20.04)

To enable authentication, simply run: mongo

> use admin
> db.createUser(
  {
    user: "admin",
    pwd: "admin123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

then edit /etc/mongod.conf and find the line that says security. If it is disabled, enable it. If not, add enabled like so:

security:
  authorization: "enabled"

run sudo systemctl restart mongod, then mongo, then test like so:

db test
db.test.find({})

If it fails with lack of authentication, it is working. Then, use admin and authenticate with db.auth("admin", "admin123") and try again, or insert a document db.test.insert({boop: "bloop"}) and it should work.

as the guide linked above says, users can be added with read, write and readWrite permissions to each database.

> use test
> db.createUser(
  {
    user: "myTester",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "test" } ]
  }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment