MongoDB is a free, cross-platform, open source, NoSQL document-oriented database. It supports GNU/Linux, Microsoft Windows, Solaris, and Mac OS X. MongoDB is available as two editions, Community and Enterprise edition. MongoDB community edition is free for personal usage, whereas enterprise edition is paid version that have more features and official support compared to the community edition. In this brief tutorial, we will see how to install MongoDB community edition in Linux.
Install MongoDB community edition in Linux
MongoDB is packaged for all major Linux operating systems.
Install MongoDB in RHEL, CentOS, AlmaLinux, Rocky Linux:
Set SELinux to permissive mode or disable it completely.
To do so, edit /etc/selinux/config file:
$ sudo vi /etc/selinux/config
and set the SELINUX value as permissive or disabled.
SELINUX=permissive
Reboot the system to take effect the changes.
Add MongoDB repository and install it using yum.
To do so, create /etc/yum.repos.d/mongodb.repo file:
$ sudo vi /etc/yum.repos.d/mongodb.repo
Add the following lines:
[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
Save and close the file. Then, update the repository list with command:
$ sudo yum update
Finally, install MongoDB using command:
$ sudo yum install mongodb-org
Start the MongoDB service using command:
$ sudo systemctl start mongod
To verify the service is started, run:
$ sudo systemctl status mongod
Similarly, to restart or stop the service, run:
$ sudo systemctl restart mongod
$ sudo systemctl stop mongod
To start MongoDB service on every reboot, run:
$ sudo systemctl enable mongod
Install MongoDB in Debian:
First, Import MongoDB public key:
$ wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -
Create a new file called /etc/apt/sources.list.d/mongodb.list:
$ sudo touch /etc/apt/sources.list.d/mongodb.list
For Debian 8, run the following command to add MongoDB repository:
echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb.list
For Debian 9:
echo "deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb.list
Update the repository list:
$ sudo apt-get update
Finally, install it using command:
$ sudo apt-get install mongodb-org
Start MongoDB service using command:
$ sudo service mongod start
Similarly, restart/stop the service with command:
$ sudo service mongod restart
$ sudo service mongod stop
Install MongoDB in Ubuntu:
First, import the MongoDB public key:
$ wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -
Next, we should add MongoDB repository in the Ubuntu system.
To do so, create a repository file with command:
$ sudo touch /etc/apt/sources.list.d/mongodb.list
Then, run the following command to add the MongoDB repository URL in mongodb repository file:
On Ubuntu 18.04 LTS:
$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list
On Ubuntu 16.04 LTS:
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list
On Ubuntu 14.04 LTS:
$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list
Update the repository lists using command:
$ sudo apt-get update
Finally, install MongoDB using the following command:
$ sudo apt-get install mongodb-org
Once installation is completed, run the following command to start the MongoDB service:
$ sudo systemctl start mongod
To check the status of the mongodb service, run:
$ sudo systemctl status mongod ● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: en Active: active (running) since Wed 2019-03-13 11:00:27 UTC; 1s ago Docs: https://docs.mongodb.org/manual Main PID: 2230 (mongod) CGroup: /system.slice/mongod.service └─2230 /usr/bin/mongod --config /etc/mongod.conf Mar 13 11:00:27 ubuntuserver systemd[1]: Started MongoDB Database Server
Similarly, to restart/stop this service, run:
$ sudo systemctl restart mongod
$ sudo systemctl stop mongod
Run this command to start this service automatically on every reboot.
$ sudo systemctl enable mongod
Install MongoDB in openSUSE:
Import MongoDB key:
$ sudo rpm --import https://www.mongodb.org/static/pgp/server-4.0.asc
Add MongoDB using command:
$ sudo zypper addrepo --gpgcheck "https://repo.mongodb.org/zypper/suse/12/mongodb-org/4.0/x86_64/" mongodb
Then, run the following command to install MongoDB:
$ sudo zypper -n install mongodb-org
Start the MongoDB service command with command:
$ sudo service mongod start
To restart the service, run:
$ sudo service mongod restart
To stop it:
$ sudo service mongod stop
Enable the service on every reboot, run:
$ sudo chkconfig mongod on
After installing MongoDB, run the following command to log in to mongo shell.
mongo
To exit from the shell, run:
exit
Check the official MongoDB getting started guide for in-depth MongoDB usage.
Suggested read:
Uninstall MongoDB
To completely remove MongoDB from your system, do the following.
First, stop the service using command:
$ sudo systemctl stop mongod
Or,
$ sudo service mongod stop
Next, remove the mongodb packages using command:
On RHEL / CentOS:
$ sudo yum erase $(rpm -qa | grep mongodb-org)
On Debian:
$ sudo apt-get purge mongodb-org*
On openSUSE:
$ sudo zypper remove $(rpm -qa | grep mongodb-org)
Finally, delete database and log files.
$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongo
And, that's all for now. You know now how to install MongoDB on different Linux flavors, and how to remove MongoDB from your system if you don't need it anymore.
Related read:
2 comments
Mongo only pawn… in game of life.
This is precise and accurate, thanks alot for this onpoint guidance