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.
🌐
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.
🌐
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 › 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.
🌐
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.
Find elsewhere
🌐
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 ...
🌐
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 -
🌐
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.
🌐
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 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
🌐
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.
🌐
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 Manual
mongoing.com › docs › tutorial › enable-authentication.html
Enable Auth — MongoDB Manual 3.4
Disconnect the mongo shell. ... Re-start the mongod instance with the --auth command line option or, if using a configuration file, the security.authorization setting. ... Clients that connect to this instance must now authenticate themselves as a MongoDB user.
🌐
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.