MongoDB Replication and Automated Failover: Configuration Guide
A replica set refers to a database cluster composed of multiple nodes that have replication and automated failover configured among them. Typically, a replica set requires an odd number of members, optionally including an Arbiter node, to ensure the correct selection of the PRIMARY database.
This primary database handles all incoming write operations, storing them in its log. These operations can then be accessed and replicated by each SECONDARY member to their datasets, ensuring all servers have the same data and maintain its availability.
If an unexpected issue occurs, causing the primary database to go down (e.g., due to hardware failure or connection interruption), an automatic election process begins to restore normal operation without manual intervention.
This setup benefits from replication (such as failover redundancy, increased data availability and read capacity, disaster recovery, etc.) while simplifying the management of multiple databases.
Here is a straightforward guide on creating and configuring a MongoDB replica set with three members.
This configuration is considered sufficient for most applications, providing a balance of data safety and performance to handle the necessary I/O operations. We will cover preparing the environment, setting up authentication between DB nodes, configuring replication, and ensuring everything is correctly tuned.
Create an Environment
To begin, you need at least three MongoDB nodes to set up a replica set. In this example, we’ll use MongoDB instances version 7.0.5 within a single environment.
If needed, change the Environment Name and destination Region. Once the installation is complete, ensure the security of node communication using an authentication key file.
Adding an Authentication Key File to Your Database
Securing your database communication is crucial. One way to do this is by using an authentication key file, ensuring that each member of your replica set can identify itself. Follow these steps to generate and distribute an authentication key file to protect your data from unauthorized access:
Step 1. Log into one of the database nodes using Web SSH.
Step 2. Use your own key file, or generate one using OpenSSL with the following command (using a key size of 741 bytes, for example, and naming it “my.key”):
openssl rand -base64 741 > my.key
Step 3. Distribute the newly created key file to all MongoDB instances:
- Click the Config button next to your database nodes to access the File Manager.
- In the configuration tab that opens, locate the my.key file at /home/jelastic/my.key and rename it with the mongodb.key name.
Configure the MongoDB Replication
Once you’ve secured everything as a key principle of data management, you can move on to configuring your replica set.
Step 1. Head over to the mongod.conf file inside the etc folder within the MongoDB nodes section. Scroll down to the replication section, then uncomment it and add a unique name for your replica set, such as rs0.
replSetName: rs0
Step 2. Next, include the keyFile parameter in the security section. Specify the path to your key file, like /home/jelastic/mongodb.key
Step 3. Click the appropriate button in the editor window to instantly Save all changes.
Step 4. Restart your database nodes to apply the new configuration parameters.
Step 5. Connect to the MongoDB server you want to designate as PRIMARY using the SSH protocol.
Step 6. Access the replicated database with the appropriate admin user credentials:
mongo -u {user} -p {password} {DB_name}
Where:
- {user}: Administrator username (typically sent to your email and often defaulted to admin).
- {password}: Password for the administrator user (sent to your email as well).
- {DB_name}: The database you want to replicate within the replica set (usually the default admin database).
Step 7. Once the connection is established, proceed with defining parameters for the current MongoDB node and initiating your replica set:
config = {_id : "{replica_set}", members : [{_id : 0, host:"{current_db_ip}:27017"},]}
rs.initiate()
Be sure to replace the placeholders with the appropriate data:
- {replica_set}: The name of your replicating database group, as indicated at the start of this section (in our example, “db-replication”).
- {current_db_ip}: The IP address of the chosen database container.
For instance:
config = {_id : "rs0", members : [{_id : 0, host:"10.100.3.31:27017"},]}
Then run the following command next to the admin> to initiated this config settings
rs.initiate()
Step 8. Run the following command for the remaining databases, replacing {db_ip} with the IP address of each database:
rs.add("{db_ip}:27017")
Step 9. Once you’ve added all replication members, your replica set should be fully operational. If you want to confirm everything is configured correctly, execute the rs.status() command to view a comprehensive report on your replica set.
ReplicaSet Arbiter
For optimal reliability in a replica set, it’s best to have an odd number of members. If you’re using an even number of nodes, consider adding an Arbiter node. This lightweight process helps maintain a quorum by responding to heartbeat and election requests from other replica set participants.
- The Arbiter does not store data and only votes in elections when a node fails.
- It is a lightweight process, consuming minimal resources.
- The Arbiter facilitates the exchange of encrypted user credentials between a set of replicas.
- It is advisable to deploy the Arbiter on a separate node to ensure high availability.
Here’s a step-by-step guide to adding an Arbiter to your replica set:
Step 1. Expand your database cluster horizontally with one new node.
Step 2. In the /home/jelastic directory, create a key file named mongodb.key and copy the contents from a similarly configured database node.
Step 3. Update the mongod.conf configuration file:
Uncomment the replication section and Add a replSetName (e.g., replSetName:rs0).
Include a keyFile parameter in the security section pointing to /home/jelastic/mongodb.key
Step 4. Restart the newly added node to apply these configuration settings.
Step 5. Add the Arbiter to the replica set on the PRIMARY node by issuing the command:
rs.addArb("{db_ip}:27017")
Replace {db_ip} with the IP address of the newly added node.
Step 6. Verify that the new node has indeed been added as an Arbiter. To do this, log in to the new node via SSH and connect to the MongoDB instance with credentials from the email you received upon node creation.
The newly added node will then act as the Arbiter for the rs0 replica set, ensuring quorum in any situation.
Database Cluster Availability Testing
Our advanced MongoDB cluster setup lets you connect and execute various operations remotely. For instance, you can check its current state by connecting and running a few monitoring commands via a simple PHP app.
You’ll need a web server, like Apache, to make this work. If you haven’t set one up yet, add it to your environment, or create it in a separate environment.
Step 1. Click the Change Environment Topology button and include the server.
Step 2. Open the Configuration Manager tab for Apache by clicking on the Config icon next to it.
Step 3. Navigate to /var/www/webroot/ROOT, open the index.php file, and replace its default content with the following code.
<?php
try{
$mongodbConnectionURI= "mongodb://{db_username}:{db_password}@node{NodeID}-{environment_domain}:27017,node{NodeID}-{environment_domain}:27017,node{NodeID}-{environment_domain}:27017,node{NodeID}-{environment_domain}:27017/?replicaSet={replica_set_name}&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'<br><br>';
var_dump($manager->getServers());
} catch (Exception $e){
echo $e->getMessage();
}
?>
Make sure to substitute the placeholders with the relevant data:
- {replica_set_name} – Your replica set name
- {db_username} – Admin user of the chosen primary database (default is admin)
- {db_password} – Password for the admin user
- {NodeID} – Identification number of the corresponding node from the Jelastic dashboard
- {environment_domain} – Environment domain from the Jelastic dashboard
Consequently, you’ll obtain a comparable set of strings:
Don’t forget to Save the file.
Step 4. Apache requires a special module to connect to MongoDB, so you’ll need to enable it in the php.ini file in the etc directory. Remove the semicolon before extension=mongodb.so to activate it.
Step 5. After saving, hit Restart Nodes next to your web server to apply the new configurations.
Step 6. Finally, click the Open in Browser icon nearby.
In a new browser tab, you’ll see details about your replica set members and their accessibility.
The first line shows the result of checking the availability of the replica set, which is executed by the “ping” command (line 6 of index.php).
object(stdClass)#11 (3) { ["ok"]=> float(1)
This confirms that the replica set has been successfully tested.
In the next section of the output, you’ll find a detailed list of replica set hosts, which is retrieved using the getServers function on line 11 of index.php. For instance, you can observe some of the values that are assigned during the creation of the replica set:
- Host: This is the IP address of the specific database.
- Port: This is the port used by the current replication member.
- [“is_primary”] and [“is_secondary”]: These parameters indicate the server’s status. For the primary MongoDB server, the values are true and false; for the other two MongoDB servers, the values are false and true.
Additionally, you can start or stop any of your database nodes and refresh this page to monitor the changes. This way, you can confirm that your MongoDB cluster is up and running as expected, making it suitable for any real-world application.
Jilesh Patadiya, the visionary Founder and Chief Technology Officer (CTO) behind AccuWeb.Cloud. Founder & CTO at AccuWebHosting.com. He shares his web hosting insights on the AccuWeb.Cloud blog. He mostly writes on the latest web hosting trends, WordPress, storage technologies, and Windows and Linux hosting platforms.