Software Engineer Standing Beside Server Racks

How to Create MongoDB Super User

How to Create MongoDB Super User

If Authorization is Enabled, you can create a MongoDB Super User by following these steps:

  1. Disable Authorization (if enabled):

Open the MongoDB configuration file using the command:

sudo nano /etc/mongod.conf

Comment out the following lines:

#security: # authorization: enabled
  1. Restart MongoDB:

Restart the MongoDB service with the command:

sudo service mongod restart
  1. Connect to MongoDB shell:
  • Enter the MongoDB shell using:
mongosh
  1. Show databases:
  • Display all available databases with:
show dbs
  1. Change to admin database:
  • Switch to the admin database using:
use admin
  1. 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"]})
  1. Verify Users:
  • Check if the user is created successfully:
show users
  1. Enable Authorization (if disabled):
  • Open the MongoDB configuration file again:
sudo nano /etc/mongod.conf
  • Uncomment the following lines:
security: authorization: enabled
  1. Restart MongoDB:
  • Restart MongoDB service:
sudo service mongod restart
  1. 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"
  1. For example:
mongosh --port 27017 --authenticationDatabase "admin" -u "superuser" -p "Hello123456"
  1. To Make Connection:
    • Use the following syntax to connect to MongoDB:
mongodb://username:password@IP_Address:27017/database_name
  1. For example:
mongodb://superuser:Hello123456@216.32.44.12:27017/blogdb

How to Create MongoDB User and Assign to a Database

To create a MongoDB user and assign it to a specific database, follow these steps:

  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"
  1. Show databases:
  • Display all available databases with:
show dbs
  1. Create a New Database:
  • Choose or create a new database using:
use database_name
  • For example:
use blogdb
  1. 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"}]})
  1. Verify Users:
  • Check if the user is created successfully:
show users
  1. 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"
  1. To Make Connection:
  • Use the following syntax to connect to MongoDB:
mongodb://username:password@IP_Address:27017/database_name
  • For example:
mongodb://rahul:Hello123456@216.32.44.12:27017/blogdb