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" } ]
}
)