{"id":997,"date":"2022-06-03T09:45:26","date_gmt":"2022-06-03T09:45:26","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=997"},"modified":"2024-10-19T17:07:59","modified_gmt":"2024-10-19T17:07:59","slug":"mongodb-replication-with-arbiter","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/mongodb-replication-with-arbiter","title":{"rendered":"How to Configure MongoDB replication with Arbiter node"},"content":{"rendered":"<p><strong><a href=\"https:\/\/www.checkmateq.com\/python-development\">MongoDB<\/a>\u00a0<\/strong> is an open-source document-oriented non-relational database management system used for high volume data storage, it uses collections and documents unlike tables and rows in relational database, documents consist of key-value pairs which are mongo dB&#8217;s basic unit of data, fields in a MongoDB document can be indexed with primary and secondary index&#8217;s.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Replica Set<\/strong><\/p>\n<p>we can achieve high availability for data stored in MongoDB using replica sets,\u00a0 a replica set is a group of\u00a0 <b>mongod<\/b> instances that host the same data set each replica set will have one primary node , replication happens from primary node to secondary nodes and\u00a0 any node from replica set can act as primary, if a primary node fails all the available nodes will vote to select a primary node and once the failed node(previous primary) recovers it will join replica set as secondary node, a replica set can have \u00a0maximum number of 50 nodes with only 7 voting members in it.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Arbiter Node<\/strong><\/p>\n<p>Arbiter Node is a MongoDB instance which you can add to a replica set, it participates in selecting primary node in case of a failover but will not hold any data, let&#8217;s consider a scenario to understand the need of a arbiter node in a replica set. consider you have a replica set with two nodes one acting as a primary and the other is secondary if the primary goes down mongodb will not be able to choose new primary as it need majority of members (at least two active nodes) to choose primary node.<\/p>\n<p><strong>Note:\u00a0 <\/strong>only one arbiter node should be added to a replica set.<\/p>\n<p>Consider below given table to understand the majority required for\u00a0 electing\u00a0 a primary node in case of a failure, fault tolerance is number of nodes that can go unavailable.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-1001\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg2-300x125.jpeg\" alt=\"\" width=\"737\" height=\"307\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg2-300x125.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg2-1024x428.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg2-768x321.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg2-1200x502.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg2.jpeg 1260w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>Steps to create a Replica Set<\/strong><\/p>\n<p>In this blog we will see how to create a replica set with 3 nodes in which two will be functional and the other will be a arbiter node.<\/p>\n<p><strong>Step-1:\u00a0<\/strong><\/p>\n<p>before going ahead with creating replication allow traffic on port 27017 on your machine using firewall command,\u00a0 also if you are using any cloud instances add a firewall rule\/ security group allowing traffic to port 27017, MongoDB runs on port 27017 by default, allow traffic on the port number that you are using\u00a0 if\u00a0 you&#8217;re not using the default one.<\/p>\n<p>Ensure your system has the <strong>firewalld<\/strong> installed, execute below command on your terminal.<\/p>\n<pre>sudo yum install firewalld -y<\/pre>\n<p>use below given command if firewalld has been installed now.<\/p>\n<pre>sudo systemctl enable --now firewalld<\/pre>\n<p>use below given command to open port 27017 for traffic\/connections, if you are using any other port to launch your mongod instance use it in the command\u00a0 instead of 27017.<\/p>\n<pre>firewall-cmd --zone=public --add-port=27017\/tcp --permanent &amp;&amp; firewall-cmd --reload<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-1172 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-8-300x25.jpeg\" alt=\"\" width=\"2148\" height=\"179\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-8-300x25.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-8-1024x86.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-8-768x65.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-8-1536x129.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-8-1200x101.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-8.jpeg 1698w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p><strong>Step-2:Start each member of the replica set with\u00a0 &#8211;replSet().<\/strong><\/p>\n<p>Stop already running mongodb\u00a0 server instances using db.shutdownSrver();\u00a0 against admin database on your mongo shells<\/p>\n<pre>use admin;\r\ndb.shutdownServer();<\/pre>\n<p><img loading=\"lazy\" class=\"wp-image-1010 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg-3-300x33.jpeg\" alt=\"\" width=\"727\" height=\"80\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg-3-300x33.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg-3-1024x114.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg-3-768x85.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg-3-1536x171.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg-3-1200x133.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mg-3.jpeg 1861w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>now start the mongodb servers on all the machines using below given command<\/strong><\/p>\n<p>mongod\u00a0 &#8211;bind_ip localhost,&lt;hostname(s)|ip address(es)&gt; &#8211;replSet &#8220;rs0&#8221;<\/p>\n<pre>mongod --bind_ip 127.0.0.1,172.31.42.102 --replSet rs0 --dbpath \/var\/lib\/mongo<\/pre>\n<p><strong>&#8211;bind_ip <\/strong> by default mongodb listens to localhost only, but to achieve accessibility to from network \/ networks use your ip-addresses along with local host.<\/p>\n<p><strong>&#8211;replSet<\/strong> give a name for your replica set here, if your application runs on more than one replica set each set should have a different name.<\/p>\n<p><b>Step-3: Initiate and add mongodb instances to the replica set<\/b><\/p>\n<p>from only one of your machines where mongod instances are running open a new terminal and enter &#8220;mongo&#8221; to connect to\u00a0 mongo shell(this will be your primary node).<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-1034 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-1-300x114.jpeg\" alt=\"\" width=\"1126\" height=\"428\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-1-300x114.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-1-1024x389.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-1-768x292.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-1-1536x583.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-1-1200x456.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-1.jpeg 1815w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>Now to initiate replica set use below given\u00a0 below command, I am going to initiate a replica set with only two mongodb server node and leaving third one to add it a as a arbiter node in the next step.<\/p>\n<pre>rs.initiate( {\r\n     _id : \"rs0\",\r\n     members: [\r\n     { _id: 0, host: \"ip-172-31-41-107.ap-south-1.compute.internal:27017\" },\r\n     { _id: 1, host: \"ip-172-31-41-117.ap-south-1.compute.internal:27017\" },\r\n     ]\r\n})<\/pre>\n<p>here <strong>id:&#8221;rs0&#8243;<\/strong> is the name of the replica set I gave while starting mongodb servers <strong>id:0<\/strong>,<strong>id:1<\/strong> are id&#8217;s that I am giving to the mongod servers in replica set, give a unique id to each of your servers, and for <strong>hosts<\/strong> I am using hostname of my machines along with port number that my mongod servers are running on (27017).<\/p>\n<p><img loading=\"lazy\" class=\"wp-image-1168 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-7-1-300x186.jpeg\" alt=\"\" width=\"656\" height=\"407\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-7-1-300x186.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-7-1-1024x636.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-7-1-768x477.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-7-1-1536x955.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-7-1-1200x746.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-7-1.jpeg 1678w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>now use below given commands to check the configuration and status of your replication.<\/p>\n<pre>rs.config();\r\nrs.status() ;<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-1160 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-2-300x199.jpeg\" alt=\"\" width=\"748\" height=\"496\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-2-300x199.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-2-1024x680.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-2-768x510.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-2-1200x796.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-2.jpeg 1475w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>then on your secondary node connect to your mongo shell and run below command to allow read operations.<\/p>\n<pre>rs.secondaryOk();<\/pre>\n<p>Let&#8217;s check our configuration now, in the below snapshot left one is my primary and on the right is secondary I created a data base named &#8220;my_db&#8221; and checked for it on the secondary server ,mongodb does not show databases until you insert some data into it.<br \/>\n<img loading=\"lazy\" class=\"alignnone wp-image-1162\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-3-274x300.jpeg\" alt=\"\" width=\"368\" height=\"403\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-3-274x300.jpeg 274w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-3-768x840.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-3.jpeg 854w\" sizes=\"(max-width: 368px) 85vw, 368px\" \/><img loading=\"lazy\" class=\"alignnone wp-image-1163\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-4-261x300.jpeg\" alt=\"\" width=\"351\" height=\"403\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-4-261x300.jpeg 261w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-4.jpeg 769w\" sizes=\"(max-width: 351px) 85vw, 351px\" \/><\/p>\n<p><strong>Step-4: Add Arbiter node to your replica set.<\/strong><\/p>\n<p>On your primary node set write concern using below operation, write concern request acknowledgement for write operations form specified number of mongod instances, as I will have two functional nodes and one arbiter in my replica set, I am going to set my write concern to two using below operation.<\/p>\n<p>use below operation under admin database to set write concern.<\/p>\n<pre>db.adminCommand({\r\n    \"setDefaultRWConcern\" : 1,\r\n    \"defaultWriteConcern\" : {\r\n    \"w\" : 2\r\n     }\r\n})<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-1165 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-5-300x136.jpeg\" alt=\"\" width=\"829\" height=\"376\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-5-300x136.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-5-1024x463.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-5-768x347.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-5-1536x695.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-5-1200x543.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-5.jpeg 1720w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>To add arbiter node to your replica set use below command.<\/p>\n<pre>rs.addArb(\"ip-172-31-36-253.ap-south-1.compute.internal:27017\")<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-1166 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-6-300x73.jpeg\" alt=\"\" width=\"703\" height=\"171\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-6-300x73.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-6-1024x249.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-6-768x186.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-6-1536x373.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-6-1200x291.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/06\/mdb-6.jpeg 1718w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>rs.addArb() is a function to add arbiter node to your replica set, here&#8221;ip-172-31-33-133.ap-south-1.compute.internal&#8221;\u00a0 is host name of my third machine and 27017 is port number on which mongodb server is running on.<\/p>\n<p>Please <a href=\"https:\/\/www.checkmateq.com\/contact-us\">Contact us<\/a>\u00a0 for <a href=\"https:\/\/www.checkmateq.com\/virtual-cto-services\">Virtual CTO Services<\/a> to learn more about <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">IT Staffing Services in India<\/a>, <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">Hire Software Developers in India<\/a>,\u00a0 \u00a0<a href=\"https:\/\/www.checkmateq.com\/dedicated-development-team\">product development<\/a> or <a href=\"https:\/\/www.checkmateq.com\/cloud\">cloud Infrastructure engineering services<\/a>. We can also help you hire most experienced software developers on part-time\/full-time\/project basis.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB\u00a0 is an open-source document-oriented non-relational database management system used for high volume data storage, it uses collections and documents unlike tables and rows in relational database, documents consist of key-value pairs which are mongo dB&#8217;s basic unit of data, fields in a MongoDB document can be indexed with primary and secondary index&#8217;s. &nbsp; Replica &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/mongodb-replication-with-arbiter\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Configure MongoDB replication with Arbiter node&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":1047,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,2,11,23,16,24,8],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/997"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=997"}],"version-history":[{"count":50,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/997\/revisions"}],"predecessor-version":[{"id":4620,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/997\/revisions\/4620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/1047"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}