MongoDB Replica Set with Master-Slave Replication and Automated Failover

A replica set is a group of MongoDB database nodes used for defining database clusters with master-slave replication and automated failover. A replica set contains a single primary node, multiple secondary nodes, and an arbiter node. The primary node receives all write operations while other nodes apply operations from the primary so that all nodes have the same data set.

If your primary node breaks due to hardware failure or system-related issues, the replica set members autonomously select a new primary node, and your application functioning without manual intervention.

In this post, we will explain how to set up a MongoDB replica set with three nodes. We will also show you how to create an environment, configure authentication between multiple database nodes and finally configure the replication between all nodes.

Create a MongoDB Environment

First, log in to RoseHosting Cloud platform at https://app.rosehosting.cloud as shown below:

mongodb replica set and automated failover guide

Click on the NEW ENVIRONMENT to create a new MongoDB environment as shown below:

mongodb replica set with master-slave replication tutorial

Click on the PHP tab, Select the appropriate MongoDB version, Select reserved cloudlets, Add three MongoDB instances in Horizontal scaling, Define your environment and click on the Create button. Once the MongoDB environment has been created, you should see the following screen:

how to setup mongodb replica set with master-slave replication

Add MongoDB Authentication Key File

Next, you will need to generate and add a MongoDB authentication key to secure the communication between all nodes.
Follow the below steps to generate and add the MongoDB key:

Step 1: Click on the Web SSH to log into the NOSQL Database node:

setting up mongodb replica set and automated failover

Step 2: Now, run the following command to generate a key and store it in the file named my.key:

openssl rand -base64 741 > my.key

step-by-step guide to mongodb replica set with master-slave replication and automated failover

Step 3: Next, you will need to copy the generated key to all database nodes. To do so, click on the Config button to open the File Manager as shown below:

guide to mongodb replica set with master-slave replication and automated failover

tutorial for mongodb replica set with master-slave replication and automated failover

Step 4: Here, you will need to open the my.key file located at /home/jelastic/my.key. You should see the content of the my.key file as shown below:

ultimate guide to mongodb replica set with master-slave replication and automated failover

Step 5: Now, copy its content to the clipboard. Then, navigate to the /var/lib/jelastic/keys directory as shown below:

easy guide to mongodb replica set with master-slave replication and automated failover

Step 6: Now, create a new file named mongo-set.key and paste the content from the clipboard as shown below:

easy steps for mongodb replica set with master-slave replication and automated failover

Step 7: Now, click on the Save => Save for all instances to apply the changes.

At this point, mongo-set.key will be distributed across all nodes.

Configure MongoDB Replication

Next, you will need to edit the mongod.conf file and enable the replication and define the path of the mongo-set.key.

Follow the below steps to configure MongoDB replication.

Step 1: Edit the /etc/mongod.conf file within the same configuration tab as shown below:

easy tips on mongodb replica set with master-slave replication and automated failover

Specify the unique name for your replica set, specify the path to your key file then click on the Save => Save for all instances to save the changes.

Step 2: Next, click on the Restart Nodes button to start your database nodes to apply the changes.

easy tutorial for mongodb replica set with master-slave replication and automated failover

Step 3 : Now, click on the Web SSH to access the first database node that you want to use as a PRIMARY as shown below:

how to configure mongodb replica set with master-slave replication and automated failover

Step 4: Now, connect to the MongoDB database which should be replicated using the appropriate admin credentials:

mongod -u username -p password dbname

easy configuration for mongodb replica set with master-slave replication and automated failover

where:

username : MongoDB admin username sent to your email address.
password : Password for the admin user sent to your email address.
dbname : Name of the MongoDB database that you want to replicate.

Step 5: Once the connection has been established, find the IP address of the first database node as shown below:

mongodb replica set with master-slave replication and automated failover easy guide

Then, run the following command to define parameters for the current MongoDB node:

config = {_id : "db-replication", members : [{_id : 0, host:"10.100.2.38:27017"},]}
mongodb replica set with automated failover complete guide

Where :

10.100.2.38 is the IP address of the first database node.
db-replication is the name of the replica set which you have defined in the mongod.conf file.

Step 6: Next, initiate the replica set with the following command:

rs.initiate()
mongodb replica set with master-slave replication easy tutorial

Step 7: Next, run the following command for the remaining two database node:

rs.add("10.100.2.36:27017")
rs.add("10.100.2.37:27017")
mongodb replica set with automated failover ultimate guide

Where:

10.100.2.36 is the IP address of the second database node.
10.100.2.37 is the IP address of the third database node.

Step 8: After adding all database node members, run the following command to get detailed information of your replica set.

rs.status()

mongodb replica set with automated failovermongodb replica set with automated failover configuration guide configuration guide

MongoDB Replica Set Arbiter

If you have configured an odd number of members in the replica set then it would be more reliable. Otherwise, it is recommended to add an arbiter node to maintains a quorum. Arbiter is used to exchanges user credentials between all replicas. It is a good idea to run the arbiter on a separate node.

Follow the below steps to add an arbiter node to the replica set:

Step 1: Edit your MongoDB environment as shown below:

mongodb replica set with automated failover step-by-step tutorial
mongodb replica set with master-slave replication and automated failover step-by-step guide

Add one extra node to the database cluster horizontally then click on the Apply button to recreate an environment.

Step 2: Once the environment has been created. You should see a fourth database node on the following page:

mongodb replica set with master-slave replication and automated failover easy configuration

Step 3: Click on the config button within the fourth node, create a key file mongo-set.key and paste the same content from the other database node.

mongodb replica set with master-slave replication and automated failover instructions

Click on the Save button to save the changes.

Step 4: Edit the mongod.conf file and define the replica set name and keyfile path as shown below:

mongodb replica set with master-slave replication and automated failover instructional guide

Click on the Save button to save the changes.

Step 5: Restart the fourth node to apply the configuration changes.

mongodb replica set with master-slave replication and automated failover easy instructions

Step 6: Connect to the first database node with Web SSH and run the following command to add the arbiter node to the cluster:

rs.addArb("10.100.2.39:27017")

instructions for mongodb replica set with master-slave replication and automated failover

Where:

10.100.2.39 is the IP address of the fourth node.

Step 7: Next you will need to check whether the fourth node became an arbiter or not. To check it, connect to the fourth node with Web SSH and connect to the MongoDB with the following command:

mongod -u username -p password dbname

easy instructions for mongodb replica set with master-slave replication and automated failover

As you can see, the fourth node acts as the arbiter of db-replication.

Test MongoDB Cluster Availability

In this section, we will check whether MongoDB cluster availability functioning or not. To do so, you will need to add an application server to the MongoDB environment.

Step 1: Click on the Change Environment Topology button to edit the MongoDB environment.

instructional guide for mongodb replica set with master-slave replication and automated failover

mongodb replica set with master-slave replication complete instructions

Add the Apache application server and click on the Apply button to re-create the environment.

Step 2: Once the application server is added to the environment, click on the config tab of the Apache server.

complete instructions for mongodb replica set with master-slave replication

Step 3: Navigate to the /var/www/webroot/ROOT directory, edit the index.php file, remove the default content and add the following content:

<?php
    try {
        $mongodbConnectionURI= "mongodb://dbuser:dbpassword@firstnodeid-environmentname:27017,secondnodeid-mongo.rosehosting.us:27017,thirdnodeid-environmentname:27017,fourthnodeid-environmentname:27017/?replicaSet=replicasetname&readPreference=primary";
    $manager = new MongoDB\Driver\Manager($mongodbConnectionURI);

    $command = new MongoDB\Driver\Command(['ping' => 1]);
    $cursor = $manager->executeCommand('db', $command);
    $response = $cursor->toArray()[0];
    var_dump($response);
    echo'

'; var_dump($manager->getServers()); } catch (Exception $e){ echo $e->getMessage(); } ?>

step-by-step instructions for mongodb replica set with master-slave replication and automated failover

Click on the Save button to apply the changes.

Where:

dbuser is the admin user of the first node.
dbpassword is the password of the first node user.
firstnodeid is the ID of the first node.
secondnodeid is the ID of the second node.
thirdnodeid is the ID of the third node.
fourthnodeid is the ID of the fourth node.
environmentname is the name of your environment.
replicasetname is the name of the replica set that you have defined in the mongod.conf.

step-by-step instructions for mongodb replica set with master-slave replication

Step 4: Next, you will need to define the mongodb extension in the php.ini file to interact Apache with the MongoDB server. To do so, navigate to /etc/ directory, edit the php.ini file, find the [mongodb] section, and remove the semicolon before the extension=mongodb.so line to enable this extension as shown below:

instructions for mongodb replica set with master-slave replication

Click on the Save button to save the changes.

Step 5: Now, restart the Application server node to apply the changes.

easy instructions for mongodb replica set with master-slave replication

Step 6: Now, click on the Open in Browser button to open your application in a web browser.

instructional guide for mongodb replica set with master-slave replication

You should see the detailed information about your replica set nodes in the following page:

mongodb replica set with master-slave replication step-by-step guide

You can see the IP address and port of each node with primary, secondary, and arbiter node information in the above image. That means the replica set is functioning properly.

With our managed cloud hosting, you don’t need to do any of this configuration, simply ask our technical support guys and they will set up MongoDB Replicate Set with Master-Slave Replication and Automated Failover, for you and free of charge. They are available 24/7.

Leave a Comment