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