MongoDB Default Username and Password

In this tutorial, we’ll discuss the default username and password and how to find a username and password in MongoDB.
Default Username and Password in MongoDB
By default, MongoDB does not have access control enabled, so there is no default user or password. Use the command line option --auth
or the security.authorization
configuration file setting to enable access control.
The following are the steps that you can follow. First, open a terminal and start MongoDB Daemon.
mongod --port 27017 --dbpath /data/db
Enter the mongo shell in a new terminal tab.
mongo --port 27017
Create an admin user.
use admin
db.createUser(
{
user: "user123",
pwd: "pass123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db: "admin" }
]
}
)
Enter quit()
exit. Press and hold ctrl-c
to kill the process on the mongo daemon page, and restart MongoDB with the --auth
option enabled.
$ mongod --auth --port 27017 --dbpath /data/db
2019-02-23T16:18:38.539+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
...
...
2019-02-23T16:18:38.553+0800 I CONTROL [initandlisten] options: { net: { port: 27017 }, security: { authorization: "enabled" }, storage: { dbPath: "/data/db" } }
Log in to the mongo shell with the user-created earlier.
$ mongo --port 27017 -u "user123" -p "pass123" --authenticationDatabase "admin"
MongoDB shell version v4.0.2
connecting to: mongodb : //127.0.0.1:27017/
MongoDB server version: 4.0.2
...
Select Username/Password (MONGODB-CR/SCRAM-SHA-1)
to connect to your MongoDB deployment.
- Check
Username/Password (MONGODB-CR/SCRAM-SHA-1)
orUsername/Password (SCRAM-SHA-256)
from Agent Auth Mechanism. Cloud Manager automatically generates the Agents’ usernames and passwords. - Click Save.
Syntax:
db.getUser(username)
Parameters for the query are Name
, Description
, and Type: username
. You must have the viewUser
action on the other user’s database to view another user’s information.
Below is the query to change a MongoDB user’s password.
Query:
db.changeUserPassword("user", "12345");
Authorize MongoDB
- Start MongoDB without authentication.
- After that, connect to the server using the mongo shell.
- Create the user administrator.
- Enable authentication in the MongoDB configuration file.
- Now connect and authenticate as the user administrator.
- Finally, create additional users as needed.
Find Your Password in MongoDB
You can specify the password directly as you would with earlier versions of the mongo shell. A user document contains the username and password, optionally, the authentication mechanism, and a digest password flag.
The user’s name with access privileges for the given database and the user’s password.