Using MongoDB | Sharding | One Query Router, One Config Server And One Shard | Part 1
Posted By : Amit Kumar | 25-Feb-2014
Setup Sharding in MongoDB in Ubuntu With One Shard(3 Mongo Instance as Replica Set),One Router and One Config Server
Continue from the previous blog “Basic Configuration For Replica Set With Ubuntu†(http://www.oodlestechnologies.com/blogs/Using-MongoDB-%7C-Replication-%7C-Basic-Configuration-For-Replica-Set-With-Ubuntu) , now we are ready to implement Sharding in MongoDB
What is Sharding ?
Sharding in MongoDB is basically synonym of Horizontal Scaling, in which each document(i.e row) resides in remote databases of different replica set. Data is not bind to single database and partitioned on set of database servers
And why it is so Important ?
Basically sharding helps to improve performance as data is distributed among database servers and we can query for data with minimum latency.
And major importance of sharding is to provide scalability.
How MongoDB Handles Sharding Process ?
There is one query router(which is mongos server) which is actually handles the query or request of data and then it interacts with config servers(one or more), the config server contains metadata of cluster. And through the config servers, router interact with the shards. Sharding uses shard key to distributed all the data through out the shards
Structure :-
Initials :
1.) So we need 5 mongodb installed on each ubuntu server. One for Query Router(router-1),another is config server(config-1) and one shard i.e a replica set with 3 mongo instance (mongo-1, mongo-2, mongo-3)
2.) Make sure you can access each mongo console server from other mongo servers
Configuration for Shard Server (mongo-1, mongo-2, mongo-3)
For proceeding further i assumed that you have followed my previous blog regarding Replica Set (http://www.oodlestechnologies.com/blogs/Using-MongoDB-%7C-Replication-%7C-Basic-Configuration-For-Replica-Set-With-Ubuntu) .You should have replica set with 3 mongod server running.
Step 1:
To make Replica Set, a Shard, you just need to edit the /etc/mongodb.conf and add below lines in each node of Replica Set :-
Code Snippet :-
port = 27017 fork = true replSet = name_of_shard shardsvr = true
Where :-
port -> specifies, on which port the mongod instance will run
fork -> it means create child process
replSet -> and here it is name of shard name .[Note :It should be same in all nodes of one of the shard and i use rs1 as shard name]
shardsvr -> it has value boolean type(true for enable shard and false for disable shard(default))
Step 2:
Now Re-Start the all nodes of the Shard by issuing below commands :-
Code Snippet :-
sudo mongod --config /etc/mongodb.conf
Configuration for Config Server (config-1)
In this section i will only discuss configuration for One Config Server , and Multiple Config Servers,it will discussed in this next blog of this series.
Step 1:
First you need to make database directory where config server stores its databases. Issue the following commands to make directory on config server.
Code Snippet :-
mkdir -p /home/ubuntu/data/configdb
Step 2:
Just issue the following commands on terminal to start the Config Server:-
Code Snippet :-
mkdir -p /home/ubuntu/data/configdb sudo mongod --logpath "config-server.log" --dbpath /home/ubuntu/data/configdb --port 27017 --fork --configsvr
Where :-
logpath -> specifies the location of the log file of config server.
dbpath -> specifies the location of the database of config server.
configsvr -> specifies that mongod process should be run as config server.
Configuration for Query Router (mongos Server (rotuter-1))
Now its time setup query router, which will manage the query request and it decides, from where to fetch data either from shard-1 or shard-2. But here we have used only one shard (multiple shards will be discussed in next blogs of this series)
Step 1:
Start the mongos server with correct ip addres of config server. Basically mongos need atleast one config server to run. Just run the below commands.
Code Snippet :-
sudo mongos --logpath "mongo-1.log" --configdb ip-address-of-config-1-machine:27017
Step 2:
Some of the below commands helps to deal with sharding.
Code Snippet :-
sh.help(); sh.status();
The above commands is preety much good to deal with query router console.
Where :-
sh.help -> it will show you all the sharding related functions.
sh.status -> it will print the list of shard status with this query router.
Step 3:
Now add the shard. [Note: Only primary node's ip-address of shard(replica set) should be used]
Code Snippet :-
sh.addShard( "rs1/ip-address-of-mongo-1-machine:27017" );
The above commands is used to add the primary node of shard into the sharded cluster.
Step 4:
Now enable sharding for your database.
Code Snippet :-
sh.enableSharding("name_of_the_database");
Step 5:
Finally you have to enable sharding for your collections.[Note: Collection must have indexing on atleast one field becuase sharding key is generated on its basis.]
Code Snippet :-
sh.shardCollection("database_name.collection_name",{"field_name": 1}); sh.status();
The first command add the collection and divides it into chunks and second command will show you the current status of sharded cluster.
For more information you can see this MongoDB tutorial for Sharding.
http://docs.mongodb.org/manual/core/sharding/Thanks
Amit
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Amit Kumar
Amit is a bright Groovy and Grails developer and has worked on development of various SaaS applications using Grails framework.