Configuring Mongodb 3.4 Replica Set On Ubuntu 16.04
If you have decided to use Mongodb as your database you primarily have 2 options to run it on production. Either use Database as a Service Solution or host your own cluster. Atlas is a fully managed mongodb solution with which you can deploy, operate, and scale a MongoDB database in the cloud. If you decide to manage your own cluster you must run it as a replica set.
Replica sets provide redundancy and high availability, and are the basis for all production deployments. They can also be used to scale read capacity so that heavy read operations like data analytics etc can be done on secondary servers.
Getting Mongodb up and running is a fairly easy setup. But it’s hard to find to all the pieces in one place to run a replica set configuration. This article aims to be one place for getting all the information required to setup mongodb in a replica set mode from scratch. There is a vagrant github repo at the bottom of the article to run mongodb out of the box.
This article walks through the installation of Mongodb 3.4 running in PSS (Primary, Secondary, Secondary) configuration on Ubuntu 16.04.
Installing MongoDB
The following code snippet installs the latest stable version of MongoDB 3.4 on Ubuntu 16.04. Install on other OS.
Creating directories and configuration files for each node in replica set
You will need a configuration file to run each mongod process and a directory to store mongodb data for each node in the replica set. Setup configuration files and directories for all the nodes in the cluster. The configuration for each node will look like this.
Securing Your Installation
Creating Users and Enabling Authorization
Roles grant users access to MongoDB resources. MongoDB provides a number of built-in roles that administrators can use to control access to a MongoDB system. We will setup a root role on admin database which would have access to all the resources.
Enforce Keyfile Access Control in a Replica Set
With keyfile authentication, each mongod
instances in the replica set uses the contents of the keyfile as the shared password for authenticating other members in the deployment. Onlymongod
instances with the correct keyfile can join the replica set.
Bind the database IP’s to required servers
In local installation you might use 0.0.0.0 as bindIp parameter in configuration file but in production environment you would want only selective ip’s to have access to the servers. Your network configuration might look like this in production whitelisting ip’s of only selected servers.
bindIp: 172.0.0.10, 172.0.0.11, 172.0.0.12
Initializing the replica set
Now the configuration files are ready, users have been created and keyfile access is set between different nodes in the replica set its time to initiate the replica set. Open the primary server to setup replication. Authenticate and use rs.initiate() to start replication.
This is it. You are all setup with a fairly secure mongodb replica set. As promised here is the link to the Vagrant Image to run above configuration out of the box.
Thanks!