码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB分片集群环境搭建好后简单的创建和测试数据库表

时间:2015-08-19 20:32:05      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:mongodb   集群   数据库   分片   创建和测试数据库分片   


1.进入mongoDB:

./bin/mongo ip:30000
ip因设置而不同

2.新建数据库且分片:

mongos> use 502
switched to db 502
mongos> sh.enableSharding("502")
{ "ok" : 1 }


 
查看表状态:

mongos> sh.status()
--- Sharding Status --- 
...//略
    {  "_id" : "502",  "partitioned" : true,  "primary" : "shard0000" }


3.创建索引:

mongos> db.table1.ensureIndex({"id":1})
{
	"raw" : {
		"219.219.220.180:27017" : {
			"createdCollectionAutomatically" : true,
			"numIndexesBefore" : 1,
			"numIndexesAfter" : 2,
			"ok" : 1
		}
	},
	"ok" : 1
}



4.表分片:

mongos> sh.shardCollection("502.table1",{"id":1})
{ "collectionsharded" : "502.table1", "ok" : 1 }

查看表状态:
	<pre name="code" class="plain">mongos> sh.status()
--- Sharding Status --- 
...//略
{ "_id" : "502", "partitioned" : true, "primary" : "shard0000" }502.table1shard key: { "id" : 1 }chunks:shard0000 1{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)


5.存储数据:

mongos> for(var i=1;i<=1000000;i++){
...  db.table1.save({"id":i,"x":Math.random(),"name":"xubo","time":"20150819","ops":"testinserttimes"});
...  }
WriteResult({ "nInserted" : 1 })

查看状态:

mongos> sh.status()
--- Sharding Status ---
<pre name="code" class="plain"><pre name="code" class="plain">...//略


    {  "_id" : "502",  "partitioned" : true,  "primary" : "shard0000" }        502.table1            shard key: { "id" : 1 }            chunks:                shard0000    3                shard0001    3                shard0002    3            { "id" : { "$minKey"
 : 1 } } -->> { "id" : 2 } on : shard0001 Timestamp(2, 0)            { "id" : 2 } -->> { "id" : 10 } on : shard0001 Timestamp(4, 0)             { "id" : 10 } -->> { "id" : 117038 } on : shard0002 Timestamp(7, 0)             { "id" : 117038 } -->> { "id" : 242715
 } on : shard0000 Timestamp(7, 1)            { "id" : 242715 } -->> { "id" : 359743 } on : shard0002 Timestamp(5, 1)            { "id" : 359743 } -->> { "id" : 490166 } on : shard0002 Timestamp(4, 3)            { "id" : 490166 } -->> { "id" : 607194 } on :
 shard0000 Timestamp(5, 2)            { "id" : 607194 } -->> { "id" : 767751 } on : shard0000 Timestamp(5, 3)            { "id" : 767751 } -->> { "id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(6, 0)


查看具体的数据:

mongos> db.table1.find()
{ "_id" : ObjectId("55d45098da50bfb96a039f13"), "id" : 1, "x" : 0.03590293787419796, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d451a7da50bfb96a07532d"), "id" : 242715, "x" : 0.39869119925424457, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d452afda50bfb96a0b19c8"), "id" : 490166, "x" : 0.19621717440895736, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d45098da50bfb96a039f14"), "id" : 2, "x" : 0.6352831239346415, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d451a9da50bfb96a07532e"), "id" : 242716, "x" : 0.9266659175045788, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d452b0da50bfb96a0b19c9"), "id" : 490167, "x" : 0.00011568982154130936, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d45098da50bfb96a039f15"), "id" : 3, "x" : 0.5743637685663998, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d451a9da50bfb96a07532f"), "id" : 242717, "x" : 0.49728200025856495, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d452b0da50bfb96a0b19ca"), "id" : 490168, "x" : 0.3094450223725289, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d45098da50bfb96a039f16"), "id" : 4, "x" : 0.397825826657936, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d451a9da50bfb96a075330"), "id" : 242718, "x" : 0.11095952079631388, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d452b0da50bfb96a0b19cb"), "id" : 490169, "x" : 0.6007435184437782, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d45098da50bfb96a039f17"), "id" : 5, "x" : 0.0571024667005986, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d451a9da50bfb96a075331"), "id" : 242719, "x" : 0.46688974485732615, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d452b0da50bfb96a0b19cc"), "id" : 490170, "x" : 0.9055150467902422, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d45098da50bfb96a039f18"), "id" : 6, "x" : 0.2606754114385694, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d451a9da50bfb96a075332"), "id" : 242720, "x" : 0.024089211830869317, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d452b0da50bfb96a0b19cd"), "id" : 490171, "x" : 0.7552208981942385, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d45098da50bfb96a039f19"), "id" : 7, "x" : 0.7744387136772275, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
{ "_id" : ObjectId("55d451a9da50bfb96a075333"), "id" : 242721, "x" : 0.6262992226984352, "name" : "xubo", "time" : "20150819", "ops" : "testinserttimes" }
Type "it" for more

查看表存储:

mongos> db.table1.stats()
{
	"sharded" : true,
	"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
	"userFlags" : 1,
	"capped" : false,
	"ns" : "502.table1",
	"count" : 1000000,
	"numExtents" : 27,
	"size" : 112000000,
	"storageSize" : 182550528,
	"totalIndexSize" : 66119312,
	"indexSizes" : {
		"_id_" : 35532896,
		"id_1" : 30586416
	},
	"avgObjSize" : 112,
	"nindexes" : 2,
	"nchunks" : 9,
	"shards" : {
		"shard0000" : {
			"ns" : "502.table1",
			"count" : 403262,
			"size" : 45165344,
			"avgObjSize" : 112,
			"numExtents" : 10,
			"storageSize" : 86310912,
			"lastExtentSize" : 27869184,
			"paddingFactor" : 1,
			"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
			"userFlags" : 1,
			"capped" : false,
			"nindexes" : 2,
			"totalIndexSize" : 24380832,
			"indexSizes" : {
				"_id_" : 13114304,
				"id_1" : 11266528
			},
			"ok" : 1
		},
		"shard0001" : {
			"ns" : "502.table1",
			"count" : 232259,
			"size" : 26013008,
			"avgObjSize" : 112,
			"numExtents" : 8,
			"storageSize" : 37797888,
			"lastExtentSize" : 15290368,
			"paddingFactor" : 1,
			"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
			"userFlags" : 1,
			"capped" : false,
			"nindexes" : 2,
			"totalIndexSize" : 14038192,
			"indexSizes" : {
				"_id_" : 7546448,
				"id_1" : 6491744
			},
			"ok" : 1
		},
		"shard0002" : {
			"ns" : "502.table1",
			"count" : 364479,
			"size" : 40821648,
			"avgObjSize" : 112,
			"numExtents" : 9,
			"storageSize" : 58441728,
			"lastExtentSize" : 20643840,
			"paddingFactor" : 1,
			"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
			"userFlags" : 1,
			"capped" : false,
			"nindexes" : 2,
			"totalIndexSize" : 27700288,
			"indexSizes" : {
				"_id_" : 14872144,
				"id_1" : 12828144
			},
			"ok" : 1
		}
	},
	"ok" : 1
}
显示总计000000个文档,分布为:

"shard0000" 为"count" : 403262;

"shard0001" 为 "count" : 232259;

"shard0002" 为"count" : 364479;

分片效果达到,不过不是很均匀。



参考资料:

[1] Chodorow K. MongoDB: the definitive guide[M]. " O‘Reilly Media, Inc.", 2013.  中文版







版权声明:本文为博主原创文章,未经博主允许不得转载。

MongoDB分片集群环境搭建好后简单的创建和测试数据库表

标签:mongodb   集群   数据库   分片   创建和测试数据库分片   

原文地址:http://blog.csdn.net/xubo245/article/details/47784817

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!