For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Authenticate a User with Self-Managed Deployments

To authenticate as a user, you must provide a username, password, and the authentication database associated with that user.

Important

It is not possible to switch between users in the same mongosh session. Authenticating as a different user means the session has the privileges of both authenticated users. To switch between users exit and relaunch mongosh.

Using mongosh, you can:

Start mongosh with the -u <username>, -p, and the --authenticationDatabase <database> command line options:

mongosh --port 27017 --authenticationDatabase \
"admin" -u "myUserAdmin" -p

Enter your password when prompted.

For examples using a MongoDB driver, see the driver documentation.

Earn a Skill Badge

Master "Secure MongoDB Self-Managed: AuthN and AuthZ" for free!

Learn more

Rate this page