How to Install MongoDB on Ubuntu 18.04

Installing MongoDB on Ubuntu 18.04.

sudo apt update

Install MongoDB package

Output:

root@vps:~# sudo apt install mongodb
Reading package lists... Done
Building dependency tree
Reading state information... Done
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 libsnappy1v5 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

Managing the MongoDB Service.

To start a MongoDB service.

sudo systemctl start mongodb

To enable MongoDB service.

sudo systemctl enable mongodb   

You can verify its status using the systemctl command.

sudo systemctl status mongodb

Output:

root@vps:~# sudo systemctl status mongodb
  ● mongodb.service - An object/document-oriented database
  Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset:
  Active: active (running) since Thu 2020-03-19 14:28:08 CET; 2min 45s ago
  Docs: man:mongod(1)
  Main PID: 12970 (mongod)
  Tasks: 23 (limit: 1113)
  CGroup: /system.slice/mongodb.service
       └─12970 /usr/bin/mongod --unixSocketPrefix=/run/mongodb --config /etc

Enable Remote MongoDB Access on Firewall

By default MongoDB runs on port 27017, to allow access from everywhere you can use.

sudo ufw allow 27017

Output:

root@vps:~# sudo ufw allow 27017
Rules updated
Rules updated (v6)

But enabling access to MongoDB from everywhere gives unrestricted access to the database data. So, it’s better to give access to specific IP address location to default MongoDB’s port using following command.

sudo ufw allow from your_server_IP/32 to any port 27017 

Output:

root@vps:~# sudo ufw allow from 104.200.67.187/32 to any port 27017
Rules updated

To allow remote MongoDB connections, you need to add your server IP address to /etc/mongodb.conf configuration file as shown.

vim /etc/mongodb.conf

Output:

bind_ip = 127.0.0.1,your_server_ip
#port = 27017

To restart a MongoDB service.

sudo systemctl restart mongodb.service

Access MongoDB Shell.

You can now access MongoDB’s shell by using following command.

mongo

Output:

root@vps:~# mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2020-03-19T14:59:11.066+0100 I CONTROL  [initandlisten]
2020-03-19T14:59:11.066+0100 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-03-19T14:59:11.066+0100 I CONTROL  [initandlisten] **          Read and write access to data and configuration is        unrestricted.
 2020-03-19T14:59:11.066+0100 I CONTROL  [initandlisten]
 >

Once you have connected to the mongo shell, you can list all available databases with the following command.

show dbs

Output: show dbs admin 0.000GB config 0.000GB local 0.000GB

Creating MongoDBan Admin User.

use admin     

db.createUser({user:"root", pwd:"=@!#@%$admin1", roles:[{role:"root", db:"admin"}]})

Output:

> db.createUser({user:"root", pwd:"=@!#@%$admin1", roles:[{role:"root", db:"admin"}]})
  Successfully added user: {
    "user" : "root",
    "roles" : [
            {
                    "role" : "root",
                    "db" : "admin"
            }
    ]
   }

To list MongoDB users created.

show users

Output:

show users
{
  "_id" : "admin.root",
    "user" : "root",
    "db" : "admin",
    "roles" : [
            {
                    "role" : "root",
                    "db" : "admin"
            }
   ]
  }

Configuring Authentication for MongoDB.

The mongodb instance was started without the --auth command line option. You need to enable authentication of users by editing /lib/systemd/system/mongod.service file

sudo vim /lib/systemd/system/mongodb.service 

Under the [Service] config section, find the parameter ExecStart.

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

Change it to the following.

EnvironmentFile=-/etc/default/mongodb
Environment=CONF=/etc/mongodb.conf
Environment=SOCKETPATH=/run/mongodb
ExecStart=/usr/bin/mongod  --auth --unixSocketPrefix=${SOCKETPATH} --config ${CONF} $DAEMON_OPTS

For the changes to come into effect, reload the system and restart MongoDB.

systemctl daemon-reload

sudo systemctl restart mongodb  

sudo systemctl status mongodb   

Output:

root@vps:~# sudo systemctl status mongodb.service
● mongodb.service - An object/document-oriented database
Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset:
Active: active (running) since Thu 2020-03-19 15:18:53 CET; 7s ago
 Docs: man:mongod(1)
 Main PID: 13570 (mongod)
 Tasks: 23 (limit: 1113)
 CGroup: /system.slice/mongodb.service
       └─13570 /usr/bin/mongod --auth

Now when you try to connect to mongodb, you must authenticate yourself as a MongoDB user.

mongo -u "root" -p --authenticationDatabase "admin"

Output:

root@vps:~# mongo -u "root" -p --authenticationDatabase "admin"
MongoDB shell version v3.6.3
Enter password:
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3