Reader small image

You're reading from  MongoDB High Availability

Product typeBook
Published inJul 2014
Publisher
ISBN-139781783986729
Edition1st Edition
Tools
Right arrow
Author (1)
Afshin Mehrabani
Afshin Mehrabani
author image
Afshin Mehrabani

Afshin Mehrabani is an open source programmer. He is studying to be a computer software engineer. He started programming and web development when he was 12 years old, as well as starting with PHP. Later, he joined the Iran Technical and Vocational Training Organization. He secured the first place and received a gold medal in a competition which was conducted across the entire country in the area of web development. He became a member of the Iran National Foundation of Elite after producing a variety of new programming ideas. He was a software engineer at the Tehran Stock Exchange and is now the head of the web development team in the Yara Company. He cofounded the Usablica team in early 2012 to develop and produce usable applications. He is the author of IntroJs, WideArea, flood.js and some other open source projects. He has contributed to Socket.IO, Engine.IO, and some other open source projects. He is also interested in creating and contributing to open source applications, writing programming articles, and challenging himself with new programming technologies. He has written different articles about JavaScript, Node.js, HTML5, and MongoDB that have been published on different academic websites. Afshin has 5 years of experience in PHP, Python, C#, JavaScript, HTML5, and Node.js in many financial and stock trading projects.
Read more about Afshin Mehrabani

Right arrow

Chapter 5. Replica Set in Action

In the previous chapters, you learned the basic terms and fundamentals needed to work with a replica set network in MongoDB. Now, it's time to examine a replica set in action through a practical example.

In this chapter, we will set up a new replica set from scratch to provide a readily available MongoDB server. Furthermore, we will see the failover process of replica sets in action. You will learn all the steps using screenshots, so it will be easier to follow the procedure.

Overview of replica sets


There are two ways to deploy a replica set, for production or development, and testing. In this chapter, we will deploy replica set for production use. The same procedure is used to deploy a replica set in a development environment, but the requirements are different.

A three-member replica set can provide enough power to overcome network and server issues. To have a smooth election process, it's recommended that you have an odd number of members in a replica set.

The following diagram shows a replica set with three members:

Likewise, for production use of a replica set, each replica set should be hosted on different machines. As you are aware, each machine can face network or hardware failure, so by hosting Missing object type instance on different machines, we can make sure that if a machine fails, we won't lose all the mongod processes.

Before deploying a replica set


There are some steps you need to take before starting a replica set deployment. First of all, you should make sure that you have the latest stable version of MongoDB on each machine.

Note

If you haven't installed MongoDB yet and need help installing MongoDB, read these short instructions at http://docs.mongodb.org/manual/installation/.

Additionally, before deploying a replica set, you must make sure that there aren't any network issues between replica set members. Each machine must see the other, and you must configure the firewall not to prevent connections. In order to have an effective replica set, you should configure the machine so that every member can connect to the other.

Note

If you want a procedure to check the connection between members, visit http://docs.mongodb.org/manual/tutorial/troubleshoot-replica-sets/#replica-set-troubleshooting-check-connection.

Configurations of a replica set


In our example, we will create a replica set using three members on different machines. The following is the configuration of each machine:

  • Mac OS X 10.9.2

  • Ubuntu 12.04

  • Ubuntu 12.04

We have Mac OS X on our host machine, and we use VMware Fusion to run other machines at once. The network connections between members are provided by VMware Fusion. Each mongod object will be hosted on different machines, so we will have three members on three separate machines.

The following scheme shows each machine's hostname:

  • mongod1.replicaset.com

  • mongod2.replicaset.com

  • mongod3.replicaset.com

You can set up hostnames by configuring DNS settings or adding hostnames to the hosts file. This file can be found in the /etc/ folder of the Unix operating systems.

The following diagram shows the member's network and relationships:

It's obvious that according to the situation, secondaries can become primaries. For instance, if mongod2.replicaset.com receives the majority of the votes, it...

Deploying a replica set


Having completed the initialization steps, we can now start to define our first replica set. First of all, you need to run the mongod process on all machines through the following steps:

  1. Make sure that you have the configuration file in your machine, and then run the following command:

    mongod --config /etc/mongodb.conf
    

    Note

    In Unix operating systems, if you don't have sufficient permissions to run the command, you can use the sudo prefix. So the complete command will be sudo mongod --config /etc/mongodb.conf.

  2. After issuing the command, you should see the result depicted in the following screenshot:

  3. Because we set the fork property to true, mongod will run as a daemon in the background. So, in order to see the logs and outputs of mongod, you should use the defined logpath file. You can see the logfile content as shown in the following screenshot:

  4. For the next steps, you should use mongo interactive shell to run and configure the replica set.

  5. Run the mongo command from your...

Failover testing


In this section, we will deactivate the specific nodes to force the replica set to make an election and change the nodes' role.

First of all, we will deactivate one of the secondary nodes by stopping the mongod service on the secondary machine.

Right after stopping the service of mongod3.replicaset.com, the replica set will set this node as an unreachable secondary node, as shown in the following screenshot:

The status of the replica set shows that the third member has an issue, so this member becomes unavailable until the replica set gets a ping from it. After starting the service again, the members' status changes again as shown in the following screenshot:

Note

Network issues or hardware failure on the member's machine also has the same result as stopping the MongoDB service.

For the next test, we will halt the primary node's service. After stopping the service in the primary node, the replica set uses an election process to choose the next primary node from secondary nodes...

Summary


In this chapter, we studied how to create a replica set from scratch to provide a readily available MongoDB server. First of all, we discussed the basic considerations of a replica set. Next, we configured the workspace to run the replica set. Additionally, we learned basic replica set commands in order to add or remove members from the replica set.

In our example, we had a three-member replica set, one primary and two secondary nodes. After configuration and setup, to test the replica set, we deactivated different nodes in the replica set. Also, we found that upon disabling the primary node, the replica set will choose another primary node from the active secondary members using an election process.

Also, we learned to run an arbiter node and add it to an existing replica set network. We found that arbiter nodes won't save and hold data at all.

In the next chapter, we will learn sharding in MongoDB, and distributing a dataset into different servers and machines.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
MongoDB High Availability
Published in: Jul 2014Publisher: ISBN-13: 9781783986729
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Afshin Mehrabani

Afshin Mehrabani is an open source programmer. He is studying to be a computer software engineer. He started programming and web development when he was 12 years old, as well as starting with PHP. Later, he joined the Iran Technical and Vocational Training Organization. He secured the first place and received a gold medal in a competition which was conducted across the entire country in the area of web development. He became a member of the Iran National Foundation of Elite after producing a variety of new programming ideas. He was a software engineer at the Tehran Stock Exchange and is now the head of the web development team in the Yara Company. He cofounded the Usablica team in early 2012 to develop and produce usable applications. He is the author of IntroJs, WideArea, flood.js and some other open source projects. He has contributed to Socket.IO, Engine.IO, and some other open source projects. He is also interested in creating and contributing to open source applications, writing programming articles, and challenging himself with new programming technologies. He has written different articles about JavaScript, Node.js, HTML5, and MongoDB that have been published on different academic websites. Afshin has 5 years of experience in PHP, Python, C#, JavaScript, HTML5, and Node.js in many financial and stock trading projects.
Read more about Afshin Mehrabani