CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > How to enable authentication in MongoDB Server?

How to enable authentication in MongoDB Server?

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: July 27, 2023 ยท 2 min read ยท 0 Comments
Share: X in ๐Ÿ”—
check MongoDb database size

By default, MongoDB has authentication disabled. To enable authentication in MongoDB we need to create a user and also have to change the config file. In this post, we will learn step by step to enable authentication in MongoDB.

After installing and starting the MongoDB you can directly access it with the following command:

mongosh

Now, let’s create a root (which is super) user so that we can manage our MongoDB database and also can log in after enabling authentication.

After opening the mongo cell. Let’s list all the databases with the following command:

show dbs

The output should look like:

> show dbs
admin                      0.000GB
config                     0.000GB
local                      0.000GB

Now, let’s use the admin database.

> use admin
switched to db admin

Now we are inside admin database. All the root level users can be created inside this database. So, let’s create a root user with following query:

db.createUser(
  {
    user: "root",
    pwd: "my_secured_password",
    roles: [ { role: "root", db: "admin" } ]
  }
)

In the above query, we have defined the following things:

  1. Username: root
  2. Password: my_secured_password
  3. Role: root
  4. Database: admin

This means, create a user root with the password my_secured_password. And assign a root level role inside admin database.

We’ve successfully created a root user. Now let’s enable authentication.

To enable authentication, you have to edit the config file which should be in /etc/mongod.conf.

You can use any tool to edit this file. I would prefer to use vim. Hence, use the following command to edit:

sudo vim /etc/mongod.conf

The above command opens the file and you have to press Insert key to make it editable.

I assume you pressed the Insert key.

Then, paste the following property:

security:
    authorization: enabled

Now, you can save these changes with the following step:

  1. Press Esc key
  2. :!wq // It writes the changes and quits the edit mode.

Restart the server with the following command:

sudo systemctl restart mongo

Now, when you try to access MongoDB with a mongo command, you can access it but you can’t see any existing databases. Because the database is protected.

When you try to list the databases using the command:

show dbs

You will see a message like given below:

MongoServerError: command listDatabases requires authentication

Hence, to access the database you have to log in with credentials using the command below:

mongo -u root -p --autnenticationDatabase admin

After executing the above command you may prompt to enter the password.

Enter a password and you should be able to access your MongoDB server with authentication.

Done!

Congratulations! You have successfully enabled authentication in the MongoDB server.

Related Posts:

  • MySQL Commands for Developers
  • Connect MongoDB Atlas Cluster Using Shell
  • Install MongoDB BI Connector on Ubuntu
  • MongoDB useful commands
  • Array Operator in MongoDB
  • Most Frequently Asked Spring Boot Interview…
Tags:devopsmongodb
Was this article helpful?
โ† Previous ArticleArrayIndexOutOfBoundsException in Java
Next Article โ†’How to Create Method in Java?

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap