Hamsa K
Editor
8 min read | 5 months ago

How to Install and Configure Mongodb on Ubuntu 18 04

Install and configure Mongodb on ubuntu18.04

MongoDB is a document database, which means it stores data in JSON-like documents.It is a free and open-source document database and it belongs to a family of databases called NoSQL which are different from the traditional table-based SQL databases like Mysql.The data is stored in a "document" structure in JSON format.

Step 1: Install mongodb

mongodb package is available in Ubuntu’s official repositories.so we will use apt command to install it. Before install mongodb, Lets update the packages list to have the most recent version of the repository listings.

root@ubuntu18:~# apt update

Now install mongodb using apt.

root@ubuntu18:~# apt install mongodb
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libc-client2007e libpcre2-8-0 mlock
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libboost-filesystem1.65.1 libboost-iostreams1.65.1 libboost-program-options1.65.1 libboost-system1.65.1 libgoogle-perftools4 libpcrecpp0v5 libstemmer0d
  libtcmalloc-minimal4 libyaml-cpp0.5v5 mongo-tools mongodb-clients mongodb-server mongodb-server-core
The following NEW packages will be installed:
  libboost-filesystem1.65.1 libboost-iostreams1.65.1 libboost-program-options1.65.1 libboost-system1.65.1 libgoogle-perftools4 libpcrecpp0v5 libstemmer0d
  libtcmalloc-minimal4 libyaml-cpp0.5v5 mongo-tools mongodb mongodb-clients mongodb-server mongodb-server-core
0 upgraded, 14 newly installed, 0 to remove and 89 not upgraded.
Need to get 53.5 MB of archives.
After this operation, 217 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

once mongodb is installed, Mongodb server is automatically started after the installation.This can check through service status on next step.

Step 2: verify mongodb service

To check mongodb service use following command

root@ubuntu18:~# systemctl status mongodb
● mongodb.service - An object/document-oriented database
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-03-03 09:27:39 UTC; 1min 52s ago
     Docs: man:mongod(1)
 Main PID: 18819 (mongod)
    Tasks: 23 (limit: 1670)
   CGroup: /system.slice/mongodb.service
           └─18819 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config /etc/mongodb.conf
Mar 03 09:27:39 ubuntu18 systemd[1]: Started An object/document-oriented database

and enable it on boot with below command.

root@ubuntu18:~# systemctl enable mongodb

Bydefault it uses port 27017 and we will connect to the MongoDB database server using the mongo tool to check connection status

mongo --eval 'db.runCommand({ connectionStatus: 1 })'
root@ubuntu18:~# mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
{
        "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
        },
        "ok" : 1
}

Step 3: Allow Remote access to db

By default mongodb server runs on port 27017 on localhost.To allow access in firewall from everywhere run the following command.

root@ubuntu18:~# ufw allow 27017

But this causes security issue. Enabling internet access to MongoDB server gives anyone unrestricted access to the database server and its data.MongoDB should be accessed only from certain trusted locations, such as another server hosting an application.

root@ubuntu18:~# ufw allow from other_server_IP/32 to any port 27017
root@ubuntu18:~# ufw status

But we still need to ensure that the MongoDB server does not only listen locally. Therefore, we need to add our trusted machine ip to the conf file.

Edit below mongodb conf file

root@ubuntu18:~# vi /etc/mongodb.conf

Add trusted server ip like below

bind_ip = 127.0.0.1,192.168.0.15

Replace your server ip as above,save the file and exit.

Now restart mongodb service with following command.

root@ubuntu18:~# systemctl restart mongodb
root@ubuntu18:~# systemctl status mongodb

Step 4: configure mongodb user and password

First we will open mongo shell with following command

root@ubuntu18:~# mongo

once you are in the mongodb shell, switch to the database named admin.

use admin

now create root user with below command

db.createUser({user:"root", pwd:"admin@123", roles:[{role:"root", db:"admin"}]})

sample output:

> use admin
switched to db admin
> db.createUser({user:"root", pwd:"admin@123", roles:[{role:"root", db:"admin"}]})
Successfully added user: {
        "user" : "root",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
>

Now exit from the mongodb shell. 

mongodb instance was started without the --auth command line option.you need to enable authentication of users by modifying /lib/systemd/system/mongodb.service like below

root@ubuntu18:~# nano /lib/systemd/system/mongodb.service

Add --auth to ExecStart line like below

ExecStart=/usr/bin/mongod --auth --unixSocketPrefix=${SOCKETPATH} --config ${CONF} $DAEMON_OPTS

save and exit service. Now restart mongodb with following commands.

root@ubuntu18:~# systemctl daemon-reload
root@ubuntu18:~# systemctl restart mongodb
root@ubuntu18:~# systemctl status mongodb

Now we will connect to the MongoDB shell with created user 

root@ubuntu18:~# mongo -u root -p admin@123 --authenticationDatabase admin

you can check available databases with the following command.

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

That's all.Mongodb is a well-known NoSQL database that offers high performance, high availability and automatic scaling. you can find more information on Mongodb official documentation.

 

 



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy