Securing MongoDB with username and password
This MongoDB article will teach you how to properly configure the most basic security feature: protecting MongoDB with a username and password by creating an authentication user when accessing MongoDB remotely.
As you know, MongoDB is described as a NoSQL database that does not follow any relational database structure; instead, it stores data in JSON documents.
When you install MongoDB, MongoDB remote connection security does not have any authentication enabled, which means anyone with your HOST IP can quickly access your MongoDB service and perform CRUD operations without any MongoDB authentication, which is not a secure way to globally integrate a mongod database.
Furthermore, having no authentication means inviting everyone into your database, confiscating everything, and potentially ransoming your data.
This MongoDB article will show you how to secure MongoDB authentication so that only users with MongoDB authentication credentials can access the database.
Topics covered in this MongoDB article are:
-
Enabling secure authentication on MongoDB
- Start MongoDB without authentication
- Connect to the server using the MongoDB shell
- Create User Administrator
- Enable authentication in the mongod configuration file
- Connect and authenticate as user admin
- Create additional users
Enabling secure authentication on MongoDB
This MongoDB article applies only to self-managed MongoDB servers. All MongoDB as a Service providers come with authentication pre-enabled.
Start MongoDB without Authentication
Open a shell and run the MongoDB client. This is easy because it is the default behavior of MongoDB.
$ mongo
Connect to the server using MongoDB Shell
$ mongo mongodb://<host>:<port>
The port number will probably be 27017, but you can always change it to another port number for increased security.
Create User Administrator
Change to the admin database:
> use admin
You will need to create a user with the userAdminAnyDatabase role, which allows you to create other users on any existing database. The following example will create the admin123 user with the password pass123.
> db.createUser(
{
user: "admin123",
pwd: "pass123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Then disconnect from the mongo shell using Ctrl+D.
Enable authentication in the mongod configuration file
Edit mongod.conf using your favorite editor and enable authorization.
$ sudo vi /etc/mongod.conf
Open /etc/mongod.conf using any code editor and search for the following lines.
security:
authorization: "disabled"
In the Security section, remove any #
, change Authorization to Enabled, and add it if it's missing. It should look like this:
security:
authorization: "enabled"
If you cannot find mongod.conf or it is named mongodb.conf, then you are using an outdated and broken version of MongoDB.
In this case, you might want to consider upgrading to a newer version of MongoDB. However, if you don't care, do the following:
$ sudo vi /etc/mongodb.conf
Find auth, delete it #
and make sure it is set to true. It should look like this:
auth = true
After changing disabled to enabled, you will need to save the file and restart mongod.
$ sudo service mongodb restart
All clients connecting to this server must authenticate themselves as valid users, and they can only perform actions determined by the roles assigned to them.
Connect and authenticate as user admin
$ mongo mongodb://<host>:<port>> db.auth("admin123", "pass123")
1
Users can also use mongo mongodb://superadmin:thepianohasbeendrinking@<host>:<port>
Connect and authenticate in one step. However, this option is not recommended because it makes your credentials visible in your terminal history and can be read by any program on your computer.
Create additional users
The following operation adds the user admin456 to the test database, where the user has the read and write roles.
> use test
> db.createUser(
{
user: "admin456",
pwd: "xyz123",
roles: [ { role: "readWrite", db: "test" } ]
}
)
Press Enter and mongo will respond Successfully added user: ... Then type exit or use CTRL+C.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
$ne operator in MongoDB
Publish Date:2025/04/11 Views:84 Category:MongoDB
-
This article will discuss how the $ne operator works in MongoDB. In addition, we will list its differences from the $not operator. $ne operator in MongoDB $ne is an operator in MongoDB that stands for not equal to. This will compare the val
MongoDB $Set Operator
Publish Date:2025/04/11 Views:159 Category:MongoDB
-
With the help of this article, you will learn how to use $set the operator to partially update objects in MongoDB so that the new object overlaps/merges with the existing object. The $set operator replaces the value of a field with a given
Difference between $push and $addToSet in MongoDB
Publish Date:2025/04/11 Views:63 Category:MongoDB
-
This article explains the operators in MongoDB. What is the purpose of $push and $addToSet operators. Furthermore, the difference between these two operators is given in the code snippet. This article discusses the following topics. Operato
Sort a collection by date in MongoDB
Publish Date:2025/04/11 Views:64 Category:MongoDB
-
In this MongoDB tutorial, the problem of sorting a collection in MongoDB is discussed. The different ways to sort a collection in the database are briefly explained. Using sort() function in MongoDB This problem is solved using the MongoDB
Counting records in MongoDB
Publish Date:2025/04/11 Views:146 Category:MongoDB
-
This article discusses operators in MongoDB, aggregation operators, and different ways to calculate the total number of records. Operations in MongoDB CRUD operations are a user interface concept that allows users to browse, search, and cha
Pretty printing in MongoDB
Publish Date:2025/04/11 Views:150 Category:MongoDB
-
This article will discuss how to use pretty printing in MongoDB to display formatted results. Pretty printing in MongoDB A cursor is an object that allows programmers in the Mongo world to iterate over documents in a Mongo collection. Altho
MongoDB Adding Elements to an Array
Publish Date:2025/04/11 Views:136 Category:MongoDB
-
This article will cover the various ways to add to an array in MongoDB. Adding to an array in MongoDB Use the $push operator to add values to an array The $push operator is one of the various array update operators provided by MongoDB
MongoDB Search by ID
Publish Date:2025/04/11 Views:131 Category:MongoDB
-
The following article provides an overview of MongoDB find by Id() method. MongoDB provides a find by Id() function which can retrieve documents matching a user id. To use search by Id() in MongoDB, you need to use the find() function. If n
MongoDB starts with a query
Publish Date:2025/04/10 Views:195 Category:MongoDB
-
In this MongoDB article, users will learn how to start queries using $regex. It provides regular expression functionality for pattern matching strings in queries. MongoDB starts querying using $regex If you want to use $regex , use one of t