As a Linux user installing and configuring MongoDB for offline use can be challenging. So I am going to show a way to install and set up MongoDB for Linux machines and solve possible challenges along the way.
First, we have to check if we have MongoDB installed using:
mongod --version
if it shows your MongoDB version then you have MongoDB installed already, so skip to the part about configuring the database for usage.
Setting the machine up for MongoDB installation
We have to install MongoDB, to do these we first have to import the public key used by the package management system through the terminal using:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
if you receive an error saying that “gnupg is not installed” install gnupg using
sudo apt-get install gnupg
After installing gnupg run the first command to import the key
NEXT
After importing the keys, we need to add the MongoDB package source to the package resource list, so we do that by typing the following command and click the enter button:
echo "deb http://repo.mongodb.org/apt/debian buster/mongodb-org/5.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
After running the above command we have to refresh/reload the local package database, we do that using the following command:
sudo apt-get update
Installing MongoDB
After going the above section of command our machine is now ready to install MongoDB, we do this by typing the following command:
sudo apt-get install -y mongodb-org
This will install MongoDB on the Linux computer, it will take a while depending on your network signal strength.
Configuring MongoDB for use
Now we have MongoDB installed on our computer, next we need to start the service, we do so using:
sudo systemctl start mongod
This should start MongoDB service, but if you get the error “failed to start mongod.service: unit mongod. service not found” don’t worry run the following commands one after the other:
sudo systemctl daemon-reload
sudo systemctl start mongod
This should start the service, we can verify this by checking the status using:
sudo systemctl status mongod.service
The above screen will show if the MongoDB service is already running. if you still have an error use the command below to but enable MongoDB to run as a service and start on boot
sudo systemctl enable mongod.service
sudo systemctl start mongod.service
With this MongoDB is now up and running on our computer.
Lastly, to stop MongoDB as a service we have to run the command below:
sudo systemctl stop mongod.service
Thanks for reading.