In this guide we are going to learn how to install MongoDB 5.0 Community Edition on an Ubuntu 20.04 server.
MongoDB is a cross-platform document-oriented NoSQL database program that uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.
MongoDB was built for people building internet and business applications who need to evolve quickly and scale elegantly. Companies and development teams of all sizes use MongoDB for a wide variety of reasons.
Instead of storing data in tables of rows or columns like SQL databases, each record in a MongoDB database is a document described in BSON, a binary representation of the data. Applications can then retrieve this information in a JSON format.
Here’s a simple JSON document describing a historical figure.
{
"_id": 1,
"name": {
"first": "Ada",
"last": "Lovelace"
},
"title": "The First Programmer",
"interests": ["mathematics", "programming"]
}
Related Content
- How to install and configure MongoDB 5 on Debian 11
- How to install and configure MongoDB 5 on FreeBSD 13
- How to install Mongodb 5 in Opensuse Leap 15.3
- How to run Mongodb with Docker and Docker-Compose
- How to Set up Authentication in Mongodb
- How to install Mongodb 5 in Rocky Linux/Centos 8
- How to install Mongodb 5 on Fedora 34/35
Prerequisites
To follow along, ensure you have the following:
- An up to date Ubuntu 20.04 or latest server
- Knowledge of Linux Terminal
- Access to the internet
- Root access to the server or User with Sudo access
Table of Content
- Ensuring the server is up to date
- Instaling MongoDB
- Starting and enabling MongoDB service
- Connecting to Mongodb and Executing some test commands
- Uninstall MongoDB Community Edition
1. Ensuring the server is up to date
Before proceeding, let us make sure that our server repos and packages are in the latest versions by updating using this command:
sudo apt update
sudo apt upgrade -y
2. Installing MongoDB Community Edition
The Mongodb packages are not available in the default Ubuntu repos. To add the repo, do the following:
Import the public key used by the package management system. From a terminal, issue the following command to import the MongoDB public GPG Key from https://www.mongodb.org/static/pgp/server-5.0.asc:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
The operation should respond with an OK
.
However, if you receive an error indicating that gnupg
is not installed, you can install gnupg
and its required libraries using the following command.
sudo apt-get install gnupg
Create the list file using the command for your version of Ubuntu:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
Issue the following command to reload the local package database:
sudo apt update
You can install either the latest stable version of MongoDB or a specific version of MongoDB
sudo apt install -y mongodb-org
To install a specific release, you must specify each component package individually along with the version number, as in the following example:
sudo apt-get install -y mongodb-org=5.0.4 mongodb-org-database=5.0.4 mongodb-org-server=5.0.4 mongodb-org-shell=5.0.4 mongodb-org-mongos=5.0.4 mongodb-org-tools=5.0.4
If you only install mongodb-org=5.0.4
and do not include the component packages, the latest version of each MongoDB package will be installed regardless of what version you specified.
You can check the version of MongoDB installed using the following command:
$ mongo -version
MongoDB shell version v5.0.4
Build Info: {
"version": "5.0.4",
"gitVersion": "62a84ede3cc9a334e8bc82160714df71e7d3a29e",
"openSSLVersion": "OpenSSL 1.1.1f 31 Mar 2020",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "ubuntu2004",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
4. Starting and Enabling MongoDB Service
Once the service has been installed, it will not be started by default. Start the mongodb service using this command:
sudo systemctl start mongod
Confirm that the service is up and running by checking its status:
$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2021-11-23 18:34:51 UTC; 8s ago
Docs: https://docs.mongodb.org/manual
Main PID: 16098 (mongod)
Memory: 62.2M
CGroup: /system.slice/mongod.service
└─16098 /usr/bin/mongod --config /etc/mongod.conf
Nov 23 18:34:51 ip-10-2-40-93 systemd[1]: Started MongoDB Database Server.
Our mongodb service is up and running. Let us enable it to start on boot using this command:
sudo systemctl enable mongod
5. Connecting to Mongodb and Executing some test commands
Use the mongo command to connect to the mongo shell.
$ mongo --port 27017
MongoDB shell version v5.0.4
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("eda3ce87-6c48-4d71-b76c-ff1476f0e7c9") }
MongoDB server version: 5.0.4
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2021-11-23T18:34:51.741+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2021-11-23T18:34:52.557+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
> db.version()
5.0.4
>
Let us execute some test commands to confirm its proper workings:
> use citizix_db;
switched to db citizix_db
>
> db.users.save({
... name: "etowett",
... location: "Arctic Vault"
... });
WriteResult({ "nInserted" : 1 })
>
> db.users.find();
{ "_id" : ObjectId("619d34f11e21af9862e25c5d"), "name" : "etowett", "location" : "Arctic Vault" }
>
6. Uninstall MongoDB Community Edition
To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs.
This process will completely remove MongoDB, its configuration, and all databases. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.
The following section guides you through the necessary steps.
Stop MongoDB
Stop the mongod
process by issuing the following command:
sudo service mongod stop
Remove Packages.
Remove any MongoDB packages that you had previously installed.
sudo apt remove mongodb-org
Remove Data Directories.
Remove MongoDB databases and log files.
sudo rm -r /var/log/mongodbsudo rm -r /var/lib/mongo
Conclusion
In this guide, we managed to Install and do some basic operations with MongoDB on our Ubuntu Server.