在完成了MongoDB叢集的搭建工作之後(見 ),需要做的就是建立一個資料庫,建立表,設定分區主鍵來初始化資料了!
(1)建立WLB資料庫,設定分表wlb_orders
D:/mongodb-win32-i386-1.8.0/cmd>cd d:/mongodb-win32-i386-1.8.0/bin
D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 127.0.0.1:50000
MongoDB shell version: 1.8.0
connecting to: 127.0.0.1:50000/test
> use admin
switched to db admin
> printShardingStatus()
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{
"_id" : "ShardSetA",
"host" : "setA/127.0.0.1:10000,127.0.0.1:10001,127.0.0.1:10002"
}
{
"_id" : "ShardSetB",
"host" : "setB/127.0.0.1:20000,127.0.0.1:20001,127.0.0.1:20002"
}
{
"_id" : "ShardSetC",
"host" : "setC/127.0.0.1:30000,127.0.0.1:30001,127.0.0.1:30002"
}
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
> use wlb //在wlb資料庫不存在的情況下,就會建立一個資料庫
switched to db wlb
> db.createCollection('wlb_orders') //建立一個表wlb_orders
{ "ok" : 1 }
> use admin
switched to db admin
> db.runCommand({enablesharding:'wlb'}) //設定資料庫可以分區
{ "ok" : 1 }
> db.runCommand({shardcollection:'wlb.wlb_orders',key:{order_id:1}}) //設定表的分區主鍵為order_id
{ "collectionsharded" : "wlb.wlb_orders", "ok" : 1 }
> db.printShardingStatus() //查詢資料庫分區資訊
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{
"_id" : "ShardSetA",
"host" : "setA/127.0.0.1:10000,127.0.0.1:10001,127.0.0.1:10002"
}
{
"_id" : "ShardSetB",
"host" : "setB/127.0.0.1:20000,127.0.0.1:20001,127.0.0.1:20002"
}
{
"_id" : "ShardSetC",
"host" : "setC/127.0.0.1:30000,127.0.0.1:30001,127.0.0.1:30002"
}
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "wlb", "partitioned" : true, "primary" : "ShardSetA" }
wlb.wlb_orders chunks:
ShardSetA 1
{ "order_id" : { $minKey : 1 } } -->> { "order_id" : { $maxKey : 1 } } on : ShardSetA { "t" : 1000, "i" : 0 }
>