佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > MongoDB分片測試

MongoDB分片測試

熱門標(biāo)簽:百度競價排名 網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站 呼叫中心市場需求 鐵路電話系統(tǒng) AI電銷 Linux服務(wù)器 服務(wù)外包

分片是mongoDB擴(kuò)展的一種方式。分片分割一個collection并將不同的部分存儲在不同的機(jī)器上。當(dāng)一個數(shù)據(jù)庫的collections相對于當(dāng)前空間過大時,你需要增加一個新的機(jī)器。分片會自動的將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. 對數(shù)據(jù)庫啟用分片

2.1 當(dāng)前可連接到 mongos 查看數(shù)據(jù)庫或者集合的分片情況(沒有分片):

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

2.2 對數(shù)據(jù)庫激活分片功能:

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

2.3 此時查看數(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)行分開,還需要對 collection 進(jìn)行分片。

3. 對集合啟用分片

啟用前,有幾個問題需要考慮的:

選擇哪個鍵列作為shard key 。(更多參考:Considerations for Selecting Shard Keys)

如果集合中已經(jīng)存在數(shù)據(jù),在選定作為shard key 的鍵列必須創(chuàng)建索引;如果集合為空,mongodb 將在激活集合分片(sh.shardCollection)時創(chuàng)建索引。

集合分片函數(shù)sh.shardCollection ,

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

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

測試插入數(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插入測試數(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分片測試,希望對大家有所幫助!

您可能感興趣的文章:
  • 深入理解MongoDB分片的管理
  • Mongodb 刪除添加分片與非分片表維護(hù)
  • MongoDB的分片集群基本配置教程
  • mongodb3.4集群搭建實(shí)戰(zhàn)之高可用的分片+副本集
  • MongoDB入門教程之分片技術(shù)詳解
  • MongoDB分片詳解
  • MongoDB分片鍵的選擇和案例實(shí)例詳解
  • mongodb分片技術(shù)_動力節(jié)點(diǎn)Java學(xué)院整理
  • 詳解MongoDB4.0構(gòu)建分布式分片群集
  • MongoDB分片在部署與維護(hù)管理中常見的事項總結(jié)大全

標(biāo)簽:崇左 銅川 黃山 湘潭 蘭州 仙桃 衡水 湖南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB分片測試》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    绥滨县| 临漳县| 错那县| 双城市| 太湖县| 芮城县| 大理市| 枝江市| 常山县| 凉城县| 临潭县| 大丰市| 天等县| 彰武县| 富民县| 望都县| 寿光市| 虞城县| 家居| 新民市| 巴林右旗| 新郑市| 大洼县| 襄汾县| 左权县| 新民市| 酉阳| 平果县| 淮阳县| 宝坻区| 罗城| 沁阳市| 大港区| 姜堰市| 南川市| 赤城县| 中阳县| 邢台市| 扶余县| 明水县| 德惠市|