How to create root user in MongoDB ubuntu 20.04 LTS


 

By default, MongoDB has no any kind of authentication, We can create a root/admin user to manage MongoDB, Here we have a roles list for different-different purpose.

 

1.read
2.readWrite
3.dbAdmin
4.userAdmin
5.clusterAdmin
6.readAnyDatabase
7.readWriteAnyDatabase
8.userAdminAnyDatabase
9.dbAdminAnyDatabase


Here you can get the bulk data insert python3 script for load testing. 

https://www.techbeginner.in/2021/06/how-to-bulk-insert-in-mongodb-using.html

 

In this post, We will create root/admin user in MongoDB on ubuntu 20.04 LTS.

 

Step 1: Login MongoDB Shell

 

You need to login mongoDB shell and execute the following command one by one.

mongo

After this you will get your mongoDB shell like this.


 

Step 2: Select Admin Database

 

You need to use/select the MongoDB’s admin database by using the given command.

use admin

After this you should get output like this.

 

Step 3: Create a Root User

 

Here we can create a root user to manage MongoDB, We need to specify the username, password and roles, In my case i am using below credentials.

 

Username – root

Password – password

Role – admin

 

You need to change the username, password in real scenario.

db.runCommand({"createUser" : "root","pwd" : "password","customData" : {},"roles" : [{"role" : "root","db" : "admin"}]});

You should get output like this.

After successful execution of the query, We need to enable authentication module in MongoDB by using the main configuration file.

 

Step 4: Enabling Authentication in MongoDB

 

We need to edit and enable to authentication in MongoDB by following given command.

sudo vim /etc/mongod.conf

After this we need to apply authentication security by following to add lines in MongoDB config files. 

security:
    authorization:enabled
setParameter:
    enableLocalhostAuthBypass:false

Syntax should be like this.

Save and exit from the text editor.

Step 5: Restart MongoDB

 

Now we need to restart the MongoDB service to get new changes, for this use the given command.

sudo systemctl restart mongod


Step 6: MongoDB Testing

To test our mongoDB authentication, We need to use the following command to access mongoDB shell, Use the following command for the same.


mongo -u root -p password localhost/admin


You need to replace your credentials with above command.

 

Standard  user creation in MongoDB.

https://www.techbeginner.in/2021/05/how-to-create-standard-user-in-mongodb.html

  

Conclusion

 

We have successfully created root user with MongoDB on ubuntu machine, Still you have any issue leave a comment.

 

 


How to create root user in MongoDB ubuntu 20.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top