0%

Install MongoDB Database in Ubuntu

Method1:
From Ubuntu Library install MongoDB

This is an easy way to install MongoDB in the system, you only need to enter a command.

Install MongoDB

First, make sure your package is up to date. Open the terminal and enter:

1
sudo apt update && sudo apt upgrade -y

Continue to install MongoDB:

1
sudo apt install mongodb

That’s it! MongoDB is now installed on your computer.

The MongoDB service should be started automatically during installation, but check the service status:

1
sudo systemctl status mongodb

Check if the MongoDB service is running.

Run MongoDB

MongoDB is currently a systemd service, so we use systemctl to check and modify its status, using the following command:

1
2
3
4
sudo systemctl status mongodb 
sudo systemctl stop mongodb
sudo systemctl start mongodb
sudo systemctl restart mongodb

You can also modify whether MongoDB automatically starts with the system (default: enabled):

1
2
sudo systemctl disable mongodb 
sudo systemctl enable mongodb

To start using (creating and editing) the database, please enter:

1
mongo

This will start the mongo shell. For detailed information on queries and options, please check the manual.

Note: Depending on how you plan to use MongoDB, you may need to adjust the firewall. But this is beyond the content of this article, and depends on your configuration.

Uninstall MongoDB

If you install MongoDB from the Ubuntu repository and want to uninstall it (you may have to install it in an officially supported way), please enter:

1
2
3
sudo systemctl stop mongodb 
sudo apt purge mongodb
sudo apt autoremove

This should uninstall MongoDB completely. Make sure to back up any collections or documents you might want to keep, as they will be deleted!