MongoDB is a free and open-source NoSQL database used in modern-day web applications.
In this article, we will install and configure security auth features.
#1 Step: installing MongoDB server
First Update your ubuntu so that all the latest LTS settings can configure.
sudo apt update
Install Mongo using below command
sudo apt install -y mongodb
Note: If any error occurred while installing please refer below.
Click here for force install solution
#2 Step: Verifying and configuring server autostart
By now the database server is automatically started after installation. if not you can follow the below step.
sudo systemctl daemon-reload
sudo systemctl start mongod
sudo systemctl enable mongod
How to verify whether MongoDB is running or not.
$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'
#OUTPUT:
MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.2.2
{
"authInfo" : {
"authenticatedUsers" : [ ],
"authenticatedUserRoles" : [ ]
},
"ok" : 1
}
#3 Step: Configuring Mongo Database users and Role
LC_ALL is the environment variable that overrides all the other localization settings
Note: Below mentioned commands should be run individually one after the other.
$ export LC_ALL=C //
$ mongo
> use admin
> db.createUser({user:"mongoadmin", pwd:"mongoPassword123", roles:[{role:"root", db:"admin"}]})
> exit //then exit mongo shell to terminal.
#4 Step: Securing public access by enabling MongoDB authentication
Open mongod.service file as shown below
$ sudo vim /lib/systemd/system/mongod.service
Find below-mentioned code
ExecStart=/usr/bin/mongod — config /etc/mongod.conf
Replace with the below code with auth.
ExecStart=/usr/bin/mongod — auth — config /etc/mongod.conf
After replacing press ESC key and enter “:wq” to save and exit vim editor.
At last restart the mongo server
$ sudo service mongod restart
After all this please check whether enabled port 27017 in AWS port configuration. If you want to access MongoDB remotely.
#5 Step: Launching the mongo server in the auth mode.
mongo -u mongoadmin -p mongoPassword123 --authenticationDatabase admin
Hope, You hand a greate time installing Mongo Server, All the best for the future projects…
Arjun is a Full-stack developer, who is fond of the web. Lives in Chikmagalur, Karnataka, India