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上查看代碼片派生到My Code片
mongos> db.stats()
mongos> db.tab.stats()
2.2 對資料庫啟用分區功能:
[plain] view plain copy 在CODE上查看代碼片派生到My 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 pymongoclient = 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