By now, you should know how to install MongoDB. So, in this section, we will focus on securing databases in MongoDB. To secure MongoDB, we will start by adding an administrative user to MongoDB, as follows:
- Connect to the Mongo shell from your terminal:
$ mongo
- Select the admin database and add a new user with a username and password (for example, root and password) to this database, as follows:
> use admin
> db.createUser(
{
user: "root",
pwd: "password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" },
"readWriteAnyDatabase" ]
}
)
- Exit the shell and open the MongoDB configuration file from your terminal:
$ sudo nano /etc/mongod.conf
- Look for the security section, remove the hash, and add the authorization setting, as shown here:
// mongodb.conf
security:
authorization: "enabled"
- Save and exit the file and restart MongoDB:
$ sudo systemctl restart...