<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Sharding_in_MongoDB</id>
	<title>Sharding in MongoDB - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Sharding_in_MongoDB"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Sharding_in_MongoDB&amp;action=history"/>
	<updated>2026-05-13T09:36:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=Sharding_in_MongoDB&amp;diff=66288&amp;oldid=prev</id>
		<title>Kbaran at 22:37, 16 April 2018</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Sharding_in_MongoDB&amp;diff=66288&amp;oldid=prev"/>
		<updated>2018-04-16T22:37:41Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:MongoDB]]&lt;br /&gt;
&lt;br /&gt;
{{Copyright Notice}}&lt;br /&gt;
&lt;br /&gt;
== Author ==&lt;br /&gt;
* [http://www.kamilbaran.pl Kamil Baran]&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What is Sharding? ==&lt;br /&gt;
* process of splitting data across multiple machines&lt;br /&gt;
* manual sharding (any database engine)&lt;br /&gt;
* MongoDB supports &amp;#039;&amp;#039;&amp;#039;autosharding&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* sharding is the most difficult and complex configuration of MongoDB&lt;br /&gt;
* familiarise yourself with standalone servers and replica sets before sharding&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== When to use it? ==&lt;br /&gt;
* not too early because it adds complexity to the deployment&lt;br /&gt;
* not too late because it&amp;#039;s difficult to shard an overloaded system without downtime&lt;br /&gt;
* to increase available RAM and disk space&lt;br /&gt;
* to reduce the load on a single server&lt;br /&gt;
* to read or write data with greater throughput than a single server can handle&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to deploy MongoDB Sharded Cluster? ==&lt;br /&gt;
=== MongoDB Config Servers ===&lt;br /&gt;
* config servers store the metadata for a sharded cluster&lt;br /&gt;
* from version 3.4 it has to be deployed as a replica set using WiredTiger storage engine&lt;br /&gt;
* the admin database and the config database exist on the config servers&lt;br /&gt;
* deployed on separate physical machines, geographically distributed&lt;br /&gt;
* it doesn&amp;#039;t need many resources (1MB / 200GB)&lt;br /&gt;
* it can be deployed on machines with other services&lt;br /&gt;
* backups should be done often and especially before any maintenance&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ mongod --configsvr --replSet sh_config --dbpath c:\data\sh_config_1 --port 27019&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MongoDB Router (the mongos process) ===&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;mongos&amp;#039;&amp;#039;&amp;#039; is the process for client applications to connect to&lt;br /&gt;
* it replaces mongod used for standalone server or primary in replica sets&lt;br /&gt;
* the number of instances is not limited&lt;br /&gt;
* common setup is one mongos per application server (on the same machine)&lt;br /&gt;
* must know where the config servers are&lt;br /&gt;
* it does not need data directory&lt;br /&gt;
* set up clients to send all requests to the mongos!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ mongos --configdb sh_config/localhost:27019 --port 27017&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a new shard into the cluster ===&lt;br /&gt;
* a shard should be a replica set&lt;br /&gt;
* replica set name will be used as a name of a shard&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot;&amp;gt;&lt;br /&gt;
$ mongod --shardsvr --replSet shard_a --dbpath c:\data\shard_a_1 --port 27018&lt;br /&gt;
$ mongo localhost:27017&lt;br /&gt;
&amp;gt; sh.addShard(&amp;quot;shard_a/localhost:27018&amp;quot;)&lt;br /&gt;
&amp;gt; sh.status()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test deployment of MongoDB Sharded Cluster ===&lt;br /&gt;
* commands below will start sharded cluster with&lt;br /&gt;
** 3 shards on ports 30000, 30001, 30002 (MongoDB 3.2: 20000, 20001, 20002)&lt;br /&gt;
** one mongos process on port 30999 (MongoDB 3.2: 20006)&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot;&amp;gt;&lt;br /&gt;
$ mkdir c:\data\db\test0&lt;br /&gt;
$ mongo --nodb&lt;br /&gt;
&amp;gt; cluster = new ShardingTest({shards : 3, config : 3, other : { rs : true }})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How Sharding Works? ==&lt;br /&gt;
* databases and collections are not sharded by default&lt;br /&gt;
* you must enable sharding for each database and each collection separately&lt;br /&gt;
* shard key, chunks and chunks ranges, splitting chunks, split threshold&lt;br /&gt;
* $minKey &amp;lt;= &amp;#039;&amp;#039;&amp;#039;chunk1&amp;#039;&amp;#039;&amp;#039; &amp;lt; split point 1 &amp;lt;= &amp;#039;&amp;#039;&amp;#039;chunk2&amp;#039;&amp;#039;&amp;#039; &amp;lt; $maxKey&lt;br /&gt;
* balancer and moving chunks&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Range Based Sharding ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt; sh.status()&lt;br /&gt;
&amp;gt; sh.enableSharding(&amp;quot;test&amp;quot;)&lt;br /&gt;
&amp;gt; use test&lt;br /&gt;
&amp;gt; db.visitors.createIndex({&amp;quot;visitor&amp;quot; : 1})&lt;br /&gt;
&amp;gt; sh.shardCollection(&amp;quot;test.visitors&amp;quot;, {&amp;quot;visitor&amp;quot; : 1})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt; db.visitors.explain(1).find({visitor:&amp;quot;visitor1234&amp;quot;})&lt;br /&gt;
&amp;gt; db.visitors.explain(1).find({i:1234})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Hash Based Sharding ===&lt;br /&gt;
=== Zone Based Sharding ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Sharded Cluster Administration ==&lt;br /&gt;
=== Balancer ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt; sh.getBalancerState()&lt;br /&gt;
&amp;gt; sh.stopBalancer()&lt;br /&gt;
&amp;gt; sh.isBalancerRunning()&lt;br /&gt;
&amp;gt; sh.setBalancerState(true)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Operations on chunks ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot;&amp;gt;&lt;br /&gt;
use config&lt;br /&gt;
db.settings.save({ _id:&amp;quot;chunksize&amp;quot;, value:1 })&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot;&amp;gt;&lt;br /&gt;
sh.splitFind(&amp;quot;test.visitors&amp;quot;, {visitor:&amp;quot;visitor_10&amp;quot;})&lt;br /&gt;
sh.splitAt(&amp;quot;test.visitors&amp;quot;, {visitor:&amp;quot;visitor_10&amp;quot;})&lt;br /&gt;
use admin&lt;br /&gt;
db.runCommand({mergeChunks:&amp;quot;test.visitors&amp;quot;, bounds:[{visitor:&amp;quot;visitor_10&amp;quot;}, {visitor:&amp;quot;visitor_20&amp;quot;}]})&lt;br /&gt;
db.adminCommand({moveChunk:&amp;quot;test.visitors&amp;quot;, find:{visitor:&amp;quot;visitor_99999&amp;quot;}, to:&amp;quot;shard_a&amp;quot;})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Refreshing mongos cache ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JavaScript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt; use admin&lt;br /&gt;
&amp;gt; db.runCommand({flushRouterConfig : 1})&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kbaran</name></author>
	</entry>
</feed>