{"id":2585,"date":"2018-01-12T17:23:27","date_gmt":"2018-01-12T14:23:27","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/deploying-mongodb-sharded-cluster-on-centos-7\/"},"modified":"2018-01-12T17:23:27","modified_gmt":"2018-01-12T14:23:27","slug":"how-to-deploy-a-mongodb-sharded-cluster-on-centos-7","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7\/","title":{"rendered":"How to Deploy a MongoDB Sharded Cluster on CentOS 7"},"content":{"rendered":"<p><strong>Sharding<\/strong> is a MongoDB process to store data-set across different machines. It allows you to perform a horizontal scale of data and to partition all data across independent instances. Sharding allows you to add more machines based on data growth to your stack.<\/p>\n<h2 id=\"sharding-and-replication\">Sharding and Replication<\/h2>\n<p>Let&#8217;s make it simple. When you have collections of music, &#8216;Sharding&#8217; will save and keep your music collections in different folders on different instances or replica sets while &#8216;Replication&#8217; is just syncing your music collections to other instances.<\/p>\n<h2 id=\"three-sharding-components\">Three Sharding Components<\/h2>\n<p><strong>Shard<\/strong> &#8211; Used to store all data. And in a production environment, each shard is replica sets. Provides high-availability and data consistency.<\/p>\n<p><strong>Config Server<\/strong> &#8211; Used to store cluster metadata, and contains a mapping of cluster data set and shards. This data is used by mongos\/query server to deliver operations. It&#8217;s recommended to use more than 3 instances in production.<\/p>\n<p><strong>Mongos\/Query Router<\/strong> &#8211; This is just mongo instances running as application interfaces. The application will make requests to the &#8216;mongos&#8217; instance, and then &#8216;mongos&#8217; will deliver the requests using shard key to the shards replica sets.<\/p>\n<h2>Prerequisites<strong><br \/><\/strong><\/h2>\n<ul>\n<li>2 centOS 7 server as Config Replica Sets<\/li>\n<ul>\n<ul>\n<li><em>10.0.15.31\u00a0\u00a0\u00a0\u00a0\u00a0 configsvr1<\/em><\/li>\n<li><em>10.0.15.32\u00a0\u00a0\u00a0\u00a0\u00a0 configsvr2<\/em><\/li>\n<\/ul>\n<\/ul>\n<li>4 CentOS 7 server as Shard Replica Sets<\/li>\n<ul>\n<ul>\n<li><em>10.0.15.21\u00a0\u00a0\u00a0\u00a0\u00a0 shardsvr1<\/em><\/li>\n<li><em>10.0.15.22\u00a0\u00a0\u00a0\u00a0\u00a0 shardsvr2<\/em><\/li>\n<li><em>10.0.15.23\u00a0\u00a0\u00a0\u00a0\u00a0 shardsvr3<\/em><\/li>\n<li><em>10.0.15.24\u00a0\u00a0\u00a0\u00a0\u00a0 shardsvr4<\/em><\/li>\n<\/ul>\n<\/ul>\n<li>1 CentOS 7 server as mongos\/Query Router<\/li>\n<ul>\n<ul>\n<li><em>10.0.15.11\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mongos<\/em><\/li>\n<\/ul>\n<\/ul>\n<li>Root privileges<\/li>\n<li>Each server connected to another server<\/li>\n<\/ul>\n<h2 id=\"step-disable-selinux-and-configure-hosts\">Step 1 &#8211; Disable SELinux and Configure Hosts<\/h2>\n<p>In this tutorial, we will disable SELinux. Change SELinux configuration from &#8216;enforcing&#8217; to &#8216;disabled&#8217;.<\/p>\n<p>Connect to all nodes through OpenSSH.<\/p>\n<p class=\"command\">ssh <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"96e4f9f9e2d6c5d3c4c0d3c4dfc6\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a><\/p>\n<p>Disable SELinux by editing the configuration file.<\/p>\n<p class=\"command\">vim \/etc\/sysconfig\/selinux<\/p>\n<p>Change SELINUX value to &#8216;disabled&#8217;.<\/p>\n<pre class=\"system\">SELINUX=disabled<\/pre>\n<p>Save and exit.<\/p>\n<p>Next, edit the hosts file on each server.<\/p>\n<p class=\"command\">vim \/etc\/hosts<\/p>\n<p>Paste the following hosts configuration:<\/p>\n<pre class=\"system\">\u00a0\u00a0\u00a0 10.0.15.31\u00a0\u00a0\u00a0\u00a0\u00a0 configsvr1<br\/>\u00a0\u00a0\u00a0 10.0.15.32\u00a0\u00a0\u00a0\u00a0\u00a0 configsvr2<br\/>\u00a0\u00a0\u00a0 10.0.15.11\u00a0\u00a0\u00a0\u00a0\u00a0 mongos<br\/>\u00a0\u00a0\u00a0 10.0.15.21\u00a0\u00a0\u00a0\u00a0\u00a0 shardsvr1<br\/>\u00a0\u00a0\u00a0 10.0.15.22\u00a0\u00a0\u00a0\u00a0\u00a0 shardsvr2<br\/>\u00a0\u00a0\u00a0 10.0.15.23\u00a0\u00a0\u00a0\u00a0\u00a0 shardsvr3<br\/>\u00a0\u00a0\u00a0 10.0.15.24\u00a0\u00a0\u00a0\u00a0\u00a0 shardsvr4<\/pre>\n<p>Save and exit.<\/p>\n<p>Now restart all servers using the reboot command.<\/p>\n<p class=\"command\">reboot<\/p>\n<h2 id=\"step-install-mongodb-on-all-instances\">Step 2 &#8211; Install MongoDB on all instances<\/h2>\n<p>We will use the latest MongoDB version (3.4) for all instances. Add new MongoDB repository by executing the following command:<\/p>\n<p class=\"command\">cat &lt;&lt;&#8216;EOF&#8217; &gt;&gt; \/etc\/yum.repos.d\/mongodb.repo<br \/>[mongodb-org-3.4]name=MongoDB Repository<br \/>baseurl=https:\/\/repo.mongodb.org\/yum\/redhat\/$releasever\/mongodb-org\/3.4\/x86_64\/<br \/>gpgcheck=1<br \/>enabled=1<br \/>gpgkey=https:\/\/www.mongodb.org\/static\/pgp\/server-3.4.asc<br \/>EOF<\/p>\n<p>Now install MongoDB 3.4 from mongodb repository using the following yum command.<\/p>\n<p class=\"command\">sudo yum -y install mongodb-org<\/p>\n<p>After mongodb is installed, you can use the &#8216;<strong>mongo<\/strong>&#8216; or &#8216;<strong>mongod<\/strong>&#8216; command.<\/p>\n<p class=\"command\">mongod &#8211;version<\/p>\n<p><a class=\"fancybox\" id=\"img-1\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7.png\" alt=\"Check MongoDB version\" width=\"437\" height=\"210\" title=\"\"><\/a><\/p>\n<h2 id=\"step-create-config-server-replica-set\">Step 3 &#8211; Create Config Server Replica Set<\/h2>\n<p>In the &#8216;prerequisites&#8217; section, we&#8217;ve already defined config server with 2 machines &#8216;configsvr1&#8217; and &#8216;configsvr2&#8217;. And in this step, we will configure it to be a replica set.<\/p>\n<p>If there is a mongod service running on the server, stop it using the systemctl command.<\/p>\n<p class=\"command\">systemctl stop mongod<\/p>\n<p>Edit the default mongodb configuration &#8216;<strong>mongod.conf<\/strong>&#8216; using the <a href=\"https:\/\/www.howtoforge.com\/vim-basics\" target=\"_blank\" rel=\"noopener noreferrer\">Vim editor<\/a>.<\/p>\n<p class=\"command\">vim \/etc\/mongod.conf<\/p>\n<p>Change the DB storage path to your own directory. We will use &#8216;\/data\/db1&#8217; for the first server, and &#8216;\/data\/db2&#8217; directory for the second config server.<\/p>\n<pre class=\"system\">storage:<br\/>\u00a0 dbPath: \/data\/db1<\/pre>\n<p>Change the value of the line &#8216;bindIP&#8217; to your internal network addres &#8211; &#8216;configsvr1&#8217; with IP address 10.0.15.31, and the second server with 10.0.15.32.<\/p>\n<pre class=\"system\">bindIP: 10.0.15.31<\/pre>\n<p>On the replication section, set a replication name.<\/p>\n<pre class=\"system\">replication:<br\/>\u00a0 replSetName: \"replconfig01\"<\/pre>\n<p>And under sharding section, define a role of the instances. We will use these two instances as &#8216;configsvr&#8217;.<\/p>\n<pre class=\"system\">sharding:<br\/>\u00a0 clusterRole: configsvr<\/pre>\n<p>Save and exit.<\/p>\n<p>Next, we must create a new directory for MongoDB data, and then change the owner of that directory to &#8216;mongod&#8217; user.<\/p>\n<p class=\"command\">mkdir -p \/data\/db1<br \/>chown -R mongod:mongod \/data\/db1<\/p>\n<p>After this, start the mongod service with the command below.<\/p>\n<p class=\"command\">mongod &#8211;config \/etc\/mongod.conf<\/p>\n<p>You can use the netstat command to check whether or not the mongod service is running on port 27017.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p><a class=\"fancybox\" id=\"img-2\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-1.png\" alt=\"Configure MongoDB\" width=\"500\" height=\"279\" title=\"\"><\/a><\/p>\n<p>Configsvr1 and Configsvr2 are ready for the replica set. Connect to the &#8216;configsvr1&#8217; server and access the mongo shell.<\/p>\n<p class=\"command\">ssh <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"f5879a9a81b5969a9b939c92868387c4\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a><br \/>mongo &#8211;host configsvr1 &#8211;port 27017<\/p>\n<p>Initiate the replica set name with all configsvr member using the query below.<\/p>\n<p class=\"command\">rs.initiate(<br \/>\u00a0 {<br \/>\u00a0\u00a0\u00a0 _id: &#8220;replconfig01&#8221;,<br \/>\u00a0\u00a0\u00a0 configsvr: true,<br \/>\u00a0\u00a0\u00a0 members: [<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0 { _id : 0, host : &#8220;configsvr1:27017&#8221; },<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0 { _id : 1, host : &#8220;configsvr2:27017&#8221; }<br \/>\u00a0\u00a0\u00a0 ]\u00a0 }<br \/>)<\/p>\n<p>If you get a results &#8216;<strong>{ &#8220;ok&#8221; : 1 }<\/strong>&#8216;, it means the configsvr is already configured with replica set.<\/p>\n<p><a class=\"fancybox\" id=\"img-3\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-2.png\" alt=\"Initiate the replica set name\" width=\"384\" height=\"203\" title=\"\"><\/a><\/p>\n<p>and you will be able to see which node is master and which node is secondary.<\/p>\n<p class=\"command\">rs.isMaster()<br \/>rs.status()<\/p>\n<p><a class=\"fancybox\" id=\"img-4\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/4.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-3.png\" alt=\"see which node is master and which node is secondary\" width=\"420\" height=\"550\" title=\"\"><\/a><\/p>\n<p>The configuration of Config Server Replica Set is done.<\/p>\n<h2 id=\"step-create-the-shard-replica-sets\">Step 4 &#8211; Create the Shard Replica Sets<\/h2>\n<p>In this step, we will configure 4 &#8216;centos 7&#8217; servers as &#8216;Shard&#8217; server with 2 &#8216;Replica Set&#8217;.<\/p>\n<ul>\n<li>2 server &#8211; &#8216;<strong>shardsvr1<\/strong>&#8216; and &#8216;<strong>shardsvr2<\/strong>&#8216; with replica set name: &#8216;<strong>shardreplica01<\/strong>&#8216;<\/li>\n<li>2 server &#8211; &#8216;<strong>shardsvr3<\/strong>&#8216; and &#8216;<strong>shardsvr4<\/strong>&#8216; with replica set name: &#8216;<strong>shardreplica02<\/strong>&#8216;<\/li>\n<\/ul>\n<p>Connect to each server, stop the mongod service (If there is service running), and edit the MongoDB configuration file.<\/p>\n<p class=\"command\">systemctl stop mongod<br \/>vim \/etc\/mongod.conf<\/p>\n<p>Change the default storage to your specific directory.<\/p>\n<pre class=\"system\">storage:<br\/>\u00a0 dbPath: \/data\/db1<\/pre>\n<p>On the &#8216;bindIP&#8217; line, change the value to use your internal network address.<\/p>\n<pre class=\"system\">bindIP: 10.0.15.21<\/pre>\n<p>On the replication section, you can use &#8216;<strong>shardreplica01<\/strong>&#8216; for the first and second instances. And use &#8216;<strong>shardreplica02<\/strong>&#8216; for the third and fourth shard servers.<\/p>\n<pre class=\"system\">replication:<br\/>\u00a0 replSetName: \"shardreplica01\"<\/pre>\n<p>Next, define the role of the server. We will use all this as shardsvr instances.<\/p>\n<pre class=\"system\">sharding:<br\/>\u00a0 clusterRole: shardsvr<\/pre>\n<p>Save and exit.<\/p>\n<p>Now, create a new directory for MongoDB data.<\/p>\n<p class=\"command\">mkdir -p \/data\/db1<br \/>chown -R mongod:mongod \/data\/db1<\/p>\n<p>Start the mongod service.<\/p>\n<p class=\"command\">mongod &#8211;config \/etc\/mongod.conf<\/p>\n<p>Check MongoDB is running using the following command:<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p>You will see MongoDB is running on the local network address.<\/p>\n<p><a class=\"fancybox\" id=\"img-5\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/5.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-4.png\" alt=\"MongoDB is running on the local network address\" width=\"500\" height=\"290\" title=\"\"><\/a><\/p>\n<p>Next, create a new replica set for these 2 shard instances. Connect to the &#8216;shardsvr1&#8217; and access the mongo shell.<\/p>\n<p class=\"command\">ssh <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"7f0d10100b3f0c171e0d1b0c090d4e\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a><br \/>mongo &#8211;host shardsvr1 &#8211;port 27017<\/p>\n<p>Initiate the replica set with the name &#8216;<strong>shardreplica01<\/strong>&#8216;, and the members are &#8216;<strong>shardsvr1<\/strong>&#8216; and &#8216;<strong>shardsvr2<\/strong>&#8216;.<\/p>\n<pre class=\"system\">rs.initiate(<br\/>\u00a0 {<br\/>\u00a0\u00a0\u00a0 _id : \"shardreplica01\",<br\/>\u00a0\u00a0\u00a0 members: [<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0 { _id : 0, host : \"shardsvr1:27017\" },<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0 { _id : 1, host : \"shardsvr2:27017\" }<br\/>\u00a0\u00a0\u00a0 ]<br\/>\u00a0 }<br\/>)<\/pre>\n<p>If there is no error, you will see results as below.<\/p>\n<p>Results from <strong>shardsvr3<\/strong> and <strong>shardsvr4<\/strong> with replica set name &#8216;<strong>shardreplica02<\/strong>&#8216;.<\/p>\n<p><a class=\"fancybox\" id=\"img-6\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/6.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-5.png\" alt=\"Results from shardsvr3 and shardsvr4 with replica set name &#039;shardreplica02&#039;.\" width=\"435\" height=\"245\" title=\"\"><\/a><\/p>\n<p>Redo this step for <strong>shardsvr3<\/strong> and <strong>shardsvr4<\/strong> servers with different replica set name &#8216;<strong>shardreplica02<\/strong>&#8216;.<\/p>\n<p>Now we&#8217;ve created 2 replica sets &#8211;\u00a0&#8216;<strong>shardreplica01<\/strong>&#8216; and &#8216;<strong>shardreplica02<\/strong>&#8216; &#8211; as the shard.<\/p>\n<h2 id=\"step-configure-mongosquery-router\">Step 5 &#8211; Configure mongos\/Query Router<\/h2>\n<p>The &#8216;Query Router&#8217; or mongos is just instances that run &#8216;mongos&#8217;. You can run mongos with the configuration file, or run with just a command line.<\/p>\n<p>Login to the mongos server and stop the MongoDB service.<\/p>\n<p class=\"command\">ssh <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"e694898992a68b8988818995\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a>\u00a0 <br \/>systemctl stop mongod<\/p>\n<p>Run mongos with the command line as shown below.<\/p>\n<p class=\"command\">mongos &#8211;configdb &#8220;replconfig01\/configsvr1:27017,configsvr2:27017&#8221;<\/p>\n<p>Use the &#8216;&#8211;configdb&#8217; option to define the config server. If you are on production, use at least 3 config servers.<\/p>\n<p>You should see results similar to the following.<\/p>\n<pre class=\"system\">Successfully connected to configsvr1:27017<br\/>Successfully connected to configsvr2:27017<\/pre>\n<p>mongos instances are running.<\/p>\n<p><a class=\"fancybox\" id=\"img-7\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/7.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-6.png\" alt=\"Configure mongos\/Query Router\" width=\"500\" height=\"298\" title=\"\"><\/a><\/p>\n<h2 id=\"step-add-shards-to-mongosquery-router\">Step 6 &#8211; Add shards to mongos\/Query Router<\/h2>\n<p>Open another shell from the previous step, connect to the mongos server again, and access the mongo shell.<\/p>\n<p class=\"command\">ssh <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"f2809d9d86b29f9d9c959d81\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a><br \/>mongo &#8211;host mongos &#8211;port 27017<\/p>\n<p>Add shard server with the sh mongodb query.<\/p>\n<p>For &#8216;<strong>shardreplica01<\/strong>&#8216; instances:<\/p>\n<p class=\"command\">sh.addShard( &#8220;shardreplica01\/shardsvr1:27017&#8221;)<br \/>sh.addShard( &#8220;shardreplica01\/shardsvr2:27017&#8221;)<\/p>\n<p>For &#8216;<strong>shardreplica02<\/strong>&#8216; instances:<\/p>\n<p class=\"command\">sh.addShard( &#8220;shardreplica02\/shardsvr3:27017&#8221;)<br \/>sh.addShard( &#8220;shardreplica02\/shardsvr4:27017&#8221;)<\/p>\n<p>Make sure there is no error and check the shard status.<\/p>\n<p class=\"command\">sh.status()<\/p>\n<p>You will see sharding status similar to the way what the following screenshot shows.<\/p>\n<p><a class=\"fancybox\" id=\"img-8\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/8.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-7.png\" alt=\"Add shards to mongos\/Query Router\" width=\"500\" height=\"363\" title=\"\"><\/a><\/p>\n<p>We have 2 shard replica set and 1 mongos instance running on our stack.<\/p>\n<h2 id=\"step-testing\">Step 7 &#8211; Testing<\/h2>\n<p>To test the setup, access the mongos server mongo shell.<\/p>\n<p class=\"command\">ssh <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"b9cbd6d6cdf9d4d6d7ded6ca\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a><br \/>mongo &#8211;host mongos &#8211;port 27017<\/p>\n<p><strong>Enable Sharding for a Database<\/strong><\/p>\n<p>Create a new database and enable sharding for the new database.<\/p>\n<p class=\"command\">use lemp<br \/>sh.enableSharding(&#8220;lemp&#8221;)<br \/>sh.status()<\/p>\n<p><a class=\"fancybox\" id=\"img-9\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/9.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-8.png\" alt=\"Enable Sharding for a Database\" width=\"500\" height=\"335\" title=\"\"><\/a><\/p>\n<p>Now see the status of the database, it&#8217;s has been partitioned to the replica set &#8216;shardreplica01&#8217;.<\/p>\n<p><strong>Enable Sharding for Collections<\/strong><\/p>\n<p>Next, add new collections to the database with sharding support. We will add new collection named &#8216;stack&#8217; with shard collection &#8216;name&#8217;, and then see database and collections status.<\/p>\n<p class=\"command\">sh.shardCollection(&#8220;lemp.stack&#8221;, {&#8220;name&#8221;:1})<br \/>sh.status()<\/p>\n<p><a class=\"fancybox\" id=\"img-10\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/10.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-9.png\" alt=\"Enable Sharding for Collections\" width=\"500\" height=\"348\" title=\"\"><\/a><\/p>\n<p>New collections &#8216;stack&#8217; with shard collection &#8216;name&#8217; has been added.<\/p>\n<p><strong>Add documents to the collections &#8216;stack&#8217;.<\/strong><\/p>\n<p>Now insert the documents to the collections. When we add documents to the collection on sharded cluster, we must include the &#8216;shard key&#8217;.<\/p>\n<p>In the example below, we are using shard key &#8216;<strong>name<\/strong>&#8216;, as we added when enabling sharding for collections.<\/p>\n<p class=\"command\">db.stack.save({<br \/>\u00a0\u00a0\u00a0 &#8220;name&#8221;: &#8220;LEMP Stack&#8221;,<br \/>\u00a0\u00a0\u00a0 &#8220;apps&#8221;: [&#8220;Linux&#8221;, &#8220;Nginx&#8221;, &#8220;MySQL&#8221;, &#8220;PHP&#8221;],<br \/>})<\/p>\n<p>As shown in the following screenshots, documents have been successfully added to the collection.<\/p>\n<p><a class=\"fancybox\" id=\"img-11\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/11.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-10.png\" alt=\"Add documents to the collections &#039;stack&#039;.\" width=\"440\" height=\"142\" title=\"\"><\/a><\/p>\n<p>If you want to test the database, you can connect to the replica set &#8216;<strong>shardreplica01<\/strong>&#8216; PRIMARY server and open the mongo shell. I&#8217;m logging in to the &#8216;shardsvr2&#8217; PRIMARY server.<\/p>\n<p class=\"command\">ssh <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"ed9f828299ad9e858c9f899e9b9fdf\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a><br \/>mongo &#8211;host shardsvr2 &#8211;port 27017<\/p>\n<p>Check database available on the replica set.<\/p>\n<p class=\"command\">show dbs<br \/>use lemp<br \/>db.stack.find()<\/p>\n<p>You will see that the database, collections, and documents are available in the replica set.<\/p>\n<p><a class=\"fancybox\" id=\"img-12\" href=\"https:\/\/www.howtoforge.com\/images\/deploying_mongodb_sharded_cluster_on_centos_7\/big\/12.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/how-to-deploy-a-mongodb-sharded-cluster-on-centos-7-11.png\" alt=\"MongoDB Sharded Cluster on CentOS 7 has been successfully installed and deployed.\" width=\"500\" height=\"108\" title=\"\"><\/a><\/p>\n<p>MongoDB Sharded Cluster on CentOS 7 has been successfully installed and deployed.<\/p>\n<h2 id=\"reference\">Reference<\/h2>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sharding is a MongoDB process to store data-set across different machines. It allows you to perform a horizontal scale of data and to partition all data across independent instances. Sharding allows you to add more machines based on data growth to your stack. Sharding and Replication Let&#8217;s make it simple. When you have collections of &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2585","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2585"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2585\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}