MongoDB is a free & open-source No-SQL database, it uses JSON like document format to store data in the database which is unlike the SQL database, where data is stored in a row & column-based format.
MongoDB is very fast & has great performance when compared to the SQL database. MongoDB databases are very easy to scale & they also address various shortcomings that other SQL databased present. MongoDB is completely ideal for the types of DB required to handle the data that we have today i.e. big data, IoT, online gaming, etc.
Recommended Read: How to install MySQL on Ubuntu
Also Read: Recommended guide to install POSTGRESQL from Source
In this tutorial, we will learn to install MongoDB on CentOS/Redhat. There are two methods with which we can install MongoDB,
1- Install using YUM,
2- Install using tar packages.
1- Install MongoDB using yum
The current latest version available is 4.2 & to install it, we need to first install the official MongoDB repositories. To install the repository, follow these steps,
# vim /etc/yum.repos.d/mongodb-org-4.2.repo
& enter the following contents to the file,
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
Save file & exit. Then we can install mongodb with the following command,
# yum install mongodb-org
Or to install a specific version, use the following command,
# yum install -y mongodb-org-4.2.2 mongodb-org-server-4.2.2 mongodb-org-shell-4.2.2 mongodb-org-mongos-4.2.2 mongodb-org-tools-4.2.2
This will install mongodb & all its associated components on your server.
2- Install MongoDB using tar packages
Before we can download & install mongodb using tar packages. We must have some packages installed on our servers,
# yum install libcurl openssl
We will create a directory for keeping the mongodb packages,
# mkdir -p /data/mongodb
Next, we have to download the package using the following command,
# wget https://fastdl.mongodb.org/linux/mongodb-linux-s390x-rhel72-4.2.2.tgz
Next, we need to extract the file to the created directory,
# tar -xvzf mongodb-linux-s390x-rhel72-4.2.2.tgz -C /data/mongodb
Next, we will create symbolic links to the binaries directory,
# ln -s /data/mongodb/bin/* /usr/local/bin/
Now, we need to create a data directory & log directory as well,
# mkdir -p /var/lib/mongo
# mkdir -p /var/log/mongo
Next, we have change ownership of these folders to mongod,
# chown -R mongod:mongod /var/lib/mongo
# chown -R mongod:mongod /var/log/mongo
Note: When installing using yum, all these folders are created by system only with required permissions.
Starting & connecting MongoDB
Once we have installed mongodb, we can start the database with the following command,
# systemctl start mongod
Also, we can use the following command to start db,
# mongod –dbpath /var/lib/mongo –logpath /var/log/mongodb/mongod.log –fork
Now to connect to the database,
# mongo
Connecting database from remote systems
By default, we can only connect the database from the localhost & to be able to connect the database from other systems, we need to make configuration in mongodb configuration file,
# vi /etc/mongod.conf
& look for “net.bindIp”, then change it the following
net.bindIp 0.0.0.0
You can also change & mention a single remote IP address if you only need to connect from a single remote system. After making the changes, you need to restart the database to implement the changes made,
# systemctl stop mongod
# systemctl start mongod
Note:- If running database through CLI, we can mention the parameter “–bind_ip” followed by ‘0.0.0.0’ for connecting from all remote IPs or we can also mention a single IP address.
That’s it guys, we now end this tutorial on how to install MongoDB on CentOS & RHEL. Please do send us your questions, queries & suggestions using the comment box below.