Tool Logo
MongoDB

How to Create a User in MongoDB (Superuser and Database User)

How to Create a User in MongoDB (Superuser and Database User)
3 min read
#MongoDB

To create a MongoDB user, open mongosh, switch to the right database and call db.createUser(). For a superuser with every privilege, use the admin database and the root role:

use admin
db.createUser({ user: "superuser", pwd: passwordPrompt(), roles: ["root"] })

For a user limited to one database, give it a role such as readWrite on that database. The steps below cover both, plus enabling authorization and connecting with the new user.

If Authorization is Enabled

1. Disable Authorization (if enabled):

Open the MongoDB configuration file:

sudo nano /etc/mongod.conf

Comment out the following lines:

#security:
#  authorization: enabled

2. Restart MongoDB:

Restart the MongoDB service:

sudo service mongod restart

3. Connect to MongoDB Shell:

Enter the MongoDB shell:

mongosh

4. Show Databases:

Display all available databases:

show dbs

5. Change to Admin Database:

Switch to the admin database:

use admin

6. Create a Superuser with All Privileges:

Execute the following command, replacing "username" with your desired username:

db.createUser({ user: "username", pwd: passwordPrompt(), roles: ["root"] });

For example:

db.createUser({ user: "superuser", pwd: passwordPrompt(), roles: ["root"] });

7. Verify Users:

Check if the user is created successfully:

show users

8. Enable Authorization (if disabled):

Open the MongoDB configuration file again:

sudo nano /etc/mongod.conf

Uncomment the following lines:

security:
  authorization: enabled

9. Restart MongoDB:

Restart the MongoDB service:

sudo service mongod restart

10. Access Mongo Shell as Super User:

Use the following syntax to access the Mongo shell as a superuser:

mongosh --port 27017 --authenticationDatabase "admin" -u "username" -p "password"

For example:

mongosh --port 27017 --authenticationDatabase "admin" -u "superuser" -p "Hello123456"

11. To Make Connection:

Use the following syntax to connect to MongoDB:

mongodb://username:password@IP_Address:27017/database_name

For example:

mongodb://superuser:[email protected]:27017/blogdb

How to Create MongoDB User and Assign to a Database

1. Connect to MongoDB Shell:

If Authorization is disabled, simply run:

mongosh

If Authorization is enabled and you have a Superuser, use:

mongosh --port 27017 --authenticationDatabase "admin" -u "superuser" -p "Hello123456"

2. Show Databases:

Display all available databases:

show dbs

3. Create a New Database:

Choose or create a new database:

use database_name

For example:

use blogdb

4. Create a New User:

Execute the following command, replacing "username" with the desired username and "database_name" with the database name:

db.createUser({
  user: "username",
  pwd: passwordPrompt(),
  roles: [{ role: "readWrite", db: "database_name" }],
});

For example:

db.createUser({
  user: "rahul",
  pwd: passwordPrompt(),
  roles: [{ role: "readWrite", db: "blogdb" }],
});

5. Verify Users:

Check if the user is created successfully:

show users

6. Access Mongo Shell as User:

Use the following syntax to access the Mongo shell as the created user:

mongosh --port 27017 --authenticationDatabase "database_name" -u "username" -p "password"

For example:

mongosh --port 27017 --authenticationDatabase "blogdb" -u "rahul" -p "Hello123456"

7. To Make Connection:

Use the following syntax to connect to MongoDB:

mongodb://username:password@IP_Address:27017/database_name

For example:

mongodb://rahul:[email protected]:27017/blogdb