Setting Replica Set with Multiple MongoDB Servers

By | August 26, 2015

Replica set is a term, used for defining a DB cluster of multiple nodes inclusion, with the master-slave replication and an automated failover set between them. Such a structure usually requires an uneven number of members to ensure the correct primary (master) database’ election. This selected DB will process all the incoming write operations, storing the information about them within its oplog, where they can be accessed and replicated by every secondary (slave) replica member for being applied to their data sets. In such a way, all of the servers will represent the same content and ensure its availability.


In case some unexpected issue occurs, causing the primary database downtime (e.g. due to hardware failure or connection interruption), a new election process will be initiated automatically, helping to restore the normal application functioning, without any manual intervention required. In such a way, replica set inherits the benefits of usual replication (like failover redundancy, increased data availability and read capacity, disaster recovery, etc) and simultaneously eliminates the complexity of managing numerous databases separately.

Thus, here is a simple instruction, that will show you how to create and configure a MongoDB replica set with three members – such a complexion is considered to ensure enough margin of information safety and sufficient out-turn to handle the required amount of I/O operations, for most of the commonly used applications. So, proceed with performing the instructions in the following sections one-by-one, where we’ll discover how to prepare the appropriate environment, set an authentication between DB nodes, configure the replication itself and make sure everything is tuned properly.

Create an Environment

To start with, you’ll need at least three MongoDB nodes in order to configure our replica set, so let’s create such an environment (or you can adjust your existing one with the Change environment topology button).

In this example, we’ll allocate MongoDB instances of the 3.0 version (the 2.6 one can be used similarly) within the confines of the same topology. However, generally, they can belong to different environments, still being operated as intended below.


The creation process will take a while, so you can proceed to the next section at that time.

Add Authentication Key File

Authentication is an important security assurance process, that forces each member of the replica set to identify itself during the inner communication by means of a special unique authentication keyfile. So let’s generate your own in order to protect the data inside your DBs from illegal access.

  1. Install the OpenSSL utility at your machine in case you haven’t done this yet and run it.
  1. Depending on the OS you are using, execute one of the following commands to generate a new key file (with the preferable size in bytes, e.g. 741):
  • for Windows

          rand -base64 741 -out {file_name}.key

  • for Linux/MacOS/FreeBSD

          openssl rand -base64 741 -out ~/{file_name}.key

Here, the {file_name} placeholder should indicate the name of your key file (mongo-set.key in our case), which will be saved either into the home folder of your Unix-based OS or at the actual OpenSSL directory if using Windows.

  1. Now you need to upload your just generated keyfile to all of your MongoDB servers.


Thus, switch to the Jelastic dashboard and click the Config button next to your DB node(s).

  1. In the opened configuration tab, hover over the keys directory and click on the appeared Upload button.


Locate your keyfile within the appeared Open file window and select it for being uploaded.

  1. Repeat the last two steps for all of the MongoDB nodes you’d like to include to your replica set.
Note: In case all of your DB servers are located within the same environment (just as in our case), you can switch between them by means of the Node ID option at the top pane, choosing the required instance within the expanded drop-down list: image04

Configure the MongoDB Replication

Since one of the main data management principles – i.e. its security – is ensured, you can finally proceed to the replica set configuration itself:

  1. Switch to the mongod.conf file inside the etc folder within the same configuration tab for MongoDB nodes. Then, scroll down to the Replication Options section (at the 68th line in our case), where you need to uncomment and edit the following parameters:
  • replSetspecify the unique name for your replica set (db-replication in our case)
  • keyFile – paste path to the key file you’ve uploaded in the previous guide section (i.e. /var/lib/jelastic/keys/mongo-set.key)


Use the appropriate option above the editor window in order to instantly Save your changes for all instances for more convenience.

  1. Now, Restart your DB nodes for the new configuration parameters to be applied.


  1. Next, enter the MongoDB server, that you consider to initially become the primary one, via the SSH protocol.
Note that after the primary database is elected, other replica set members will become inaccessible for the direct write operations, which means that any changes, configurations etc (including access to the web admin panel) can be performed for the current master node only.


  1. Access the database, which should be replicated, with the appropriate admin user credentials. Use the following string for that:

mongo -u {user} -p {password} {DB_name}



  • {user} – administrator username (was sent to you via email – usually the admin one is used by default for all MongoDB nodes at Jelastic)
  • {password} – password for the corresponding DB user password (can be found within the same email)
  • {DB_name} – name of the database you would like to replicate within this replica set (we’ll use the default admin one)
  1. Once the connection is established, execute the next lines (one by one) in order to initiate your replica set and define the parameters for the current MongoDB node:

config = {_id : “{replica_set}“, members : [{_id : 0, host:”{current_db_ip}:27017″},]}



Obviously, the values in brackets should be substituted with the appropriate data, namely:

  • {replica_set}name of your replicating DB group, specified at the beginning of this section (db-replication in our case)
  • {current_db_ip} – IP address of the chosen DB container. It can be seen via terminal during the node selection step in the LAN IP column or just at the Jelastic dashboard within the additional options list:


  1. Now, fill your replica set with the remained databases by typing the following line for each of them, changing the {db_ip} value to the corresponding IP addresses:



After you’ve added all the rest of the replication members (two more in our case), you’ll get a fully functional replica set.

Tip: In case you’d like to ensure everything is configured properly, you can execute the rs.status() command to get the full information regarding your replica set.
image16 If everything is alright, you’ll see the result similar to the one shown above.

DB Cluster Availability Check-Up

Our configured advanced MongoDB cluster allows you to connect and perform different operations with it remotely. As an example, let’s get its actual state by connecting and executing a few check up commands by means of a simple PHP applet.

Obviously, you’ll need an application server for that (e.g. Apache), so either add one to your environment (as we did) or just create it within a separate environment and proceed with the steps below:

  1. Open the Configuration Manager tab for the added Apache server by selecting the Config icon next to it.


  1. Navigate to the webroot/ROOT directory, open the index.php file and paste the following piece of code instead of its default content:

    $options = array(‘replicaSet’=> “{rset_name}”,
    “username” => “{user}” ,
    “password” => “{db_password}”
        $m = new
        MongoClient(“mongodb://mongodb{NodeID}–{environment_domain}:27017,mongodb{NodeID}–{environment_domain}:27017,mongodb{NodeID}–{environment_domain}:27017/”, $options);
        $hosts = $m->getHosts();
        } catch (Exception $e){
            echo $e->getMessage();


    where the following values should be substituted with the corresponding data:

  • {rset_name} – your replica set name
  • {user} – admin user of the chosen primary DB (admin by default)
  • {db_password} – the above specified user’s password
  • {NodeID} – identification number of the corresponding node, that can be found at the Jelastic dashboard


Note  that you need to specify the ID of every node, included to your replica set, within the appropriate MongoClient section.
  • {environment_domain}domain name of the corresponding MongoDB node (the same for all of them in our case)

As a result, you’ll get the similar set of strings:image07

Don’t forget to Save this file.

  1. Apache requires a special module for being able to interact with the MongoDB server, so you need to add it within configs.

For that, move to the etc folder and open the php.ini file. Find the [mongo] section (approximately at the 437th line) and remove the semicolon before the line to enable this extension.


  1. In order to apply new configurations, click on Save above the editor window and hit the Restart node button next to your application server.


  1. Finally, click the Open in Browser icon nearby.


As a result, within a new browser tab, you’ll see information about your replica set members and their accessibility.


The first line shows whether the connection was established successfully: [connected] => 1 (or 0 in case this failed for some reason). As for the replica set participants themselves, the next information is presented:

  • host – a particular database’ IP address
  • port – current replication member port
  • health – indicates whether the corresponding DB server is up and running (1) or it’s currently down (0)
  • state – marks instance status with the appropriate value: 1 – for the primary one, 2 – for secondaries

In addition, you can try to start and stop any of your database nodes and refresh this page to track the changes. In such a way, you can make sure that your MongoDB cluster is available and works as intended, and thus can be applied for any real case right now!

If you would like to test how to create and configure a MongoDB replica set with Jelastic, simply sign up for a 2-week-free-trial and share your experience in the comments below.

Related Articles:

MongoDB Backup in the Cloud
MongoDB Master Slave Replication

Subscribe to get the latest updates