{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/mongodb-creating-shardedcluster/",
    "result": {"data":{"site":{"siteMetadata":{"title":"CrewCode Solutions"}},"markdownRemark":{"id":"613a2d5b-3600-502e-8676-56a4afe8de81","excerpt":"In this article i will explain you step by step to create replica set in mongodb 5.0 community edition.Sharding is a method for distributing a single dataset…","html":"<p>In this article i will explain you step by step to create replica set in mongodb 5.0 community edition.Sharding is a method for distributing a single dataset across multiple databases, which can then be stored on multiple machines.</p>\n<h4>Step 1: Create directories for shards</h4>\n<p>We will create two shards named shard1, shard2 so create two folders with the name shard1 and shard2 and inside shard1 folder create three folder db11, db12, db13 and inside shard2 folder create three folder db21, db22, db23 you can choose any location and directories in your computer to create these folders.</p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Mongodb-Sharding-1.png\" alt=\"sharding-directories1\"></p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Mongodb-Sharding-2.png\" alt=\"sharding-directories2\"></p>\n<h4>Step 2: Creating mongodb instances for shard1 replica set and initiating replica set</h4>\n<p>Now we will create mongodb instances for db11, db22, db23 on port 27117, 27127, 27137</p>\n<p>Command for creating mongodb instances with replica set shard1 on port 27117, 27127, 27137</p>\n<pre><code class=\"language-js\">/* replace the port number and folder path here\n  and execute the same command for port 27127, 27137 and in separate tab/window of terminal */\nmongod --shardsvr --dbpath `your folder path for db11` --replSet shard1 --port 27117\n</code></pre>\n<p><img src=\"https://drive.google.com/uc?export=view&#x26;id=1JlRwTh15JoDaZh5lVq7JEd2vqDo5zsVa\" alt=\"creating-mongdb-instance\"></p>\n<p>After all the three mongodb instance 27117, 27127, 27137 has been created/started then initiate the replca set</p>\n<pre><code class=\"language-js\">// start this mongo instance in new tab or window of terminal don't close the above server you have started\nmongo --port 27117\n\n// type this command after you have started the instance\nvar configuration = {\n  _id: \"shard1\",\n  members: [\n    { _id: 0, host: \"localhost:27117\" },\n    { _id: 1, host: \"localhost:27127\" },\n    { _id: 2, host: \"localhost:27137\" },\n  ],\n};\n\n// type this after above variable initialized\nrs.initiate(configuration);\n</code></pre>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Mongodb-Sharding-3.png\" alt=\"initiate-replica-set\"></p>\n<h4>Step 3: Creating mongodb instances for shard2 replica set and initiating replica set</h4>\n<pre><code class=\"language-js\">/*replace the port number and folder path here and execute\nthe same command for port 27227, 27237 and in separate tab/window of terminal\nalso remember to put --shardsvr flag\n*/\n\nmongod --shardsvr --dbpath `your folder path for db21` --replSet shard2 --port 27217\n</code></pre>\n<p>After all the three mongodb instance 27217, 27227, 27237 has been created/started then initiate the replca set</p>\n<pre><code class=\"language-js\">// start this mongo instance in new tab or window of terminal don't close the above server you have started\nmongo --port 27217\n\n// type this command after you have started the instance\nvar configuration = {\n  _id: \"shard2\",\n  members: [\n    { _id: 0, host: \"localhost:27217\" },\n    { _id: 1, host: \"localhost:27227\" },\n    { _id: 2, host: \"localhost:27237\" },\n  ],\n};\n\n// type this after above variable initialized\nrs.initiate(configuration);\n</code></pre>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Mongodb-Sharding-4.png\" alt=\"initiate-replica-set\"></p>\n<h4>Step 4: Creating config replica set</h4>\n<p>Config replica set stores all the important configuration needed to run the sharded cluster and it has following restrictions</p>\n<p>no arbiters</p>\n<p>no delayed members</p>\n<p>all members must build indexes</p>\n<p>We need to create three directories for configuration server same as we did for shard1 and shard2 replica set you can choose\nthe folder names as per your choice</p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Mongodb-Sharding-5.png\" alt=\"config-replica-set\"></p>\n<p>Same as above we need to start the three mongod server on port 30117, 30127, 30137 and replica set name configReplica</p>\n<pre><code class=\"language-js\">/*replace the port number and folder path here and execute\nthe same command for port 30127, 30137 and in separate tab/window of terminal\nalso make sure to pass the flag --configsvr which makes these server config server\nalso remember to put --shardsvr flag\n*/\n\nmongod --dbpath `your folder path for config1` --replSet configReplica --port 30117 --configsvr\n</code></pre>\n<p>After all the three mongodb instance 30117, 30127, 30137 has been created/started then initiate the replca set</p>\n<pre><code class=\"language-js\">// start this mongo instance in new tab or window of terminal don't close the above server you have started\nmongo --port 30117\n\n// type this command after you have started the instance\nvar configuration = {\n  _id: \"configReplica\",\n  configsvr:true,\n  members: [\n    { _id: 0, host: \"localhost:30117\" },\n    { _id: 1, host: \"localhost:30127\" },\n    { _id: 2, host: \"localhost:30137\" },\n  ],\n};\n\n// type this after above variable initialized\nrs.initiate(configuration);\n</code></pre>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Mongodb-Sharding-6.png\" alt=\"config-replica-set-initiate\"></p>\n<h4>Step 5: Starting mongos query router and connect to it</h4>\n<p>We need to start our query router i.e mongos to start serving queries from different shard mongos which is our query router sends the query to each shards and fetch the result</p>\n<p>We will start the mongos on port 27017 which is default port</p>\n<pre><code class=\"language-js\">mongos --configdb configReplica/localhost:30117,localhost:30127,localhost:30137\n</code></pre>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Mongodb-Sharding-7.png\" alt=\"start-mongos\"></p>\n<h4>Step 6: Connect to mongos and add replica set to the shard</h4>\n<p>After executing above steps we need connect to mongos and add the replica set to the shard</p>\n<pre><code class=\"language-js\">mongo;\n\n//executing this command after connection successful shard1 is the name of replica set\nsh.addShard(\"shard1/localhost:27117\");\n\n//adding second replica set that is shard2\nsh.addShard(\"shard2/localhost:27217\");\n</code></pre>\n<p>After the above steps you are able to successfully create sharded cluster with two shards shard1 and shard2</p>\n<h4>Step 7: Watch our you tube video to follow the above steps</h4>\n<p><a href=\"https://www.youtube.com/watch?v=3hW6_mhLnTM\" title=\"Sharded cluster\"><img src=\"https://img.youtube.com/vi/3hW6_mhLnTM/hqdefault.jpg\" width=\"100%\"></a></p>","fields":{"slug":"/mongodb-creating-shardedcluster/"},"frontmatter":{"title":"MongoDB Sharding - Step By Step With Example","date":"November 15, 2022","description":"In this article i will explain you step by step to create mongodb sharded cluster on mongodb 5.0 community edition","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/mongo.jpg"}},"previous":{"fields":{"slug":"/mongodb-creating-replicaset/"},"frontmatter":{"title":"MongoDB Replica Set - Step by Step With Example","date":"November 01, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/mongo.jpg"}},"next":{"fields":{"slug":"/mqtt-in-nodejs/"},"frontmatter":{"title":"MQTT in NodeJS application","date":"November 28, 2022","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/mqtt-nodejs.jpeg"}}},"pageContext":{"id":"613a2d5b-3600-502e-8676-56a4afe8de81","previousPostId":"e7a83800-bec8-558f-b143-1faac990dc88","nextPostId":"70d3a11a-670c-5a46-8f08-68f675fa722c"}},
    "staticQueryHashes": ["3860684146"]}