Installing MongoDB in CentOS 7

Posted by Jose Estudillo on December 10, 2014

Installing MongoDB in CentOS 7

Adding MongoDB repositories to yum

Following the steps specified in the [MongoDB installation guide][], we need to add mongo to the yum repository adding to /etc/yum.repos.d/mongodb.repo the following:

[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

As super user this file can be created using the command showed below:

printf "[mongodb]\nname=MongoDB Repository\nbaseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/\ngpgcheck=0\nenabled=1\n" > /etc/yum.repos.d/mongodb.repo

Installing MongoDB with yum

Once the file have been created we can install MongoDB using yum:

# run as super user, su/sudo
yum install -y mongodb-org

Configuring MongoDB

By default, MongoDB configuration is stored in /etc/mongodb.conf there is not need to change anything to make it work. In the case external connections are required, the value bind_ip=127.0.0.1 must be commented out. This can be done using:

# run as super user, su/sudo
sed -i -e s/bind_ip=127.0.0.1/#bind_ip=127.0.0.1/g /etc/mongod.conf

By default MongoDB is runining on the port 21017, so as part of allowing external access, this port must be opened in the firewall.

Opening MongoDB ports in CentOS 7

firewall-cmd --permanent --zone=public --add-port=27017/tcp
firewall-cmd --reload

Managing MongoDB service

sudo service mongod start