MongoDB分片測(cè)試
分片是mongoDB擴(kuò)展的一種方式。分片分割一個(gè)collection并將不同的部分存儲(chǔ)在不同的機(jī)器上。當(dāng)一個(gè)數(shù)據(jù)庫的collections相對(duì)于當(dāng)前空間過大時(shí),你需要增加一個(gè)新的機(jī)器。分片會(huì)自動(dòng)的將collection數(shù)據(jù)分發(fā)到新的服務(wù)器上。
1. 連接到mongos可查看系統(tǒng)相關(guān)信息
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. 對(duì)數(shù)據(jù)庫啟用分片
2.1 當(dāng)前可連接到 mongos 查看數(shù)據(jù)庫或者集合的分片情況(沒有分片):
mongos> db.stats() mongos> db.tab.stats()
2.2 對(duì)數(shù)據(jù)庫激活分片功能:
# mongo racdb:28885 mongos>sh.enableSharding("test") #或者 # mongo racdb:28885 mongos> use admin mongos> db.runCommand( { enableSharding:"blogdb"} )
2.3 此時(shí)查看數(shù)據(jù)庫分區(qū)情況,partitioned變?yōu)?“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 }
啟用數(shù)據(jù)庫分片并沒有將數(shù)據(jù)進(jìn)行分開,還需要對(duì) collection 進(jìn)行分片。
3. 對(duì)集合啟用分片
啟用前,有幾個(gè)問題需要考慮的:
選擇哪個(gè)鍵列作為shard key 。(更多參考:Considerations for Selecting Shard Keys)
如果集合中已經(jīng)存在數(shù)據(jù),在選定作為shard key 的鍵列必須創(chuàng)建索引;如果集合為空,mongodb 將在激活集合分片(sh.shardCollection)時(shí)創(chuàng)建索引。
集合分片函數(shù)sh.shardCollection ,
sh.shardCollection(".",shard-key-pattern)
mongos>sh.shardCollection("test.tab", { "_id": "hashed"})
測(cè)試插入數(shù)據(jù):
--使用python命令 #創(chuàng)建python文件 $ vi batch_insert.py #-*- coding: UTF-8 -*- import pymongo client = pymongo.MongoClient("racdb", 28885) db = client.testdb #查看testdb數(shù)據(jù)庫中集合信息 print (db.collection_names()) #連接到my_collection集合 print (db.my_collection) #清空my_collection集合文檔信息 db.my_collection.remove() #顯示my_collection集合中文檔數(shù)目 print (db.my_collection.find().count()) #插入10000條文檔信息 for i in range(10000): db.my_collection.insert({"id":i,"name":"Licz"}) #顯示my_collection集合中文檔數(shù)目 print ('插入完畢,當(dāng)前文檔數(shù)目:') print (db.my_collection.find().count()) #執(zhí)行插入 [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
插入完畢,當(dāng)前文檔數(shù)目:
10000 #或是用mongo shell插入測(cè)試數(shù)據(jù) 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 狀態(tài) mongos> db.printShardingStatus();
以上內(nèi)容是小編給大家介紹的MongoDB分片測(cè)試,希望對(duì)大家有所幫助!
相關(guān)文章
mongodb exception: $concat only supports strings, not Number
這篇文章主要介紹了mongodb exception: $concat only supports strings, not NumberInt32解決辦法,需要的朋友可以參考下2014-06-06MongoDB聚合分組取第一條記錄的案例與實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于MongoDB聚合分組取第一條記錄的案例與實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法
副本集是MongoDB的主從復(fù)制中的重要功能,經(jīng)常被用來作額外的備份,這里我們就來詳解MongoDB中用sharding將副本集分配至服務(wù)器集群的方法,首先還是來回顧一下MongoDB中副本集的基本知識(shí):2016-07-07MongoDB教程之?dāng)?shù)據(jù)操作實(shí)例
這篇文章主要介紹了MongoDB教程之?dāng)?shù)據(jù)操作實(shí)例,本文講解了批量插入、數(shù)據(jù)庫清除、數(shù)據(jù)更新、修改器、數(shù)組修改器、upsert等內(nèi)容,需要的朋友可以參考下2015-05-05