Authenticating and Authorizing a MongoDB Database in a React and Nodejs project
I don’t want to keep talking and bore you. Let’s run through the authentication process and some errors that follow. I’ll however start from the basics just in case — Sorry:(

To start the mongoDB community database in your localhost, open CMD prompt by pressing Windows+R and typing CMD. Then type

cd C:\Program Files\MongoDB\Server\4.4\bin

This will then take you to the folder in the shell. Then type

mongod --port 27017 --bind_ip 127.0.0.1

For some users, an error might pop when mongod runs and it shuts down. The error would be as follows:

"error":"NonExistentPath: Data directory C:\\data\\db\\ not found. Create the missing directory or specify another path using (1) the --dbpath command line option, or (2) by adding the 'storage.dbPath' option in the configuration file."

This is common. Just go to the root drive of your PC (C: drive in my PC) and create a folder named data and create a folder db inside this folder. If you already have the data folder in the mongoDB folder, just copy and paste that folder in the root drive of your PC (again C: drive in my case).

Now, again open the cmd prompt in the ……\bin folder and run :

mongod --port 27017 --dbpath C:\\data\\db\\ --bind_ip 127.0.0.1

The shell should run smoothly now and should have the last line as :

{“t”:{“$date”:”2020–10–04T19:43:06.904+05:30"},”s”:”I”, “c”:”NETWORK”, “id”:23016, “ctx”:”listener”,”msg”:”Waiting for connections”,”attr”:{“port”:27017,”ssl”:”off”}}

Now simultaneously open another cmd prompt window and type

mongo -- port 27017

You should see this in the window:

The server generated these startup warnings when booting:

2020–10–04T19:43:06.636+05:30: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted

This means that your mongo database is not authenticated and is not SECURE. So what should be done?

In the same shell where mongo is running, type

use admin

and press enter. This should switch to the admin database. Now, type or copy (paste in the shell by right-clicking the mouse) the following with a admin username and password of your choice:

db.createUser(

{

user : "MongoAdmin",

pwd : MongoAdmin,

roles : [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]

}

)

Upon execution, the following appears:

Successfully added user: {

"user" : "MongoAdmin",

"roles" : [

{

"role" : "userAdminAnyDatabase",

"db" : "admin"

},

"readWriteAnyDatabase"

]

}

Now, end the mongod shell by pressing Ctrl + C, and the mongo shell by pressing Ctrl + D.

Now, go to the bin folder of mongoDB and open mongod.cfg. I recommend you to use Notepad++ to open the file and change the settings and save in administrator mode. The config file should look like this:

# mongod.conf

# http://docs.mongodb.org/manual/reference/configuration-options/ # for documentation of all options, see: # Where and how to store data.

storage:

dbPath: C:\Program Files\MongoDB\Server\4.4\data

journal:

enabled: true

# engine:

# mmapv1:

# wiredTiger: # where to write logging data.

systemLog:

destination: file

logAppend: true

path: C:\Program Files\MongoDB\Server\4.4\log\mongod.log # network interfaces

net:

port: 27017

bindIp: 127.0.0.1 #processManagement: security:

authorization: enabled #operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog: #snmp:

There’s lots to configure but they are mostly used for complex projects. But for a simple project, you don’t need to change much except for the :

security:

authorization: enabled

Now restart the

mongod by using the following command:

mongod --auth --port 27017 --dbpath C:\\data\\db\\

2. mongo using the following command:

mongo --port 27017 --authenticationDatabase "admin" -u "MongoAdmin" -p

You can just

(Highly Recommended) : Keep the -p (password) field empty. Upon execution, it prompts you to enter the password — used in cases when somebody is around you waiting to have a sneak at your password — Sorry sneaker :(, or You could just type the password after the -p which would be openly typed.

Upon authenticating with the correct username and password, you will be shown this in the window :

MongoDB shell version v4.4.0

Enter password: *********

connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("klsdsf5989-fu74hh-6622-jdjweu83qy47") }

With a wrong password or by incorrectly following the steps, it will lead to an authentication failure.
['Anees Ahamed']
['Nodejs', 'Authentication', 'Mongodb', 'NoSQL', 'Programming']