亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MongoDB分片測試

 更新時間:2016年03月24日 11:20:17   作者:我思,故我在  
分片是mongoDB擴(kuò)展的一種方式。分片分割一個collection并將不同的部分存儲在不同的機(jī)器上,本文給大家介紹MongoDB分片測試,需要的朋友參考下吧

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

相關(guān)文章

  • MongoDB賬戶密碼設(shè)置的方法詳解

    MongoDB賬戶密碼設(shè)置的方法詳解

    這篇文章主要給大家介紹了關(guān)于MongoDB賬戶密碼設(shè)置的相關(guān)資料,我們知道m(xù)ysql在安裝的時候需要我們設(shè)置一個數(shù)據(jù)庫默認(rèn)的用戶名和密碼,mongodb也不例外,需要的朋友可以參考下
    2023-09-09
  • MongoDB中的MapReduce簡介

    MongoDB中的MapReduce簡介

    這篇文章主要介紹了MongoDB中的MapReduce簡介,MapReduce是一種計算模型,簡單的說就是將大批量的工作(數(shù)據(jù))分解(MAP)執(zhí)行,然后再將結(jié)果合并成最終結(jié)果(REDUCE),需要的朋友可以參考下
    2015-05-05
  • MongoDB教程之?dāng)?shù)據(jù)操作實例

    MongoDB教程之?dāng)?shù)據(jù)操作實例

    這篇文章主要介紹了MongoDB教程之?dāng)?shù)據(jù)操作實例,本文講解了批量插入、數(shù)據(jù)庫清除、數(shù)據(jù)更新、修改器、數(shù)組修改器、upsert等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • 最新評論