MongoDB Master Slave Replication

By May 22, 2013 HowTo 6 Comments
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone

As we’ve already told you in our previous post about MySQL 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, your customers will experience improved availability of replicated data. If data from one database server is unavailable, clients 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.

Create master environment

First of all we create an environment in Jelastic for our master database.

1. Go to and sign up if you haven’t done it yet or log in with your Jelastic credentials by clicking the Sign In link on the page.

2. Ask Jelastic to create a new environment.


3. In the Environment topology dialog pick MongoDB as a database you want to use. Set the cloudlet limit and type the name of your first environment, for example, master.


Wait just a minute for your environment to be created.


Generate private key

You have to generate your own private key in order to protect the data on master and slave databases from illegal access.

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

2. Run OpenSSL using command line (C:openssl.exe on Windows,~/ on Linux), generate your private key (e.g. 753 bit) and save it as a .key file:

...\OpenSSL\bin>openssl.exe rand -base64 753 > mykey.key

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

Configure master database

Let’s configure master base now.

1. Click config button for your master database.


2. Upload the key file, you’ve generated earlier to the keys folder.


3. Navigate to mongod.conf file and specify the next configuration:

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


4. Save the changes and restart MongoDB in order to apply the new configuration parameters.


Create slave environment

It’s time to create the environment for our slave database now. For this purposes you can easily clone your master’s environment to save even more time. Cloned environment is identical to the original environment including all data, configurations etc.

1. Click the Clone button next to the master environment.


2. Specify the name for the copy and confirm your choice. Let’s name it slave.


Now you have two identical environments with two databases.


Configure slave database

Let’s configure our slave base now.

1. Click config button for your slave database.

2. Navigate to mongod.conf file and ensure that the following strings are uncommented/added:

auth = true
slave = true
source = {master_base_IP_address}
master = true 
keyFile = /var/lib/jelastic/keys/{your_key_file_name}


In this case we use parameter master=true to avoid the issue with access to collections on slave node in RockMongo.

4. Save the changes and restart your slave database server in order to apply the new configuration parameters.

Check the results

We have to ensure now that master-slave replication works for our databases.

1. Click Open in browser button for your master database.


2. Log into RockMongo admin page using the credentials which Jelastic sent you when you created the environment for your master database.

3. Create the new database (e.g. Jelastic) with at least one collection in your master base.


4. Navigate to your slave base admin page and you’ll see that the new database was successfully replicated.


As you can see MongoDB master slave replication works nicely in Jelastic! MongoDB rocks! It hits a very sweet spot between SQL and NoSQL technologies.

That’s it for now. I’ll talk more about how to setup MongoDB replication via Replica Sets in my future posts. Stay tuned!

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone


Leave a Reply

Subscribe to get the latest updates