When recently building an AWS environment consisting of a number of resources including a replication cluster for MongoDB on a standard linux amazon EC2 instance, I encountered a few minor issues with how the documentation on MongoDB not necessarily covering all basis for how one might go about setting up a replica set.

As a result of this encountered issues with my instances not being able to communicate with each other. Even after specifying the replication set on initiation “mongod –replSet Name”, the master instance still had trouble talking to a slave instance. There were also permission issues with writing data to the specified MongoDB partition data directory amongs’t others.

Long story short.

Just a small note on things to consider and possibly configure when planning on setting up a replica set for MongoDB in production. And again this was all done using an Amazon Web Service EC2 “Amazon Linux AMI release 2016.09” standard image running Centos7.

A. At this point, your MongoDB database should already be installed with the service not yet running.

[ec2-user@SQ-MGODBMP etc]$ service mongod status
\\verify service is stopped if not stop it. service mongod stop
\\incase you have systemctl, replace command accordingly.

B. As best practice suggests when creating a replica set for Production, at a minimum ensure you have 3 instances ready for replication as well as a separate mounted disk for your individual MongoDb data directory. This improves performance. So mount your disk, partition using the appropriate tool, Gdisk/Fdisk, create your custom data directory and add it to your fstab.

mkdir /mnt/mongodb/
mount disk fdisk /dev/XXXX
blkid \\copy id and add to fstab.
cat /etc/fstab

\\should look look something like this:
  UUID="f54f7034-c7c9-4a51-9088-e70f6b42981b" /mnt/mongodb/ ext4 defaults,noatime 0 0
  sysfs       /sys        sysfs   defaults        0   0
\\MANDATORY: even if no disks were mounted, still change the permissions.
\\As root ofcause, alter partition data directory properties.
chmod 775 /mnt/mongodb/
chown -R mongod:mongod /mnt/mongodb/

C. Add your replication set name to your MongoDB configuration file.

[ec2-user@SQ-MGODBMP ~]$ vim /etc/mongod.conf
\\uncomment and update the replication section.
  replSetName:  Starktower

D. And firewall, this one I banged my head agains’t the wall a few times. Regardless of whether firewallD is installed or not, Mongo still requires you grant internal route access to specific services internally to the machine in question.

\\will show you whats presently granted or denied.
[root@SQ-MGODBMP ~]$ iptables -L
\\as root add this entry to your route table.
iptables -A INPUT -p tcp --dport 27017 -j ACCEPT

E. Set service to restart on reboot for high availability.

\\should read on between [2-5]. If not $chkconfig mongod on
\\systemctl enable alternative incase.
[root@SQ-MGODBMP ~]# chkconfig mongod --list
mongod         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

And those are some of the simple thing’s to consider when setting up database replication for MongoDB. This could have been on an on-premise set of servers, a cloud environment or even a local vagrant development environment. Either way they have helped consistently set up replication when required.

Thanks for reading and if you have questions or need help let us know.

Merry Christmas.