How to Install and Configure MongoDB on Centos/Redhat Servers
MongoDB is one of the best NoSQL databases. It is been widely used by many big organizations for big data and other workloads.
Install and Configure MongoDB
In this tutorial, we will walk you through the steps for installing and configuring MongoDB on a centos7 or a RedHat 7 server. Also, you will learn the commands and custom configurations for managing the MongoDB server.
1. Create a MongoDB repo file
sudo touch /etc/yum.repos.d/mongodb-org-3.2.repo
2. Copy the following contents to the repo file.
[mongodb-org-3.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
3. Install MongoDB using the yum command.
sudo yum install -y mongodb-org
4. Open /etc/selinux/config
file set the SELINUX parameter as follows.
SELINUX=disabled
Note: If you do not want to disable SeLinux, execute the following command.
semanage port -a -t mongod_port_t -p tcp 27017
27017 is the port which MongoDB runs. If you are using a differnt port, then change it accordingly.
5. Restart the server.
sudo init 6
MongoDb Configuration Files
The default configurations are as follows.
1. You can find the MongoDB data in /var/lib/mongo
directory.
2. All the MongoDB logs are saved to /var/log/mongodb
3. MongoDb configuration file is /etc/mongod.conf
By default, MongoDB service is bind to 127.0.0.1 (localhost). If you are accessing MongoDB from the same server which is installed, then this configuration is fine.
If you want remote access, you need to change the bind_ip
parameter under network interfaces in /etc/mongod.conf
. You can either give 0.0.0.0 to allow any IP or a particular IP. For example,
net: port: 27017 bindIp: 0.0.0.0
or
net: port: 27017 bindIp: 192.168.33.10
Start MongoDb
You can now start the MongoDB service using the following.
sudo service mongod start
or
sudo systemctl start mongod
Manage MongoDb Service
1. To add MongoDB to the boot service,
sudo systemctl enable mongod
2. To restart,
sudo systemctl restart mongod
3. To stop,
sudo systemctl stop mongod
Access MongoDB Shell
To access the MongoDB shell, just execute the following command from the terminal.
mongo