How to Connect to MongoDB using mongosh
Table of Contents
1. Environment
If mongosh is not found in your PATH, add the MongoDB bin directory to ~/.bash_profile. In this example, MongoDB is installed in /app/mongodb/7.0.43.
[mongop@recovery ~]$ which mongosh
/usr/bin/which: no mongosh in (/home/mongop/.local/bin:/home/mongop/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin)
[mongop@recovery ~]$
vi ~/.bash_profile
# Add these two lines at the end of the file
export MONGO_HOME=/app/mongodb/7.0.43
export PATH=$MONGO_HOME/bin:$PATH
# Reload the file in your current session (it is read automatically only at login), then verify:
source ~/.bash_profile
[mongop@recovery ~]$ which mongosh
/app/mongodb/7.0.43/bin/mongosh
[mongop@recovery ~]$
[mongop@recovery ~]$ mongosh --version
2.12.0
[mongop@recovery ~]$
2. Connect With Credentials
When authentication is enabled on the MongoDB server, pass the username and the database where that user was created (usually admin). MongoDB will ask for the password.
mongosh --host localhost --port 27017 -u myUser -p --authenticationDatabase admin
[mongop@recovery ~]$ mongosh -u admin -p --authenticationDatabase admin
Enter password: ******
Current Mongosh Log ID: 6ae9dcd902ca1ad80c716cea
Connecting to: mongodb://@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+2.12.0
Using MongoDB: 7.0.43
Using Mongosh: 2.12.0
mongosh 2.14.1 is available for download: https://www.mongodb.com/try/download/shell
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
Enterprise test>
--- OR --
[mongop@recovery ~]$ mongosh --host localhost --port 27017 -u admin -p --authenticationDatabase admin
Enter password: ******
Current Mongosh Log ID: 6ae9dcff04ce3c397ca3af62
Connecting to: mongodb://@localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+2.12.0
Using MongoDB: 7.0.43
Using Mongosh: 2.12.0
mongosh 2.14.1 is available for download: https://www.mongodb.com/try/download/shell
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
Enterprise test>
You can also use a connection string:
mongosh "mongodb://myUser:myPassword@localhost:27017/?authSource=admin"
[mongop@recovery ~]$ mongosh "mongodb://admin:oracle@localhost:27017/?authSource=admin"
Current Mongosh Log ID: 6ae9dd4ece122f32228a146d
Connecting to: mongodb://@localhost:27017/?authSource=admin&directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.12.0
Using MongoDB: 7.0.43
Using Mongosh: 2.12.0
mongosh 2.14.1 is available for download: https://www.mongodb.com/try/download/shell
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
Enterprise test>If the password contains special characters such as @ or :, they must be URL-encoded (for example, @ becomes %40).
Passing the password in the command itself is not recommended, because it can be seen in shell history:
mongosh -u myUser -p 'myPassword' --authenticationDatabase admin
Another way is to connect first and log in from inside the shell:
[mongop@recovery ~]$ mongosh
Current Mongosh Log ID: 6ae9de0f0e3a304d18204004
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.12.0
Using MongoDB: 7.0.43
Using Mongosh: 2.12.0
mongosh 2.14.1 is available for download: https://www.mongodb.com/try/download/shell
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
Enterprise test> use admin
switched to db admin
Enterprise admin> db.auth("admin", passwordPrompt())
Enter password
******{ ok: 1 }
Enterprise admin>
It returns { ok: 1 } when the login works.
3. Verify the Connection
Enterprise admin> db.runCommand({ connectionStatus: 1 })
{
authInfo: {
authenticatedUsers: [ { user: 'admin', db: 'admin' } ],
authenticatedUserRoles: [ { role: 'root', db: 'admin' } ]
},
ok: 1
}
Enterprise admin> show dbs
admin 132.00 KiB
config 108.00 KiB
local 72.00 KiB
Enterprise admin>
In the connectionStatus output, authenticatedUsers shows who you are logged in as. It is empty if you connected without credentials.
4. Connect Without Credentials
Simply run mongosh without a username or password.
mongosh
# or with host and port
mongosh --host localhost --port 27017
What happens next depends on the server:
- Authentication disabled: you can run every command normally.
- Authentication enabled: the connection succeeds, but commands such as
show dbsfail withUnauthorizeduntil you log in using Section 2.
Disabling authentication (test environments only)
In /etc/mongod.conf, remove or disable the authorization setting, then restart MongoDB. Never do this on a production server, because anyone who can reach the server gets full access.
# /app/mongodb/config/mongod.conf
security:
authorization: disabled
# Restart the service
systemctl restart mongod
# Connect
mongosh