{"id":2967,"date":"2018-03-05T18:52:09","date_gmt":"2018-03-05T15:52:09","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/install-a-mongodb-sharded-cluster-on-centos-7\/"},"modified":"2018-03-05T18:52:09","modified_gmt":"2018-03-05T15:52:09","slug":"how-to-install-a-mongodb-sharded-cluster-on-centos-7","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-a-mongodb-sharded-cluster-on-centos-7\/","title":{"rendered":"How to Install 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 do a horizontal scale of data, partition data across independent instances, and it can be &#8216;Replica Sets&#8217;. Data-set partitioning on &#8216;Sharding&#8217; uses shard key. Sharding allows you to add more machines based on data growth on 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 into a different folder. &#8216;Replication,&#8217; on the other hand, 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, 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 mongos instances, and then mongos will deliver the requests using shard key to the shards replica sets.<\/p>\n<p><strong>Prerequisites<\/strong><\/p>\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>For 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=\"a0d2cfcfd4e0f3e5f2f6e5f2e9f0\" 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:<\/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 latest MongoDB (MongoDB 3.4) for all instances. Add new MongoDB repository by executing the following commands:<\/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 yum command below.<\/p>\n<p class=\"command\">sudo yum -y install mongodb-org<\/p>\n<p>After mongodb is installed, use &#8216;<strong>mongo<\/strong>&#8216; or &#8216;<strong>mongod<\/strong>&#8216; command in the following way to check version details.<\/p>\n<p class=\"command\">mongod &#8211;version<\/p>\n<p><a class=\"fancybox\" id=\"img-1\" href=\"https:\/\/www.howtoforge.com\/images\/install_a_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\/03\/how-to-install-a-mongodb-sharded-cluster-on-centos-7.png\" alt=\"Install MongoDB on All Instances\" 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 prerequisites 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 mongod service running on the server, stop it with the following systemctl command.<\/p>\n<p class=\"command\">systemctl stop mongod<\/p>\n<p>Edit the default mongodb configuration &#8216;<strong>mongod.conf<\/strong>&#8216;.<\/p>\n<p class=\"command\">vim \/etc\/mongod.conf<\/p>\n<p>Change 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 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 address. &#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>In 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 ownership permissions of that directory to the &#8216;mongod&#8217; user.<\/p>\n<p class=\"command\">mkdir -p \/data\/db1<br \/>chown -R mongod:mongod \/data\/db1<\/p>\n<p>Next, start the mongod service with the following command.<\/p>\n<p class=\"command\">mongod &#8211;config \/etc\/mongod.conf<\/p>\n<p>You can check mongod service is running on port 27017 with the netstat command.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p><a class=\"fancybox\" id=\"img-2\" href=\"https:\/\/www.howtoforge.com\/images\/install_a_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\/03\/how-to-install-a-mongodb-sharded-cluster-on-centos-7-1.png\" alt=\"Create Config Server Replica Set\" 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=\"d5a7babaa195b6babbb3bcb2a6a3a7e4\" 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 members 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 the replica set.<\/p>\n<p><a class=\"fancybox\" id=\"img-31\" href=\"https:\/\/www.howtoforge.com\/images\/install_a_mongodb_sharded_cluster_on_centos_7\/big\/31.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/how-to-install-a-mongodb-sharded-cluster-on-centos-7-2.png\" alt=\"Initiate the replica set name with all configsvr members\" 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>The configuration of Config Server Replica Set is done.<\/p>\n<p><a class=\"fancybox\" id=\"img-41\" href=\"https:\/\/www.howtoforge.com\/images\/install_a_mongodb_sharded_cluster_on_centos_7\/big\/41.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/how-to-install-a-mongodb-sharded-cluster-on-centos-7-3.png\" alt=\"configuration of Config Server Replica Set\" width=\"420\" height=\"550\" title=\"\"><\/a><\/p>\n<h2 id=\"step-create-shard-replica-sets\">Step 4 &#8211; Create Shard Replica Sets<\/h2>\n<p>In this step, we will configure 4 centos 7 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 and stop the mongod service (If the service is running), and edit the MongoDB configuration file.<\/p>\n<p class=\"command\">systemctl stop mongod<br \/>vim \/etc\/mongod.conf<\/p>\n<p>Change default storage to your specific directory.<\/p>\n<pre class=\"system\">storage:<br\/>\u00a0 dbPath: \/data\/db1<\/pre>\n<p>In the &#8216;bindIP&#8217; line, change the value to your internal network address.<\/p>\n<pre class=\"system\">bindIP: 10.0.15.21<\/pre>\n<p>In 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 third and fourth shard server.<\/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 whether MongoDB is running with the command below:<\/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-51\" href=\"https:\/\/www.howtoforge.com\/images\/install_a_mongodb_sharded_cluster_on_centos_7\/big\/51.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/how-to-install-a-mongodb-sharded-cluster-on-centos-7-4.png\" alt=\"Create Shard Replica Sets\" width=\"500\" height=\"290\" title=\"\"><\/a><\/p>\n<p>Next, create a new replica set for these 2 shard instances. Connect to &#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=\"c6b4a9a9b286b5aea7b4a2b5b0b4f7\" 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 shown 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-61\" href=\"https:\/\/www.howtoforge.com\/images\/install_a_mongodb_sharded_cluster_on_centos_7\/big\/61.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/how-to-install-a-mongodb-sharded-cluster-on-centos-7-5.png\" alt=\"Initiate the replica set\" width=\"435\" height=\"245\" title=\"\"><\/a><\/p>\n<p>Redo this step on <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 as the shard &#8211; &#8216;<strong>shardreplica01<\/strong>&#8216; and &#8216;<strong>shardreplica02<\/strong>&#8216;.<\/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 are running &#8216;mongos&#8217;. You can run mongos with the configuration file, or run it 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=\"92e0fdfde6d2fffdfcf5fde1\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a>\u00a0 <br \/>systemctl stop mongod<\/p>\n<p>Run mongos with the command 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 will see results below.<\/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\/install_a_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\/03\/how-to-install-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 step 5, 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=\"63110c0c17230e0c0d040c10\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a><br \/>mongo &#8211;host mongos &#8211;port 27017<\/p>\n<p>Add shard server with 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 sharding status as shown in the screenshot below.<\/p>\n<p><a class=\"fancybox\" id=\"img-8\" href=\"https:\/\/www.howtoforge.com\/images\/install_a_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\/03\/how-to-install-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>Now we&#8217;ll test the MongoDB server by enabling sharding and then add documents.<\/p>\n<p>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=\"7705181803371a1819101804\" 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\/install_a_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\/03\/how-to-install-a-mongodb-sharded-cluster-on-centos-7-8.png\" alt=\"Enable sharding.\" width=\"500\" height=\"335\" title=\"\"><\/a><\/p>\n<p>Now see the status of the database &#8211; it 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\/install_a_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\/03\/how-to-install-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 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>You can use an 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>Documents in successfully added to the collection, as shown in the following screenshot.<\/p>\n<p><a class=\"fancybox\" id=\"img-11\" href=\"https:\/\/www.howtoforge.com\/images\/install_a_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\/03\/how-to-install-a-mongodb-sharded-cluster-on-centos-7-10.png\" alt=\"Add documents to 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 am 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=\"44362b2b3004372c25362037323676\" 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 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\/install_a_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\/03\/how-to-install-a-mongodb-sharded-cluster-on-centos-7-11.png\" alt=\"You will see the database, collections, and documents are available in the replica set.\" width=\"500\" height=\"108\" title=\"\"><\/a><\/p>\n<p>MongoDB sharded cluster on CentOS 7 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 do a horizontal scale of data, partition data across independent instances, and it can be &#8216;Replica Sets&#8217;. Data-set partitioning on &#8216;Sharding&#8217; uses shard key. Sharding allows you to add more machines based on data growth on your stack. Sharding and &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-2967","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2967","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=2967"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2967\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}