MongoDB Master Slave Replication

The database replication offers various benefits depending on its type and the options you choose, but the common benefit of replication is the availability of data when and where it is needed. As a result, you will experience improved availability of replicated data. If data from one database server is unavailable, customers can still access the replicated copy of the data.

Now let's examine a simple example on how to configure master-slave replication on MongoDB.

You are able to set a DB cluster in a two ways:

One-click Installation

You can get a MongoDB cluster with two databases up and running within minutes using Jelastic one-click install option.




The process is simple.

Just click Get it hosted now and type your email. You’ll get an environment with replicated master and slave databases just in a minute, skipping the steps of manual installation.

Note that current widget is provided for beta testing of automated Master-Slave DB replication adjustment. Therefore you should take into consideration a few peculiarities it has:

  • after the environment creation you’ll receive just one email with admin credentials to your master database
  • slave DB can be accessed only via SSH protocol using master database credentials


The complete and full-featured one-click solution will be provided in the nearest future.

Manual Installation

If you prefer to configure the Master-Slave replication manually in order to get more slave replicators or specify some custom configurations, please, follow the instruction below.

Create Environments

To begin with, we’ll create two identical environments with MongoDB server in each of them. Let’s start with master environment creation.

1. Log into your dashboard and ask Jelastic to create a new environment.

2. In the Environment topology dialog pick MongoDB as a database you want to use (it can be found in the NoSQL databases drop-down list). Set the cloudlet limits for this node, type the name of your first environment (for example, mongo-master) and confirm the creation.

3. Wait a minute until this process is completed.

4. Then create one more environment with MongoDB database in the same way as it is described above or just clone the already existing one, specifying the desired environment’s name (e.g. mongo-slave) in the appropriate frame.

MongoDB in this environment will be your standby database.

In case of using cloning for the second environment creation, it will be identical to the original one, including all data, configurations etc. Therefore credentials for admin access to the second DB will be the same as for the first one.

For now you have two identical environments with MongoDB database node in each one.

Edit the Privileges

The next step is granting the additional privileges to the default admin user on both servers.

1. Access your environment with master DB via SSH and enter the MongoDB container it contains.

In the case you haven’t done the similar operations before, you need to:

2. Once you’ve entered the container, connect to the database via console client using the credentials you’ve previously received via email.

In this example we work with the default admin database. Nevertheless, you can easily create your own one (e.g. with the help of RockMongo admin panel) and specify it in the following parameters for establishing the connection.

mongo -u admin -p {admin_password} {DB_name}
use {DB_name}


Note:

  • values inside the curly brackets should be substituted with the password for admin user and name of the preferred database (admin in our case) respectively.
  • if you are going to work with the same DB you’ve used to establish the connection (in the first string), the second use {DB_name} one is not obligatory.

3. Now let’s assign a root role to the default admin user in order to allow him working with all the databases at the server.

db.grantRolesToUser(
 "admin",
 [
    {
     role: "root", db:"{DB_name}"
    }
 ]
)


4. To make sure the changes were applied, run the following command:

show users

As you can see, everything is correct. Therefore you can log out of the master DB.

5. Now you should enter the MongoDB container in the environment with standby DB server via SSH in the same way you did it for the master one:

6. Once you’re inside, repeat 2-4 steps of the current instruction’s section for the standby DB.

Do not forget that in case you’ve cloned the master environment for getting the slave one, credentials for administrator access to both MongoDB servers are the same.

Fine! Let’s go on.

Generate and Add an SSL Key

Now you have to generate your own private key in order to protect the data on master and slave databases from illegal access. For that perform the following:

1. Install OpenSSL on your computer if you haven't done so yet.

2. Run this tool and generate your private key (with the preferable size in bits, 753 in our case), saving it as a file with .key extension. Depending on the OS you are using, run one of the following lines to perform that:

  • for Windows

...\OpenSSL\bin>openssl.exe rand -base64 753 > {your_key_file_name}.key

Your key file will be located in the OpenSSL/bin folder.

  • for Linux

openssl rand -base64 753 -out ~/{your_key_file_name}.key

A newly generated key can be found in your home folder.

3. Then switch to the Jelastic dashboard and click Config button for your master database.

4. In the opened tab upload the key file you've just created to the keys folder.

5. Just in the same way add this key file to the DB server in your mongo-slave environment.

Now security of your data is ensured! Let’s turn to the adjusting of replication itself.

Configure the Replication

1. Open the Configuration manager for your master DB and navigate to the mongod.conf file. Find the appropriate section (see the image above) and state the next settings:

auth = true
master = true
keyFile = /var/lib/jelastic/keys/{your_key_file_name}.key


2. Save the changes and Restart the node in order to apply new configuration parameters.

3.  Let's configure our slave base now. Open the same mongod.conf file for it and ensure the following strings are uncommented and added:

auth = true
slave = true
source = {master_base_IP}
keyFile = /var/lib/jelastic/keys/{your_key_file_name}.key


Note that {master_base_IP} value should be substituted with the corresponding IP address, which can be easily seen at the dashboard:

4. Again, Save the changes and Restart your slave database server in order to apply the new settings.

NOTE: when clustering mode is applied (by means of adding master = true and slave = true strings to the appropriate servers’ configs) RockMongo administrator panels of the databases become unavailable. This issue will be fixed with the nearest platform update.

If it’s necessary to access your DB via web-interface, just delete the abovementioned settings from the configs and restart your servers. Replication will be disabled in this case.

Check the Results

Everything is configured, therefore now we have to ensure that master-slave replication works for our databases.

1. Access the master DB server via SSH again and connect to your database:

mongo -u admin -p {admin_password} {DB_name}
use {DB_name}


2. Then run the following command in order to add a test record to your DB:

db.{DB_name}.save( {mytestkey:"mytestvalue"} )

You’ll be shown the following output, informing that record is successfully added:

WriteResult({ "nInserted" : 1 })

3. In addition, let’s verify that our test record exists:

db.{DB_name}.find()

As a result, you’ll receive a line with an object ID and key/value you’ve specified:

{ "_id" : ObjectId("5405be9c430c1564c2551b3f"), "mytestkey" : "mytestvalue" }

Log out of the master base.

4. Now enter your slave MongoDB container and connect to the database similarly you did it for master.

mongo -u admin -p {admin_password} {DB_name}
use {DB_name}


5. Run the same
db.{DB_name}.find() command. As you can see, the newly added record has been successfully replicated to your slave database.

Great! Your MongoDB cluster has been configured! Now you can rest assured in the security of your data.