How to Install MongoDB on Ubuntu 18.04 | 20.04

How to Install MongoDB on Ubuntu 18.04 20.04

This post shows users and new students that how to Install MongoDB on Ubuntu 18.04 | 20.04. If your are going to Install MongoDB on Ubuntu then this post is ideal for you.

MongoDB is a open source and cross-platform document-oriented database system. It is Classified as a NoSQL database system. It is developed by MongoDB Inc. and licensed under the Server Side Public License.

For more detail above MongoDB, go to its official website.

Follow below steps to start Installing MongoDB on Ubuntu:

Step 1: Add MongoDB Package repository to Ubuntu

To get the latest version of MongoDB, you need to add its official repository to Ubuntu. For to do that simply, run the below commands to add the official repository key.

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

Now, run the below commands to add MongoDB repository :

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

Step 2: Install MongoDB on Ubuntu 18.04 |20.04

Now run the below commands to install the package.

sudo apt update
sudo apt install mongodb-org

Step 3: Manage MongoDB

After finish installing MongoDB, run the below commands to stop, start and enable MongoDB services.

sudo systemctl stop mongod.service
sudo systemctl start mongod.service
sudo systemctl enable mongod.service

Then, run the below commands for verifying that MongoDB is running and active :

sudo systemctl status mongod

When you run the above command it will show result like the below:

mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-05-21 12:37:32 CDT; 52s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 3409 (mongod)
     Memory: 72.7M
     CGroup: /system.slice/mongod.service
             └─3409 /usr/bin/mongod --config /etc/mongod.conf

May 21 12:37:32 ubuntu2004 systemd[1]: Started MongoDB Database Server.

Run the below commands to connect MongoDB shell:

mongo --host 127.0.0.1:27017

Above command show result similar like below:

MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1b88c27f-e477-4a29-b562-835ee56591b5") }
MongoDB server version: 4.2.6
Welcome to the MongoDB shell.
For interactive help, type "help".

Step 4: Adding Admin User

In the MongoDB, by default authentication is not enabled for MongoDB users. But it is important to secure your server and enable user authentication.

So use the below steps to secure your server and enable user authentication.

Run the below command to create a admin user after you have logged into MongoDB server.

> use admin

Then run the below commands to create a new admin user

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

You will see that admin user created succesfully.

Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

Now, exit and continue below to enable MongoDB authentication.

For Enable, run the below commands to open MongoDB config file.

sudo nano /lib/systemd/system/mongod.service

Now change highlighted line to similar like below and save.

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf
PIDFile=/var/run/mongodb/mongod.pid
# file size

Save and exit the config file.

Now, restart the MongoDB to apply the changes.

sudo systemctl daemon-reload
sudo service mongod restart

After finish installing MongoDB, its configuration file is located at /etc/mongod.conf.

Open the configuration file by running the commands below to enable authentication for all the users who can access the database without authenticating.

sudo nano /etc/mongod.conf

Now, edit the line shown below to enabled authentication:

security:
  authorization: enabled

After making above changes in config file, restart the MongoDB services.

sudo service mongod restart

Now only authentication users is allowed to access the database server.

mongo -u admin -p new_password_here --authenticationDatabase admin

Run the below commands to verify that authentication of MongoDB server is enabled.

mongo -u admin -p --authenticationDatabase admin

When you run above command then it will be asked for a password.

Step 5: Completely Remove MongoDB

If you want to completely remove MongoDB server from your system then you need to remove the MongoDB applications, its configuration files, and its related data and logs.

Stop the MongoDB server

sudo systemctl stop mongod.service

Then remove all MongoDB packages

sudo apt purge mongodb-org*

Now, remove databases and log files.

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

That’s all

If you find any error and issue in above steps , please use comment box below to report.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top