MongoDB is an open-source, NoSQL, document-based database. MongoDB provides a number of features,
- Schemaless
- Provides replication
- Easy to scale-out
- index on any attribute
- auto-sharding
- Deep/rich query abilities, etc
It has been created keeping the current database requirement in mind for modern applications & the cloud era. 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 databases present.
Recommended Read: How to install Google Cloud SDK in Linux
Also Read: Simple guide to install MongoDB on Ubuntu
In this tutorial, we will learn to install MongoDB on CentOS, we will be installing the community edition of MongoDB. We have options to install either using the official repositories or using the source packages.
Install MongoDB on CentOS / RHEL using yum package manager
First we need to add the official MongoDB repositories on our system. To do that create a repository file,
# vi /etc/yum.repos.d/mongodb-org-4.4.repo
& add the following content to it.
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
Once we have added the repository, we can now install MongoDB using the following command,
# yum install -y mongodb-org
We can also install a particular version for MongoDB,
# yum install -y mongodb-org-4.4.2 mongodb-org-server-4.4.2 mongodb-org-shell-4.4.2 mongodb-org-mongos-4.4.2 mongodb-org-tools-4.4.2
This will install MongoDB on CentOS / RHEL. We will now discuss installation method using source/tar packages.
Install MongoDB on CentOS / RHEL using source packages
First we need to install some packages on CentOS / RHEL before MongoDB installation,
# yum install libcurl openssl
next, 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/src/mongodb-src-r4.4.2.tar.gz
Now extract the file to the created directory,
# tar -xvzf mongodb-src-r4.4.2.tar.gz -C /data/mongodb
Next, 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
We have now installed MongoDB on CentOS / RHEL system.
Configuring MongoDB
After the installation is complete, we can start the MongoDB with the following command,
# systemctl start mongod
Note:- You can also start MongoDB with the following command if you have installed it using the tar package,
# mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
Once the DB has started, we can connect to it on the local system by running the following command from the terminal,
# mongo
Now, that is the way to connect MongoDB from a local system only but we can also allow connections from remote systems. For that, we need to make some changes to the config file.
Open the MongoDB config file,
# vi /etc/mongod.conf
& look for line with “net.bindIp” & then change it to following,
net.bindIp 0.0.0.0
Or you can also mention a single IP address like,
net.bindIp 10.10.1.10
You can also mention multiple IPs or a network subnet. Also if you have started your DB with the following command,
# mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
Then you need to also add ‘--bind_ip’ parameter followed by the remote IP address, like,
# mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --bind_ip 0.0.0.0 --fork
Once you have made the required changes, we can then apply the changes by restarting the services,
# systemctl stop mongod
# systemctl start mongod
Or if you used the other method to start the DB, then kill the MongoDB service & start it again. So that’s it, this completes our tutorial on how to install MongoDB on CentOS / RHEL. Do send in any questions or queries you might have using the comment box below.