A password containing special characters, especially the dollar sign, has to be put in single quotes to protect them from the command shell:

$ mongo admin -u uname -p 'password'
Answer from ronasta on Stack Overflow
🌐
MongoDB
mongodb.com › docs › mongodb-shell
Welcome to MongoDB Shell (mongosh) - mongosh - MongoDB Docs
Manage replication or sharding conveniently in your shell. Check server status with a variety of Server Status Methods. ... Create or update roles, define and update privileges, or drop roles using Role Management Methods. Create and update users, authenticate users, and manage user roles with User Management Methods. ... Write scripts to run with the MongoDB Shell that perform CRUD or administrative operations in MongoDB.
🌐
MongoDB
mongodb.com › docs home › reference › mongosh methods › user management
db.auth() (mongosh method) - Database Manual - MongoDB Docs
Authenticate users to a database using `db.auth()` in the shell, with options for password prompts and authentication mechanisms.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-the-mongodb-shell
How To Use the MongoDB Shell | DigitalOcean
July 29, 2021 - Note: Instead of typing the exit command, an alternative way to close the shell is to press CTRL + C instead. Now try reconnecting the MongoDB shell to the database server, but this time provide a username and password to properly authenticate into your MongoDB instance.
🌐
MongoDB
mongodb.com › docs home › tools › mongodb shell
Connect to a Deployment - mongosh - MongoDB Docs
The $external argument must be placed in single quotes, not double quotes, to prevent the shell from interpreting $external as a variable. Set --authenticationMechanism to PLAIN. When you use one-time passwords with LDAP authentication, adding the connection string options maxPoolSize=1&srvMaxHosts=1 to your connection string is recommended to reduce the potential for connection failures. Include the --host and --port of the MongoDB deployment, along with any other options relevant to your deployment.
🌐
MongoDB
mongodb.com › docs home › tools › sql interface › connect
Connect from the MongoDB Shell - Atlas - MongoDB Docs
Connect to a federated database instance using MongoDB Shell, with steps for installation, authentication, and query syntax options.
🌐
TutorialsTeacher
tutorialsteacher.com › mongodb › mongodb-shell-commands
MongoDB Shell Commands
C:>mongosh --help $ mongosh [options] [db address] [file names (ending in .js or .mongodb)] Options: -h, --help Show this usage information -f, --file [arg] Load the specified mongosh script --host [arg] Server to connect to --port [arg] Port to connect to --version Show version information --verbose Increase the verbosity of the output of the shell --quiet Silence output from the shell during the connection process --shell Run the shell after executing files --nodb Don't connect to mongod on startup - no 'db address' [arg] expected --norc Will not run the '.mongoshrc.js' file on start up -
Find elsewhere
🌐
MongoDB
mongodb.com › docs home › development › atlas cluster connection
Connect to a Cluster via mongosh - Atlas - MongoDB Docs
This string includes the name of the MongoDB user that can authenticate with the cluster. Copy this string. To connect as a different MongoDB user, change the --username option. Paste the mongosh command and connection string into a terminal. Run the command. The shell prompts you for the password.
🌐
MongoDB
mongodb.com › products › tools › shell
MongoDB Shell: Manage and interact with your database | MongoDB
Download MongoDB Shell (mongosh) as a standalone tool to run in your terminal, or access it directly within Compass, MongoDB's official GUI.
🌐
BMC Software
bmc.com › blogs › mongo-shell-basic-commands
MongoDB Shell Basic Commands – BMC Software | Blogs
Both of the above commands only work if your MongoDB server is running on the localhost. If you want to connect to a remote server, use the `–host` option with the mongo command, as shown below. ... Now it’s time to work with the Mongo shell.
🌐
Medium
medium.com › @yasiru.13 › mongodb-setting-up-an-admin-and-login-as-admin-856ea6856faf
MongoDB Setting up an admin and login as admin | by Yasiru Nilan | Medium
February 17, 2017 - Then exit the mongoDB shell using, >exit; Again log in to mongo shell, use following commands to log in as the admin. >use admin >db.auth('admin','password'); Mongodb · Nodejs · 13 followers · ·24 following · Help · Status · About · Careers ...
Top answer
1 of 1
1

Each user is associated with an authentication database. So if your user is declared in admin database (and authorization activated, what MUST be done in all environment server), it's mandatory to add this param in your login command.

See authentication, and more generally security for more details and explanations.

EDIT In case of using Connection String URI Format, you can skip authentication database param, in this case 'admin' will be used by default for authentication database, and 'test' by default as target database.

Example : (your user is created in 'admin' database.)

Here's different behaviors :

authentication against 'admin', database targeted is 'test'

$ mongo --host mongodb://user:pwd@myhost:27000
$ mongo --host mongodb://user:pwd@myhost:27000/test?authSource=admin
$ mongo --host mongodb://user:pwd@myhost:27000/?authSource=admin

authentication against 'admin', database targeted is 'admin'

$ mongo --host mongodb://user:pwd@myhost:27000/admin
$ mongo --host mongodb://user:pwd@myhost:27000/admin?authSource=admin

authentication against 'test', database targeted is 'test' (will not succeed)

$ mongo --host mongodb://user:pwd@myhost:27000/test
$ mongo --host mongodb://user:pwd@myhost:27000/test?authSource=test

EDIT 2 As you really need to use mongo -h -u -p notation, create your user in your 'test' database, which will be used by default in this case :

this will authenticate against 'test' database, and target 'test' database

mongo -h 199.99.99.99:27000 -u user -p pwd
🌐
ScaleGrid
help.scalegrid.io › docs › mongodb-command-line-syntax
Connect Using Mongo Shell - ScaleGrid Documentation
Learn how to connect to MongoDB® from the command line syntax and MongoDB® web shell.
Top answer
1 of 16
142

Wow so many complicated/confusing answers here.

This is as of v3.4.

Short answer.

  1. Start MongoDB without access control (/data/db or where your db is).

    mongod --dbpath /data/db
    
  2. Connect to the instance.

    mongo
    
  3. Create the user.

    use some_db
    db.createUser(
      {
        user: "myNormalUser",
        pwd: "xyz123",
        roles: [ { role: "readWrite", db: "some_db" },
                 { role: "read", db: "some_other_db" } ]
      }
    )
    
  4. Stop the MongoDB instance and start it again with access control.

    mongod --auth --dbpath /data/db
    
  5. Connect and authenticate as the user.

    use some_db
    db.auth("myNormalUser", "xyz123")
    db.foo.insert({x:1})
    use some_other_db
    db.foo.find({})
    

Long answer: Read this if you want to properly understand.

It's really simple. I'll dumb the following down https://docs.mongodb.com/manual/tutorial/enable-authentication/

If you want to learn more about what the roles actually do read more here: https://docs.mongodb.com/manual/reference/built-in-roles/

  1. Start MongoDB without access control.

    mongod --dbpath /data/db
    
  2. Connect to the instance.

    mongo
    
  3. Create the user administrator. The following creates a user administrator in the admin authentication database. The user is a dbOwner over the some_db database and NOT over the admin database, this is important to remember.

    use admin
    db.createUser(
      {
        user: "myDbOwner",
        pwd: "abc123",
        roles: [ { role: "dbOwner", db: "some_db" } ]
      }
    )
    

Or if you want to create an admin which is admin over any database:

   use admin
   db.createUser(
     {
       user: "myUserAdmin",
       pwd: "abc123",
       roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
     }
   )
  1. Stop the MongoDB instance and start it again with access control.

    mongod --auth --dbpath /data/db
    
  2. Connect and authenticate as the user administrator towards the admin authentication database, NOT towards the some_db authentication database. The user administrator was created in the admin authentication database, the user does not exist in the some_db authentication database.

    use admin
    db.auth("myDbOwner", "abc123")
    

You are now authenticated as a dbOwner over the some_db database. So now if you wish to read/write/do stuff directly towards the some_db database you can change to it.

use some_db
//...do stuff like db.foo.insert({x:1})
// remember that the user administrator had dbOwner rights so the user may write/read, if you create a user with userAdmin they will not be able to read/write for example.

More on roles: https://docs.mongodb.com/manual/reference/built-in-roles/

If you wish to make additional users which aren't user administrators and which are just normal users continue reading below.

  1. Create a normal user. This user will be created in the some_db authentication database down below.

    use some_db
    db.createUser(
      {
        user: "myNormalUser",
        pwd: "xyz123",
        roles: [ { role: "readWrite", db: "some_db" },
                 { role: "read", db: "some_other_db" } ]
      }
    )
    
  2. Exit the mongo shell, re-connect, authenticate as the user.

    use some_db
    db.auth("myNormalUser", "xyz123")
    db.foo.insert({x:1})
    use some_other_db
    db.foo.find({})
    

Last but not least due to users not reading the commands I posted correctly regarding the --auth flag, you can set this value in the configuration file for mongoDB if you do not wish to set it as a flag.

2 of 16
140

You need to start mongod with the --auth option after setting up the user.

From the MongoDB Site:

Run the database (mongod process) with the --auth option to enable security. You must either have added a user to the admin db before starting the server with --auth, or add the first user from the localhost interface.

MongoDB Authentication

🌐
MongoDB
mongodb.com › docs home › self-managed deployments › security › authentication › users
Authenticate a User with Self-Managed Deployments - Database Manual - MongoDB Docs
Authenticate users in self-managed MongoDB deployments using `mongosh` with username, password, and authentication database.
🌐
MongoDB
mongodb.com › try › download › shell
MongoDB Shell Download | MongoDB
Connect to a cluter to navigate MongoDB databases and collections, prototype CRUD operations, access the MongoDB Shell, export to language and more with this integration.
🌐
GeeksforGeeks
geeksforgeeks.org › mongodb › how-to-use-the-mongodb-shell
How To Use the MongoDB Shell - GeeksforGeeks
September 24, 2025 - Commands like use and show allow us to switch databases and display information about databases and collections. exit and quit are used to exit the shell. The Mongo and connect commands are used to create new connections to MongoDB servers.