Tuesday, April 9, 2013

setup mongo sharding, a sample configuration and testing sharding behavior.


Terminal 1
mongod --port 27022 --dbpath /config --configsvr

Terminal 2
mongos --configdb localhost:27022 --port 27034 --chunkSize 1

Termail 3
mongod --port 27023 --dbpath /app/mongo/shards/shard0/data --shardsvr

Termail 4
mongod --port 27024 --dbpath /app/monngo/shards/shard1/data -shardsvr

Connect to mongos
mongo localhost:27034


mongos>  db.runCommand({addshard : "localhost:27023",allowLocal: true})
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard : "localhost:27024",allowLocal: true})
{ "shardAdded" : "shard0001", "ok" : 1 }

mongos> testdb = db.getSisterDB("testdb");
testdb
mongos> db.runCommand({ enablesharding:"testd"})
{ "ok" : 1 }
mongos> db.runCommand({ enablesharding:"testdb"})
{ "ok" : 1 }

mongos> db.runCommand({ enablesharding:"testdb"})
{ "ok" : 0, "errmsg" : "already enabled" }



mongos> db.runCommand({shardcollection : "testdb.testcollection", key : {testkey : 1}})
{ "collectionsharded" : "testdb.testcollection", "ok" : 1 }



Use the following java to insert 200000+ rows into new db 

package dbHelper;  
import java.util.Random;
import com.mongodb.*;

import java.net.UnknownHostException;

public class InsertTester {
MongoHelper mh; 
public InsertTester (String p_hostname, int p_portnumber , String p_username, String p_password , String p_dbname) {
try {
mh = new MongoHelper(p_hostname,p_portnumber,p_username,p_password,p_dbname);
}
catch ( Exception e ) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
InsertTester tester=new InsertTester ("cvlqmongo1",27034,null,null,"testdb");
int testKey=0;
Random rd = new Random();
BasicDBObject myobj = null;
for ( int i=0; i < 10000 ; i++ ) {
testKey = rd.nextInt();
myobj=new BasicDBObject("testkey",testKey);
myobj.append("Content", "WhatEver");
   tester.mh.addContents("testcollection", myobj);
   
}
}

}

Row counts 
mongos> db.testcollection.count();
20342

Numbers in shr0 and shr1 are on pair. Almost 10000 on each node. 


Add another shard

Termial 5
mongod --port 27025 --dbpath /app/mongo/shards/shards/shard2 --shardsvr 

mogos localhost:27034
db.runCommand( { addshard : "localhost:27025", allowLocal:true});

mongos> use admin
switched to db admin
mongos> db.runCommand({listShards:1});
{
        "shards" : [
                {
                        "_id" : "shard0000",
                        "host" : "localhost:27023"
                },
                {
                        "_id" : "shard0001",
                        "host" : "localhost:27024"
                },
                {
                        "_id" : "shard0002",
                        "host" : "localhost:27025"
                }
        ],
        "ok" : 1
}


Execute testing program again.  insert 20000+ more rows. Node 3 only has 2 rows populated. 

mongos> use testdb
switched to db testdb
mongos> db.testcollection.count();
40342

Node 1: 
mongo localhost:27023
> use testdb
switched to db testdb
> db.testcollection.count();
19957


Node 2: 
 mongo localhost:27024
> use testdb
switched to db testdb
> db.testcollection.count();
20383

Node 3: 
 mongo localhost:27025
> use testdb
switched to db testdb
>  db.testcollection.count();
2






No comments:

Post a Comment