Authentication
Authentication: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 72 of 100
Authentication
Foundations & CRUD ✓ → Queries & Schema ✓ → Aggregation & Scale ✓ → Atlas & Projects
Atlas & Projects · 4 — Build · ~10 min · MongoDB — Atlas & Security
What is this?
Authentication proves who you are — usually username/password (SCRAM) against MongoDB users stored in the admin database, or Atlas user credentials / LDAP / x509 in enterprise setups.
Why should you care?
An open mongod on the internet without auth will be ransomed. Every environment beyond a locked laptop needs authentication.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
use admin
db.createUser({
user: "appUser",
pwd: "changeMe-strong",
roles: [ { role: "readWrite", db: "NoSQLVerse" } ]
})
// Connect:
// mongosh "mongodb://appUser:changeMe-strong@localhost:27017/NoSQLVerse?authSource=admin"
db.runCommand({ connectionStatus: 1 })
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- createUser defines appUser with readWrite on your database only.
- authSource=admin tells the client where credentials live.
- connectionStatus shows the authenticated user.
- Restart mongod with --auth for enforcement on self-managed installs.
Practice next
- Create a least-privilege user for your database.
- Connect with that user and try writing.
- Try dropping admin — should fail.
- Create a read-only reporter user.
- Rotate the appUser password and update the URI.
Remember
Authentication verifies identity. Create per-app users with limited roles. Always enable auth outside local toys.
Separate app vs admin creds
Node API uses appUser; humans use SSO into Atlas with MFA.
Outcome: Leaked app password cannot drop the whole cluster as easily.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!