MongoDB分片测试

所属分类: 数据库 / MongoDB 阅读数: 105
收藏 0 赞 0 分享

分片是mongoDB扩展的一种方式。分片分割一个collection并将不同的部分存储在不同的机器上。当一个数据库的collections相对于当前空间过大时,你需要增加一个新的机器。分片会自动的将collection数据分发到新的服务器上。

1. 连接到mongos可查看系统相关信息

configsvr> show dbs
configsvr> use config
configsvr> show collections
onfigsvr> db.mongos.find()
{ "_id" :"racdb:28885", "ping" :ISODate("2016-03-21T09:23:05.106Z"), "up" :NumberLong(1436), "waiting" : true, "mongoVersion" :"3.2.3" }
{ "_id" :"host8.localdomain:28885", "ping" :ISODate("2016-03-21T09:23:07.960Z"), "up" :NumberLong(1427), "waiting" : true, "mongoVersion" :"3.2.3" }
{ "_id" :"host9.localdomain:28885", "ping" :ISODate("2016-03-21T09:23:03.521Z"), "up" :NumberLong(1407), "waiting" : true, "mongoVersion" :"3.2.3" }
configsvr> db.shards.find()
{ "_id" : "shard1","host" : "shard1/host8:28017,racdb:28017" }
{ "_id" : "shard2","host" : "shard2/host8:28018,racdb:28018" }
configsvr> db.databases.find()
{ "_id" :"im_offline_msg", "primary" : "shard1","partitioned" : true }
{ "_id" : "testdb","primary" : "shard2", "partitioned" : true }
{ "_id" : "test","primary" : "shard1", "partitioned" : true }
{ "_id" : "blogdb","primary" : "shard2", "partitioned" : false }

2. 对数据库启用分片

2.1 当前可连接到 mongos 查看数据库或者集合的分片情况(没有分片):

mongos> db.stats()
mongos> db.tab.stats()

2.2 对数据库激活分片功能:

# mongo racdb:28885
mongos>sh.enableSharding("test")
#或者
# mongo racdb:28885
mongos> use admin
mongos> db.runCommand( { enableSharding:"blogdb"} )

2.3 此时查看数据库分区情况,partitioned变为 “true”。

configsvr> use config
switched to db config
configsvr> db.databases.find()
{ "_id" :"im_offline_msg", "primary" : "shard1","partitioned" : true }
{ "_id" : "testdb","primary" : "shard2", "partitioned" : true }
{ "_id" : "test","primary" : "shard1", "partitioned" : true }
{ "_id" : "blogdb","primary" : "shard2", "partitioned" : true }

启用数据库分片并没有将数据进行分开,还需要对 collection 进行分片。

3. 对集合启用分片

启用前,有几个问题需要考虑的:

选择哪个键列作为shard key 。(更多参考:Considerations for Selecting Shard Keys)

如果集合中已经存在数据,在选定作为shard key 的键列必须创建索引;如果集合为空,mongodb 将在激活集合分片(sh.shardCollection)时创建索引。

集合分片函数sh.shardCollection ,

sh.shardCollection(".",shard-key-pattern)

mongos>sh.shardCollection("test.tab", { "_id": "hashed"})

测试插入数据:

--使用python命令
#创建python文件
$ vi batch_insert.py
#-*- coding: UTF-8 -*-
import pymongo
client = pymongo.MongoClient("racdb", 28885)
db = client.testdb
#查看testdb数据库中集合信息
print (db.collection_names())
#连接到my_collection集合
print (db.my_collection)
#清空my_collection集合文档信息
db.my_collection.remove()
#显示my_collection集合中文档数目
print (db.my_collection.find().count())
#插入10000条文档信息
for i in range(10000):
db.my_collection.insert({"id":i,"name":"Licz"})
#显示my_collection集合中文档数目
print ('插入完毕,当前文档数目:')
print (db.my_collection.find().count())
#执行插入
[mongod@racdb ~]$ python2.7.3batch_insert.py
[u'system.indexes', u'table1',u'my_collection']
Collection(Database(MongoClient(host=['racdb:28885'],document_class=dict, tz_aware=False, connect=True), u'testdb'), u'my_collection')
0

插入完毕,当前文档数目:

10000
#或是用mongo shell插入测试数据
for (var i=1; i<=100000; i++) {
db.cc.insert({"id": i,"myName" : "cc"+i, "myDate" : new Date()});
}

启用集合分片

mongos> show collections
mongos> db.cc.find()
mongos> db.cc.createIndex({"id": "hashed" })
mongos> db.cc.getIndexes()
mongos>sh.shardCollection("testdb.cc", { "id": "hashed"})
mongos> db.stats()
mongos> db.cc.stats()
--查看sharding 状态
mongos> db.printShardingStatus();

以上内容是小编给大家介绍的MongoDB分片测试,希望对大家有所帮助!

更多精彩内容其他人还在看

MongoDB插入、更新、删除文档实现代码

本文通过实例代码给大家简单介绍了mongodb插入、更新、删除文档的方法,需要的的朋友参考下吧
收藏 0 赞 0 分享

mongodb 修改器($inc/$set/$unset/$push/$pop/upsert)

对于文档的更新除替换外,针对某个或多个文档只需要部分更新可使用原子的更新修改器,能够高效的进行文档更新。更新修改器是中特殊的键
收藏 0 赞 0 分享

mongodb 数据类型(null/字符串/数字/日期/内嵌文档/数组等)

MongoDB的文档类似于JSON,JSON只是一种简单的表示数据的方式,只包含了6种数据类型(null、布尔、数字、字符串、数组及对象),需要的朋友可以参考下
收藏 0 赞 0 分享

MongoDB的创建、更新和删除

下面开始学习MongoDB最重要也是最基础的部分:C(创建)R(查询)U(更新)D(删除);由于R(查询)操作相对来说内容比较多,也比较繁琐,同时使用频率也比较高,所以下一篇会拿出来单独介绍。废话不多说,连上服务器,我们直接进入正题
收藏 0 赞 0 分享

MongoDB的下载、安装与部署方法

这篇文章主要介绍了MongoDB的下载、安装与部署方法,需要的朋友可以参考下
收藏 0 赞 0 分享

MongoDB的基础知识简介

这篇文章主要介绍了MongoDB的基础知识简介,需要的朋友可以参考下
收藏 0 赞 0 分享

MongoDB的查询方法

这篇文章主要介绍了MongoDB的查询方法,需要的朋友可以参考下
收藏 0 赞 0 分享

MongoDB的索引

数据库中的索引就是用来提高查询操作的性能,但是会影响插入、更新和删除的效率,因为数据库不仅要执行这些操作,还要负责索引的更新
收藏 0 赞 0 分享

MongoDB数据查询方法干货篇

查询操作在我们日常操作数据库的时候是必不可少的一部分,最近有些空闲时间,所有就将MongoDB数据查询的一些方法技巧总结了处理,这篇文章主要介绍了MongoDB数据查询的相关资料,需要的朋友可以参考借鉴,一起来看看吧。
收藏 0 赞 0 分享

MongoDB数据更新方法干货篇

之前给大家分享了MongoDB中数据查询的相关方法和技巧,那么下面这篇文章主要给大家介绍了MongoDB中数据更新方法的相关资料,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享
查看更多