Saturday, July 30, 2011

Mongodb Sharding Configurations on Windows

When i started using mongodb for the first time.... my all relational database concepts went crazy :p :p :p In RDBMS we had to normalize the data in order to avoid the redundancy etc.... but mongodb says that why you keep the data separately if then you had to pick the data by joining the table. Therefore there are no joins in the mongo and you have to put the pre joined data in the collections (tables in RDBMS)

But this forum is again for the sharding configuration so i am not gonna put the other details in it.... which are actually very basic and you can also find them from the mongodb official site.

Sharding is basically a concept of distributing the data with respect to their accordance... like the goolge shows the different results for the same keyword if we search them on different google domains... (google.com,google.com.pk,google.fr) Similarly we tell the database that how the data is being sharded either w.r.t country wise /region wise etc.
So ok download the latest copy from the official mongodb site and extract it to the safe location
First we create a simple mongodb database you need to follow the following commands.
Open the command prompt as administrator..... press window button...write cmd and right click the cmd and open it as administrator.
Lets see i have extracted the mongofile to c drive and suppose it is in c:\mongodb\bin
Make a folder where you want to put the database let say d:\mongo\db1
you need to reach the path to extracted directory (c:\mongodb\bin) and wtite
mongod --dbpath="d:\mongo\db1" --logpath="d:\mongo\db1\log.txt"
database shard will be created and you can communicate with it by starting the mongodb client (talk about it later) and you can also create the multiple shards with this command either on the same machine with different ports or on other machines too.
Now we need to add a configuration db. This will keep the record that how many shards will be in the database.
So make a folder where you want to place config e.g (d:\mongo\config)
mongod --dbpath="d:\mongo\config" --port (port can be explicitly specify with each mongo process)
Now we have to configure a mongos which will be responsible for routing the request to the particular shard
mongos –port 10003 –configdb localhost:10002
e.g 10003 is the port on which the routing service process will run and we will assign it the address of the running config db (e.g in my case it is running on localhost:10002)
So at the end we have to start the mongodb client to see the database and collections etc
run the following command
mongo localhost:10003
now you are connected to the shell and now you need to tell the shell that db1 (that we created in first step) will be the shard of the database to do this
use admin
db.runCommand( { addshard : “localhost:10000″, name : “shard10000″ } );
To shard a database you can run the following command:
db.runCommand( { enablesharding : “” }
In this scenario you will be splitting the collections based on the key pattern that you’ve specified.  I’ll explain how this works in a location based scenario a bit later on.  For now, you need to know that while MongoDB allows you to specify keys to split the underlying collection in the database, it does not allow you to specify which shard to write to.  So, if you set this up MongoDB will decide where to place the data.  There is a way to force this to happen here, but that doesn’t mean that MongoDB will keep that configuration.

So here we go...its all done now.... and similarly we can create more shard and can add them to the same config db and mongodb automatically distributes the data in form of chunks (64mb max) and place them together with respect to the shard key.....
cheers

 
Umar 
Senior Software Engineer
Lahore, Pakistan
Bookmark and Share

1 comment:

Unknown said...

HI.. Thanks for sharing the blog.. Iam search like anything to setup a shard on windows, Many things avail only for ubuntu.. Really Thanks,...