Install MongoDB 5.0 on Ubuntu 20.04

Install MongoDB 5.0 on Ubuntu 20.04

MongoDB is a cross-platform document database. It is classified as a NoSQL database. Instead of using tables and rows as in the relational databases such as MySQL, MongoDB uses JSON-like documents. MongoDB is an open-source project available under the Server Side Public License (SSPL).

This tutorial explains how to install MongoDB 5.0 on Ubuntu 20.04.

Install MongoDB

Download and add GPG key:

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

Add the MongoDB repository:

echo "deb https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Run the following commands to update the package lists and install MongoDB:

sudo apt update
sudo apt install -y mongodb-org

When installation is completed, we can check version of MongoDB server:

mongod --version

By default, MongoDB service is not started. Start the MongoDB service and enable it to start on boot:

sudo systemctl enable --now mongod

We can use the following command to check if MongoDB service is running:

sudo service mongod status

We can also stop, start and restart the MongoDB service:

sudo service mongod stop
sudo service mongod start
sudo service mongod restart

By default, access control is not enabled in the MongoDB. It controls which users have access to databases and operations. Recommended to enable access control.

First, connect to MongoDB without access control:

mongosh

From MongoDB shell, type the following command to switch to admin database:

use admin

Create a root user which will be used to manage the MongoDB. We set root as username and pwd123 as password.

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

Exit MongoDB shell:

exit

Now we need to enable access control. It can be done in mongod.conf configuration file.

sudo nano /etc/mongod.conf

Find and uncomment security section. Add authorization option to enable role-based access control.

security:
  authorization: enabled

Restart MongoDB service:

sudo service mongod restart

Try to connect to MongoDB using a root user you have previously created:

mongosh -u root -p --authenticationDatabase admin

You can try to connect to MongoDB by typing mongosh without any arguments. Once connected, you can execute show dbs command. You will get error:

MongoServerError: command listDatabases requires authentication

Uninstall MongoDB

If you want to completely remove MongoDB and related dependencies, run the following command:

sudo apt purge --autoremove -y mongodb-org

Disable MongoDB service:

sudo service mongod stop
sudo systemctl disable mongod
sudo systemctl daemon-reload
sudo systemctl reset-failed

Remove GPG key and repository:

sudo apt-key del E2C63C11
sudo rm -rf /etc/apt/sources.list.d/mongodb-org-5.0.list

Remove MongoDB user:

sudo deluser mongodb

You can also remove MongoDB logs, data, and other related directories and files:

sudo rm -rf /var/log/mongodb
sudo rm -rf /var/lib/mongodb
rm -rf ~/.mongodb
rm -rf ~/.dbshell
rm -rf ~/.mongorc.js

Leave a Comment

Cancel reply

Your email address will not be published.