Installation, setup and HA on mongo dB

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.

I wont go deep into explaining the different parts of mongodb. But i just have a quick agenda here for the below.

Again most of my post are based on video. So if you want to have a full video on this, you can look at the video from this site or access through YouTube.


Configure repository.

Create an /etc/yum.repos.d/mongodb-enterprise-4.4.repo file so that you can install MongoDB enterprise directly using yum:

[mongodb-enterprise-4.4]name=MongoDB Enterprise Repositorybaseurl=$releasever/mongodb-enterprise/4.4/$basearch/gpgcheck=1enabled=1gpgkey=

NOTE If you have a mongodb-enterprise.repo file in this directory from a previous installation of MongoDB, you should remove it. Use the mongodb-enterprise-4.4.repo file above to install MongoDB 4.4.

You can also download the .rpm files directly from the MongoDB repository. Downloads are organized by Red Hat / CentOS version (e.g. 7), then MongoDB release version (e.g. 4.4), then architecture (e.g. x86_64). Odd-numbered MongoDB release versions, such as 4.4, are development versions and are unsuitable for production deployment.2

Install the MongoDB Enterprise packages.

Install MongoDB Enterprise 4.4.

Issue the following command:

sudo yum install -y mongodb-enterprise

Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers. Replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.

Why Replication?

  • To keep your data safe
  • High (24*7) availability of data
  • Disaster recovery
  • No downtime for maintenance (like backups, index rebuilds, compaction)
  • Read scaling (extra copies to read from)
  • Replica set is transparent to the application

How Replication Works in MongoDB

MongoDB achieves replication by the use of replica set. A replica set is a group of mongod instances that host the same data set. In a replica, one node is primary node that receives all write operations. All other instances, such as secondaries, apply operations from the primary so that they have the same data set. Replica set can have only one primary node.

  • Replica set is a group of two or more nodes (generally minimum 3 nodes are required).
  • In a replica set, one node is primary node and remaining nodes are secondary.
  • All data replicates from primary to secondary node.
  • At the time of automatic failover or maintenance, election establishes for primary and a new primary node is elected.
  • After the recovery of failed node, it again join the replica set and works as a secondary node.

A typical diagram of MongoDB replication is shown in which client application always interact with the primary node and the primary node then replicates the data to the secondary nodes.

MongoDB Replication

Replica Set Features

  • A cluster of N nodes
  • Any one node can be primary
  • All write operations go to primary
  • Automatic failover
  • Automatic recovery
  • Consensus election of primary

Set Up a Replica Set

In this tutorial, we will convert standalone MongoDB instance to a replica set. To convert to replica set, following are the steps −

  • Shutdown already running MongoDB server.
  • Start the MongoDB server by specifying — replSet option. Following is the basic syntax of –replSet −

Force a Member to be Primary Using Database Commands

Consider a replica set with the following members:

  • – the current primary.
  • – a secondary.
  • – a secondary .

To force a member to become primary use the following procedure:

  1. In a mongo shell, run rs.status() to ensure your replica set is running as expected.
  2. In a mongo shell connected to the mongod instance running on, freeze so that it does not attempt to become primary for 120 seconds.
  1. In a mongo shell connected the mongod running on, step down this instance that the mongod is not eligible to become primary for 120 seconds:
rs.stepDown(120) becomes primary.

Download and Install Compass

To download Compass on Linux systems, use wget.NOTE

Alternatively, you can download Compass from the MongoDB downloads page.

  1. Download MongoDB Compass wget
  2. Install MongoDB Compass sudo yum install mongodb-compass-1.26.1.x86_64.rpm
  3. Start MongoDB Compass mongodb-compass

About the author

User Avatar


Hi, I'm Benson Yerima, a database administrator with an obsession for all things tech. This blog is dedicated for helping people learn about database technology.

View all posts