Header Ads

Header ADS

Install MongoDB on ubuntu server and setup remote connection



Step-01 Installing MongoDB: To obtain the most recent version of this software you must include MongoDB dedicated package repository to your API sources, the you will be able to install mongodb-org

curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

This command will return ok if the key was added successfully.

Then run the following command, which creates a file in the sources.list.d named mongodb-org-4.4.list

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

After running this command update servers's local package

sudo apt update

then you can install MongoDB flowing the command

sudo apt install mongodb-org

Step-02 starting MongoDB service: 

sudo systemctl start mongod.service

then check service status by flowing command

sudo systemctl status mongod

The command will return output like the flowing code

mongod.service - MongoDB Database Server
    Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
    Active: active (running) since Fri 2021-11-05 17:21:01 UTC; 8s ago
      Docs: https://docs.mongodb.org/manual
  Main PID: 61787 (mongod)
    Memory: 59.9M
    CGroup: /system.slice/mongod.service
            └─61787 /usr/bin/mongod --config /etc/mongod.conf

After that enable the MongoDB service to start up at boot by flowing command.

sudo systemctl enable mongo

now you can see connection status by flowing command

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

The command will return the status of the database connection

MongoDB server version: 4.4.10
{
       "authInfo" : {
               "authenticatedUsers" : [ ],
               "authenticatedUserRoles" : [ ]
       },
       "ok" : 1
}


Step-03 secure MongoDB:

First of all, adding an administrative user.to add an administrative user you must first connect to the mongo shell by flowing command

mongo

then you will see the mongo shell prompt 

MongoDB shell version v4.4.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a6cfea64-3a83-441e-bfb0-4b3bd30b2ada") }
MongoDB server version: 4.4.10
Welcome to the MongoDB shell.

than run

show dbs

then you will return the below output

admin   0.000GB
config  0.000GB
local   0.000GB

now you have to add an administrative user that's why you must first connect to the admin database by flowing command

use admin

then you will return the below output


switched to db admin

than creating administrative user by flowing command

db.createUser({user:"shohedul",pwd:"*******",roles:[{role:"userAdminAnyDatabase", db:"admin"}]})

than you will return 

Successfully added user: {
       "user" : "shohedul",
       "roles" : [
               {
                       "role" : "userAdminAnyDatabase",
                       "db" : "admin"
               }
       ]
}
Now flowing the command you can exit the mongo shell
 exit

Enabling authentication: to enable authentication you must edit mongo.conf

open the configuration file by the following command

sudo nano /etc/mongod.conf

scroll down and comment-out security section line and then add the authorization parameter and set it to enable

security:
 authorization: enabled

After adding save and closing the file by flowing command CTRL + X then Y and then press Enter.

Then restart the daemon by flowing command

sudo systemctl restart mongod

now check the status of the services to make sure that it restarted correctly

sudo systemctl status mongod

Testing authenticate

mongo

then you will return the output

MongoDB shell version v4.4.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("04bf1b65-1568-4104-bb24-b628961a9da8") }
MongoDB server version: 4.4.10

show dbs
in this command does not return any output because you haven't authenticated as a privileged user. now exit mongo shell CRTL + C or type exit command

next 
mongo -u shohedul -p --authenticationDatabase admin
-u flag which name of the administrative username
then execute the command and provide an administrative password and then show dbs command again.
 show dbs
this command will successfully return a list of database

admin   0.000GB
config  0.000GB
local   0.000GB


Step-04 configure remote access: If you connect to your MongoDB server from a remote location, you have to allow incoming connections to the port where the database is listening by adding a new UFW rule.
now execute the command
sudo lsof -i | grep mongo 

and see the output 
mongod    62136   mongodb   10u  IPv4 568247      0t0  TCP localhost:27017 (LISTEN)



next open the MongoDB configuration file and bindIp value: 0.0.0.0 in the network interface section

# network interfaces
net: 
 port: 27017
 bindIp: 0.0.0.0

After adding save and closing the file by flowing command CTRL + X then Y and then press Enter.

Then restart the daemon by flowing command

sudo systemctl restart mongod


Test remote activity

nc -zv server_ip 27017

if everything is ok then return the output below

Connection to server_ip 27017 port [tcp/*] succeeded!

Now your connection string URL, like this

mongodb://username:password@server_ip:27017

Now you can manage your database remotely.











No comments

Theme images by 5ugarless. Powered by Blogger.