标签:
ReplicaSet+Sharding部署:http://blog.csdn.net/lichangzai/article/details/50927588
MongoDB分片测试
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.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 进行分片。
启用前,有几个问题需要考虑的:
选择哪个键列作为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