运维开发网

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

运维开发网 https://www.qedev.com 2020-03-04 16:33 出处:网络 作者:运维开发网整理
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 --- ...//略 {

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" : {
		"192.0.1.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.table1
            shard 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">
...//略
<pre name="code" class="html">    {  "_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;

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

猜测原因:数据量过小。

测试:

test 分布是否均匀

m1
> db.table1.stats()
{
	"ns" : "503.table1",
	"count" : 18939488,
	"size" : 2121222656,
	"avgObjSize" : 112,
	"numExtents" : 21,
	"storageSize" : 2897301504,
	"lastExtentSize" : 756662272,
	"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" : 1243267088,
	"indexSizes" : {
		"_id_" : 616707504,
		"id_1" : 626559584
	},
	"ok" : 1
}
> 


m2
switched to db 503
> db.table1.stats()
{
	"ns" : "503.table1",
	"count" : 20130979,
	"size" : 2254669648,
	"avgObjSize" : 112,
	"numExtents" : 21,
	"storageSize" : 2897301504,
	"lastExtentSize" : 756662272,
	"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" : 1330701232,
	"indexSizes" : {
		"_id_" : 653221520,
		"id_1" : 677479712
	},
	"ok" : 1
}
> 



m3

> db.table1.stats()
{
	"ns" : "503.table1",
	"count" : 19441399,
	"size" : 2177436688,
	"avgObjSize" : 112,
	"numExtents" : 21,
	"storageSize" : 2897301504,
	"lastExtentSize" : 756662272,
	"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" : 1276813216,
	"indexSizes" : {
		"_id_" : 637327376,
		"id_1" : 639485840
	},
	"ok" : 1
}
> 


M4

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" : "503.table1",
	"count" : 58746135,
	"numExtents" : 63,
	"size" : 6579567120,
	"storageSize" : 8691904512,
	"totalIndexSize" : 3864926016,
	"indexSizes" : {
		"_id_" : 1914860080,
		"id_1" : 1950065936
	},
	"avgObjSize" : 112,
	"nindexes" : 2,
	"nchunks" : 189,
	"shards" : {
		"shard0000" : {
			"ns" : "503.table1",
			"count" : 19173757,
			"size" : 2147460784,
			"avgObjSize" : 112,
			"numExtents" : 21,
			"storageSize" : 2897301504,
			"lastExtentSize" : 756662272,
			"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" : 1257411568,
			"indexSizes" : {
				"_id_" : 624311184,
				"id_1" : 633100384
			},
			"ok" : 1
		},
		"shard0001" : {
			"ns" : "503.table1",
			"count" : 20130979,
			"size" : 2254669648,
			"avgObjSize" : 112,
			"numExtents" : 21,
			"storageSize" : 2897301504,
			"lastExtentSize" : 756662272,
			"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" : 1330701232,
			"indexSizes" : {
				"_id_" : 653221520,
				"id_1" : 677479712
			},
			"ok" : 1
		},
		"shard0002" : {
			"ns" : "503.table1",
			"count" : 19441399,
			"size" : 2177436688,
			"avgObjSize" : 112,
			"numExtents" : 21,
			"storageSize" : 2897301504,
			"lastExtentSize" : 756662272,
			"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" : 1276813216,
			"indexSizes" : {
				"_id_" : 637327376,
				"id_1" : 639485840
			},
			"ok" : 1
		}
	},
	"ok" : 1
}
mongos> > db.table1.stats()
2015-08-20




继续增大:

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" : "503.table1",
	"count" : 283894130,
	"numExtents" : 81,
	"size" : 31796142560,
	"storageSize" : 40796368752,
	"totalIndexSize" : 22588293056,
	"indexSizes" : {
		"_id_" : 9886361968,
		"id_1" : 12701931088
	},
	"avgObjSize" : 112,
	"nindexes" : 2,
	"nchunks" : 969,
	"shards" : {
		"shard0000" : {
			"ns" : "503.table1",
			"count" : 95244054,
			"size" : 10667334048,
			"avgObjSize" : 112,
			"numExtents" : 27,
			"storageSize" : 13598789584,
			"lastExtentSize" : 2146426864,
			"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" : 7583419872,
			"indexSizes" : {
				"_id_" : 3313430288,
				"id_1" : 4269989584
			},
			"ok" : 1
		},
		"shard0001" : {
			"ns" : "503.table1",
			"count" : 93219937,
			"size" : 10440632944,
			"avgObjSize" : 112,
			"numExtents" : 27,
			"storageSize" : 13598789584,
			"lastExtentSize" : 2146426864,
			"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" : 7423055808,
			"indexSizes" : {
				"_id_" : 3256157408,
				"id_1" : 4166898400
			},
			"ok" : 1
		},
		"shard0002" : {
			"ns" : "503.table1",
			"count" : 95430139,
			"size" : 10688175568,
			"avgObjSize" : 112,
			"numExtents" : 27,
			"storageSize" : 13598789584,
			"lastExtentSize" : 2146426864,
			"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" : 7581817376,
			"indexSizes" : {
				"_id_" : 3316774272,
				"id_1" : 4265043104
			},
			"ok" : 1
		}
	},
	"ok" : 1
}

通过上述发现三个节点的数据相差很小,基本分布均匀。

参考资料:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消