Simple Steps To Create A Multi-Node MongoDB Cluster

In this article, we will see how to set up a 3-node MongoDB replica set on your local machine easily. I will set up this cluster in my Windows 10 machine.

Introduction

MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need. MongoDB stores data in versatile, JSON-like documents. This means that the fields will vary from document to document and the data structure can be modified over time.

MongoDB has very good documentation. Please open this link to explore more features of MongoDB.
 
C# Corner also has a set of good articles on MongoDB. Please refer to these articles to get started with MongoDB.
 
The latest version of MongoDB Free Community Edition can be downloaded and installed from this link. After following the installation instructions, you can find that MongoDB is installed in the below Windows directory.
 
Simple Steps To Create A Multi Node MongoDB Cluster
The “bin” folder contains many executable files and the “data” folder is used for default database files. The “log” folder consists of all kinds of logs.

You can open the “bin” directory and can see that below files are created with a typical installation.

Simple Steps To Create A Multi Node MongoDB Cluster
 
The “mongod” file is used for starting a new MongoDB instance. Usually, it uses the configuration file “mongod.cfg”. Please note, when we start a new MongoDB instance, it is listening to 27017 port by default.

The “mongo” file is used for connecting to the MongoDB database. We can run it from our command prompt. We can use all CRUD actions with this CLI.

Our purpose for this article is to explain multi-node replication. Hence, I am not explaining more about MongoDB in this article.

Replica Set

A replica set in MongoDB could be a cluster of mongod processes that maintain constant data set. Replica sets give redundancy and high availability and are the basis for all production deployments.

Replication in MongoDB

 
For testing purpose, we are going to create 3 separate instances of MongoDB on the same Windows machine. I am using a Windows 10 machine.

The primary node receives all write operations. A replica set can have only one primary and has “n” number of secondary nodes. You may think, what will happen if the primary node fails. MongoDB automatically assigns any other secondary node as primary and everything works fine. That’s why MongoDB is often called data redundant and highly-available database.

Please refer to this official MongoDB document to get more details about replication.

Let's create our first node.

Open the command prompt and run below command to create a new MongoDB instance. Please make sure you are opening the command prompt in administrative mode.

mongod --replSet replSarath --dbpath="C:\mongo-data\db1"
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
I have used “mongod” command along with the “replSet” parameter. It creates a new replica set in the name of “replSarath”. Please note, I have not given any port number. By default, it takes “27017” port and starts listening. Also, note that I have explicitly given the database path so, that all the data files are being created to given path.
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
You can open another command prompt and execute the below command to create a new MongoDB instance.
 
mongod --replSet replSarath --dbpath="C:\mongo-data\db2" --port 27027
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
Please note, in the above statement, we have explicitly given the port number. So, the new instance will listen to 27027 port.
 
Our new instance is running now. Please note we have not set the replication yet.
 
We can create one more node now. Open a new command prompt again and use the below command to start a new MongoDB instance.
 
mongod --replSet replSarath --dbpath="C:\mongo-data\db3" --port 27037
 
Now, our third instance is also ready and running.
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
We can open a new command prompt and using “mongo” command, we can connect to MongoDB instance.
 
Simple Steps To Create A Multi Node MongoDB Cluster

We will be connected to default port “27017”.

We can check the replication status by below command.
 
rs.status()
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
Our replication has not yet started. We can initiate it by using the below command.
 
rs.initiate()
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
Please ignore the message. Our command prompt is changed to “replSarath:OTHER” now. Again, we can check the replication status now.
 
rs.status()
 
 Simple Steps To Create A Multi Node MongoDB Cluster
 
Our replication is started, and the primary node is initialized. You can notice that the command prompt is changed to “replSarath:PRIMARY” now.

We can add secondary nodes to this replication from our primary node. Please use the below commands to add secondary nodes.

  1. rs.add("localhost:27027")
  2. rs.add("localhost:27037")
Simple Steps To Create A Multi Node MongoDB Cluster
 
Two secondary nodes are added successfully to the replica set.
 
We can again check the status.
 
rs.status()
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
We have one primary node and two secondary nodes in the replica set now. So, open a new command prompt with the below command to connect MongoDB secondary node.
 
mongo --port 27027
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
We can go to the primary node and list current databases by the below command.
 
show dbs
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
I have a set of documents in a JavaScript file. We can load those documents from JavaScript file using the below command (I have attached the JavaScript file with this article).
 
load("loadMovieDetailsDataset.js")
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
If you check the total databases using “show dbs” command, you can see that a new database, “video”, is created now.
 
You can see the collection name by “show collections” command.
 
show collections
 
You can also find the total number of documents in the collection by using the below command.
 
db.movieDetails.count()
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
Please note there are 2295 documents in the “movieDetails” collection in “video” database. We can go to the secondary node and find whether the same collection is created there also.
 
You can use the “show dbs” command. If you are getting the below error after executing this command, just make this node as “slave”.
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
To make this node as a slave, you can use the below command.
 
rs.slaveOk()
 
We can see the same document count in this node as well.
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
We can connect to the third node and check the document count there also.
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
We got the same document count here also. This implies that we have successfully set up the 3-node replica set and a simple cluster on the same machine. This is only for testing purposes. For production purposes, we can use multiple systems in the same network and create multi-node MongoDB clusters easily.
 
We can even connect the cluster with MongoDB Compass. This is a visual tool to handle the databases, collections, and documents. You can get the free community edition from the MongoDB website.
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
Now you may think what will happen if the primary node stops working by accidently. Well, we can stop the instance (running with port 27017) forcefully and check what happens.
 
I have stopped the primary instance by using “Ctrl C” command. Now, we can check the replication status in the second node. We have already connected this in another command prompt, right?
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
You may notice that the third port “27037” now acts as Primary. Our entire cluster will work without any issues. You can refresh the MongoDB Compass and notice that it shows a 2-node replica set now. All other databases, collections, and documents will work without any trouble.
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
We can again try to reconnect our first node (27017 port) and connect from any command prompt using “mongo” command.
 
Simple Steps To Create A Multi Node MongoDB Cluster
 
Please note, this node works as a secondary node now. If you again check the Compass, you can see 3 nodes listed there. As we stated in the beginning, Mongo DB replica set is a very powerful, redundant, and highly-available cluster. If you connect with any of your applications, you can run the application without any interruption irrespective of any node machine failures.

Conclusion

In this article, we have created three different instances of MongoDB database in different ports and we have created a replication with three nodes. After that, we have created a database and collection with some set of documents from a JavaScript file using “load” command. We have seen that new collections and documents are immediately replicated to other nodes as well. Finally, we stopped the primary node instance forcefully and have seen that another secondary node acts as primary immediately. We have also connected our replica set in MongoDB Compass IDE also.