首页 / MONGODB / MongoDB分片测试
MongoDB分片测试
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MongoDB分片测试,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3679字,纯文字阅读大概需要6分钟。
内容图文
ReplicaSet+Sharding部署:http://blog.csdn.net/lichangzai/article/details/50927588
MongoDB 分片测试
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 查看数据库或者集合的分片情况(没有分片):
[plain] view plain copy 在 CODE 上查看代码片派生到我的代码片
mongos> db.stats()
mongos> db.tab.stats()
2.2 对数据库激活分片功能:
[plain] view plain copy 在 CODE 上查看代码片派生到我的代码片
# 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("<database>.<collection>",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();
参考:http://blog.csdn.net/kk185800961/article/details/45932747
http://www.cnblogs.com/magialmoon/archive/2013/04/11/3015394.html
原文:http://blog.csdn.net/lichangzai/article/details/50963659
内容总结
以上是互联网集市为您收集整理的MongoDB分片测试全部内容,希望文章能够帮你解决MongoDB分片测试所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。