MongoDB is a cross-platform, document-oriented database that is classified in the NoSQL family. That means it’s human-readable and scalable to meet the high demands of enterprise businesses. MongoDB works on all types of computing platforms, whether they be cloud-hosted or on-premises, and can serve as the backend for numerous use-cases.
I’ve covered the installation of MongoDB for Ubuntu Server 16.04 (see: How to install MongoDB community edition on Ubuntu Linux), but the process has changed since 2016. This time around we’re going to install MongoDB Community Edition on both Ubuntu Server 20.04 and AlmaLinux (the CentOS fork). In an upcoming piece, I’ll show you how to then create a MongoDB cluster.
SEE: Linux service control commands (TechRepublic Premium)
What you’ll need
To get MongoDB installed, you’ll need a running, updated instance of either Ubuntu Server 20.04 or AlmaLinux. You’ll also need a user with sudo privileges.
Note: The official (and initial) release of AlmaLinux isn’t until March 30, 2021. If you want to test this process, you can download the beta release or migrate a running instance of CentOS 8 to AlmaLinux (see: How to migrate CentOS to AlmaLinux and avoid downtime in your data center).
How to install MongoDB on Ubuntu Server 20.04
The first thing we must do is import the MongoDB GPG key onto the server. To do that, log in to your Ubuntu instance and issue the command:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
Once the key is added, we then must create a new apt list file for MongoDB. Issue the command:
sudo nano /etc/apt/sources.list.d/mongodb-org-4.4.list
In that file, paste the following contents:
deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse
Save and close the file.
Update the apt database with the command:
sudo apt-get update
After apt completes the update process, you can install the latest version of MongoDB with the command:
sudo apt-get install -y mongodb-org
When the installation finishes, start and enable the MongoDB service with the commands:
sudo systemctl start mongod
sudo systemctl enable mongod
You can now launch the MongoDB shell with the command:
mongo
How to install MongoDB on AlmaLinux
Let’s head over to AlmaLinux for the installation. Log in to that instance and create a new yum repo file with the command:
sudo nano /etc/yum.repos.d/mongodb-org-4.4.repo
In that file, paste the following:
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
Save and close the file.
Install MongoDB with the command:
sudo dnf mongodb-org -y
After the installation completes, start and enable the service with the commands:
sudo systemctl start mongod
sudo systemctl enable mongod
Like with Ubuntu, to access the MongoDB shell, issue the command:
mongo
And that’s all there is to installing MongoDB on both Ubuntu Server and AlmaLinux. These steps can be used for nearly any Debian- or Red Hat-based Linux distributions.
Next time around we’ll take a look at how to create a small cluster of MongoDB servers.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.