MongoDB is a document-based open-source database management system, which can be used to store a large volume of data in various forms. It is commonly used in modern web applications.
It supports various features, some of which include indexing, replication, load-balancing, file storage, etc.
This guide outlines the steps to install MongoDB in various Linux distributions such as CentOS, Ubuntu and Debian.
CentOS
To install MongoDB in CentOS, the MongoDB repository needs to be first enabled. For this, please create a file /etc/yum.repos.d/mongodb-org.repo with any editor of your choice.
# vi /etc/yum.repos.d/mongodb-org.repo
Add the below entries in the created repository file, save and exit the editor.
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
Execute the below command to install the MongoDB package.
# yum install -y mongodb-org
Start the MongoDB service.
# systemctl start mongod
Stop or Restart the MongoDB service.
# systemctl stop mongod
# systemctl restart mongod
Check the status of the MongoDB service.
# systemctl status mongod
To check and confirm that the MongoDB service has been installed, make use of the mongo
command to access the MongoDB shell.
# mongo
The below screenshot shows how the MongoDB shell looks like.
Ubuntu
To install MongoDB in Ubuntu, the MongoDB repository needs to be enabled prior to the installation. For this, the MongoDB public key needs to be imported first and then create the required source list file for MongoDB.
Import Key:
# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 4B7C549A058F8B6B
Create source list file:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list
Once this has been done, the package repository needs to be updated by executing the below command.
# sudo apt update
Install the MongoDB package.
# sudo apt install -y mongodb-org
Enable and start the MongoDB service.
# sudo systemctl enable mongod
# sudo systemctl start mongod
Stop or Restart the MongoDB service.
# sudo systemctl stop mongod
# sudo systemctl restart mongod
To check and confirm that the MongoDB service has been installed, make use of the mongo
command to access the MongoDB shell.
Debian
To install MongoDB in Debian, it would be to install the required packages for adding a new repository.
# apt install software-properties-common dirmngr
Add the MongoDB public GPG key to the system.
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 –recv
9DA31620334BD75D9DCB49F368818C72E52529D4
The MongoDB repository needs to be added then, for which run the below command.
# add-apt-repository 'deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main'
Update the packages list and install the MongoDB package.
# apt update
# apt install mongodb-org
Enable and start the MongoDB service.
# systemctl enable mongod
# systemctl start mongod
Stop or Restart the MongoDB service.
# sudo systemctl stop mongod
# Sudo systemctl restart mongod
To check and confirm that the MongoDB service has been installed, make use of the mongo
command to access the MongoDB shell.
Fedora
To install MongoDB in Fedora, the MongoDB repository needs to be first enabled. For this, please create a file /etc/yum.repos.d/mongodb-org.repo with any editor of your choice.
# sudo vi /etc/yum.repos.d/mongodb-org.repo
Add the below entries in the created repository file, save and exit the editor.
[Mongodb]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
Execute the below command to install the MongoDB package.
# sudo dnf install -y mongodb-org
Start the MongoDB service.
# systemctl start mongod
Stop or Restart the MongoDB service.
# systemctl stop mongod
# systemctl restart mongod
Check the status of the MongoDB service.
# systemctl status mongod
To check and confirm that the MongoDB service has been installed, make use of the mongo command to access the MongoDB shell.
# mongo
The below screenshot shows how the MongoDB shell looks like.
Related Tutorials