Programmers use different databases in web and application development. SQL or NoSQL-based databases have different uses according to system requirements and programmers’ preferences. MongoDB is one of these databases. MongoDB is one of the most popular No SQL databases, which has a flexible structure and is mostly used in projects with large volumes of data. This article will teach you How to Install and run MongoDB on Ubuntu 20.10.
Prerequisites
– A Linux VPS with Ubuntu operating system
– Root or sudo access
– Sufficient system resources including disk space, RAM, and CPU.
First, your Ubuntu 20.10 must have a non-root executable user and a UFW-configured firewall. You need to install the latest version of MongoDB software. To install and run MongoDB on Ubuntu, you should put the dedicated MongoDB package repository in your APT resources. You can now install MongoDB.
Enter the following command to enter the public GPG key for the latest stable version of MongoDB:
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
cURL is a command-line tool used in many operating systems to transfer data. cURL reads the data stored in the URL and prints the content to the system output. If the GPG server crashes or cURL is unable to connect to the GPG server, cURL will not add the resulting error code to your list of valid keys.
If the key is added successfully, you will see the following command key:
Output OK
To double-check how to add the key correctly, you must enter the following command:
apt-key list
You can return the MongoDB key somewhere in the output by entering the following command:
Output /etc/apt/trusted.gpg -------------------- pub rsa4096 2019-05-28 [SC] [expires: 2024-05-26] 2069 1EEC 3521 6C63 CAF6 6CE1 6564 08E3 90CF B1F5 uid [ unknown] MongoDB 4.4 Release Signing Key <[email protected]> . . .
The APT looks for online sources of packages for download and installation in either sources.list file or sources.list.d directory. The sources.list.d directory allows you to add sources.list them as separate files. sources.list Lists the active sources of APT data. So that your first preferred sources are at the top of the list and a source is mentioned in each line.
Creates a file in the sources.list.d directory named mongodb-org-4.4.4 by running the following command:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
In this step, to find the mongodb-org package, enter the following command and then update the local server package index:
sudo apt update
After activating the repository, install the mongodb-org package by entering the following command:
sudo apt install mongodb-org
In this step, to confirm the installation of the package, you must select Y and then ENTER.
MongoDB is now installed on your system but cannot be used. At this point, you start the MongoDB service and test the databases.
You can manage MongoDB with various systemctl commands. Start Daemon MongoDB and activate it by entering the following command:
sudo systemctl start mongod.service
Now you should check the status of the service. Note that this command does not include .service in the service file definition. It is not necessary to enter systemctl in the following command. If it is not already present, it will automatically add to any argument you pass:
sudo systemctl status mongod
The output will look something like the following and indicate that the service is active:
mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled) Active: active (running) since Tue 2020-06-09 12:57:06 UTC; 2s ago Docs: https://docs.mongodb.org/manual Main PID: 37128 (mongod) Memory: 64.8M CGroup: /system.slice/mongod.service └─37128 /usr/bin/mongod --config /etc/mongod.conf
You must now confirm “the service is running as expected“.
Activate the MongoDB service by entering the following command to start it at startup:
sudo systemctl enable mongod
To check if the installation is successful, use the following command to connect to the MongoDB database server and extract the current version, server address, and port:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
In the output below, the value of 1 for the ok field in the response indicates that the server is working as expected:
Output MongoDB shell version v4.4.0 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("1dc7d67a-0af5-4394-b9c4-8a6db3ff7e64") } MongoDB server version: 4.4.0 { "authInfo" : { "authenticatedUsers" : [ ], "authenticatedUserRoles" : [ ] }, "ok" : 1 }
Like other Ubuntu services, you can manage MongoDB using standard systemctl commands. The systemctl status command checks the status of the MongoDB service:
sudo systemctl status mongod
You can stop the service with the following command:
sudo systemctl stop mongod
Run the following command to start the service again:
sudo systemctl start mongod
You can restart the running server with the following command:
sudo systemctl restart mongod
To disable automatic startup, enter the following command:
sudo systemctl disable mongod
Now enter the following command to re-enable it to startup:
sudo systemctl enable mongod
If you wish, you can read The Ultimate Guide to Install MongoDB on Ubuntu 22.04.
In this guide, we walked you through the process of installing and running MongoDB on Ubuntu. By following these steps, you have successfully set up MongoDB on your Ubuntu machine and can now start leveraging its powerful features for handling large volumes of data. Remember to keep your installation updated by regularly checking for MongoDB updates and applying them as needed.
How useful was this post?
Click on a star to rate it!
Average rating 5 / 5. Vote count: 1
No votes so far! Be the first to rate this post.
A system monitoring tool, also known as monitoring software or system monitor, is a type of software...
We live in a data-driven world and these data should be organized and easily accessible information....
What is your opinion about this Blog?
sudo systemctl start mongod
mongo
sudo nano /etc/systemd/system/mongodb.service
sudo systemctl daemon-reload
sudo systemctl enable mongodb
sudo systemctl start mongodb
sudo systemctl stop mongodb