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. 
中文版

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

时间: 2024-11-05 13:39:49

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

MongoDB分片集群环境搭建记录

--创建配置服务器mongod.exe --logpath "G:\USERDATA\MONGODB\Test2\Log\mongodb.log" --logappend --dbpath "G:\USERDATA\MONGODB\Test2\DB" --port 27031 --serviceName "MongoTest2" --serviceDisplayName "MongoTest2" --install --con

mongodb分片集群实战搭建

架构图: 1.准备机器,IP分别设置为 192.168.1.201 192.168.1.202 192.168.1.203 2.在每个机器上下载mongodb 3.在每个机器上建立mongos.config,shard1,shard2.shard3五个文件夹 mongos:数据库集群请求的入口(生产环境应有多个) config:配置服务器,存储所有数据库元信息(路由.分片)的配置(对应mongos) shard1:分片1 shard2:分片2 shard3:分片3 (mongos 3个, con

MongoDB 分片集群实战

背景 在如今的互联网环境下,海量数据已随处可见并且还在不断增长,对于如何存储处理海量数据,比较常见的方法有两种: 垂直扩展:通过增加单台服务器的配置,例如使用更强悍的 CPU.更大的内存.更大容量的磁盘,此种方法虽然成本很高,但是实现比较简单,维护起来也比较方便. 水平扩展:通过使用更多配置一般的服务器来共同承担工作负载,此种方法很灵活,可以根据工作负载的大小动态增减服务器的数量,但是实现比较复杂,得有专门的人员来运维. Databases for MongoDB 试用 IBM Cloud 上提

spark-1.2.0 集群环境搭建

1.下载scala2.11.4版本 下载地址为:http://www.scala-lang.org/download/2.11.4.html ,也可以使用wget http://downloads.typesafe.com/scala/2.11.4/scala-2.11.4.tgz?_ga=1.248348352.61371242.1418807768 2.解压和安装: 解压 :[[email protected] scala]$ tar -xvf scala-2.11.4.tgz  ,安装:[

一步一步教你搭建基于docker的MongoDB复制集群环境

一步一步教你搭建基于docker的MongoDB复制集群环境 1.安装docker 2.创建MongoDB的Image 3.搭建MongoDB的集群 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中. 1.ubuntu14.04安装docker 参考文档 http://docs.docker.com/installation/ubuntulinux/ 参考文档 http://docs.docker.com/mac/started/ pc@pc-Th

MongoDB分片集群搭建及扩容

### 实验:分片集群搭建及扩容#### 实验目标及流程 * 目标:学习如何搭建一个两分片的分片集群 * 环境:3台Linux虚拟机器,4Core 8GB * 步骤: * 配置域名解析 * 准备分片目录 * 创建第一个分片复制集并初始化 * 初始化分片集群,加入第一个分片 * 创建分片表 * 加入第二个分片#### 实验架构 ``` demo1 shard1(Primary|27017) shard2(Primary|27011) Config1(27019) mongos(27017) ```

elasticsearch与mongodb分布式集群环境下数据同步

1.ElasticSearch是什么 ElasticSearch 是一个基于Lucene构建的开源.分布式,RESTful搜索引擎.它的服务是为具有数据库和Web前端的应用程序提供附加的组件(即可搜索的存储库).ElasticSearch为应用程序提供搜索算法和相关的基础架构,用户只需要将应用程序中的数据上载到ElasticSearch数据存储中,就可以通过RESTful URL与其交互.ElasticSearch的架构明显不同于它之前的其他搜索引擎架构,因为它是通过水平伸缩的方式来构建的.不同

Bugsnag的MongoDB分片集群使用经验

Bugsnag是一家为移动应用开发者提供实时的Bug追踪及检测服务的创业公司,Bugsnag已经使用MongoDB存储超过TB级的文档数据.从Bugsnag的第一个版本开始他们就使用MongoDB存储业务数据.近日,Bugsnag的工程师Simon Maynard在博客上分享了他们的MongoDB分片集群经验,并开源了几个他们常使用的脚本. 带标签的分片(Tag Aware Sharding) 带标签的分片是MongoDB 2.2版本中引入的新特性,此特性支持人为控制数据的分片方式,从而使数据存

Redis之集群环境搭建

原文:Redis之集群环境搭建 前面文章介绍了Redis的主从复制,虽然该模式能够在一定程度上提高系统的稳定性,但是在数据访问量比较大的情况下,单个master应付起来还是比较吃力的,这时我们可以考虑将redis集群部署,本文就来重点给大家介绍下Redis的集群部署操作. Redis之主从复制2 Redis之主从复制1 Redis集群 一.Redis集群相关概念 1.Redis集群介绍 Redis 集群是一个提供在多个Redis间节点间共享数据的程序集. Redis集群并不支持处理多个keys的