Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓÆµ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Modeler   Code  
»áÔ±   
 
   
 
 
     
   
 ¶©ÔÄ
  ¾èÖú
MongoDBʹÓÃС½á£ºÒ»Ð©²»³£¼ûµÄ¾­Ñé·ÖÏí
 
×÷ÕߣºUri Margalit À´Ô´£ºInfoQ ·¢²¼ÓÚ£º2015-8-13
  7239  次浏览      29
 

×î½üÒ»ÄêæµÓÚÊý¾Ý´¦ÀíÏà¹ØµÄ¹¤×÷£¬¸úMongoDB´ò½»µÀ¼«¶à£¬ÒÔÏÂΪʵ¼ù¹ý³ÌÖеÄQ&A£¬ºóÐø»á²»¶¨ÆÚ¸üв¹³ä¡£

1¡¢countͳ¼Æ½á¹û´íÎó

ÕâÊÇÓÉÓÚ·Ö²¼Ê½¼¯ÈºÕýÔÚÇ¨ÒÆÊý¾Ý£¬Ëüµ¼ÖÂcount½á¹ûÖµ´íÎó£¬ÐèҪʹÓÃaggregate pipelineÀ´µÃµ½Õýȷͳ¼Æ½á¹û£¬ÀýÈ磺

db.collection.aggregate([{$group: {_id: null, count: {$sum: 1}}}])

ÒýÓ㺡°On a sharded cluster, count can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.¡±

2¡¢´ÓshellÖиüÐÂ/дÈëµ½ÎĵµµÄÊý×Ö£¬»á±äΪfloatÀàÐÍ

ÒýÓ㺡°shellÖеÄÊý×Ö¶¼±»MongoDBµ±×÷ÊÇË«¾«¶ÈÊý¡£ÕâÒâζ×ÅÈç¹ûÄã´ÓÊý¾Ý¿âÖлñµÃµÄÊÇÒ»¸ö32λÕûÊý£¬ÐÞ¸ÄÎĵµºó£¬½«Îĵµ´æ»ØÊý¾Ý¿âµÄʱºò£¬Õâ¸öÕûÊýÒ²¾Í±»»»³ÉÁ˸¡µãÊý£¬¼´±ã±£³ÖÕâ¸öÕûÊýÔ­·â²»¶¯Ò²»áÕâÑùµÄ¡£¡±

3¡¢restoreÊý¾Ýµ½ÐÂDBʱ£¬²»ÒªÈ¥ÏȽ¨Ë÷Òý

°ÑbsonÊý¾ÝÎļþrestoreµ½ÁíÒ»¸öDBʱ£¬ÐèҪעÒ⣺²»ÄÜÏÈ´´½¨Ë÷ÒýÔÙrestoreÊý¾Ý£¬·ñÔòÐÔÄܼ«²î£¬mongorestore¹¤¾ßĬÈÏ»áÔÚrestoreÍêÊý¾Ýʱ£¬¸ù¾Ýdump³öÀ´µÄindexÐÅÏ¢´´½¨Ë÷Òý£¬ÎÞÐë×Ô¼º´´½¨£¬Èç¹ûÊÇÒª¸ü»»Ë÷Òý£¬Ò²Ó¦¸ÃÔÚÊý¾ÝÈë¿âÍêÖ®ºóÔÙ´´½¨¡£

4¡¢DBÖеÄnamespaceÊýÁ¿Ì«¶àµ¼ÖÂÎÞ·¨´´½¨ÐµÄcollection

´íÎóÌáʾ£ºerror: hashtable namespace index max chain reached:1335£¬ÈçºÎ½â¾öÄØ£¿

ÕâÊÇDBÖеÄcollection¸öÊýÌ«¶àµ¼Ö£¬ÔÚʵ¼ùÖÐÒÔÿ¸öcollection 8KB¼ÆË㣨¸ú¹Ù·½ÎĵµÀï˵µÄ²»Í¬£¬¿ÉÄܸúindexÓйØÏµ£©£¬256MB¿ÉÒÔÖ§³Ö36000¸öcollection¡£db.system.namespaces.count() ÃüÁî¿ÉÒÔͳ¼Æµ±Ç°DBÄÚµÄcollectionÊýÄ¿£¬DB¿ÉÖ§³ÖcollectionÊýÁ¿ÊÇÓÉÓÚnssize²ÎÊýÖ¸¶¨µÄ£¬ËüÖ¸¶¨ÁËdbname.ns´ÅÅÌÎļþµÄ´óС£¬Ò²¾ÍÖ¸¶¨ÁËDB¿ÉÖ§³ÖµÄ×î´ócollectionÊýÄ¿£¬nsΪnamespaceËõд¡£Ä¬ÈÏnssizeΪ16MB¡£

Èç¹ûÖØÆôMongoD²¢ÐÞ¸ÄÁËnssize²ÎÊý£¬ÕâÐÂnssizeÖ»»á¶ÔмÓÈëµÄDBÉúЧ£¬¶ÔÒÔǰÒѾ­´æÔÚµÄDB²»ÉúЧ£¬Èç¹ûÄãÏë¶ÔÒѾ­´æÔÚµÄDB²ÉÓÃеÄnssize£¬±ØÐëÔÚ¼Ó´ónssizeÖØÆôÖ®ºóн¨DB£¬È»ºó°Ñ¾ÉDBµÄcollection ¸´ÖƵ½ÐÂDBÖС£

namespaceÏÞÖÆÏà¹ØÎĵµ£ºhttp://docs.mongodb.org/manual/reference/limits/#Number-of-Namespaces

5¡¢moveChunkÒò¾ÉÊý¾Ýδɾ³ý¶øÊ§°Ü

´íÎóÈÕÖ¾£º¡±moveChunk failed to engage TO-shard in the data transfer: can't accept new chunks because there are still 1 deletes from previous migration¡°¡£

Òâ˼ÊÇ˵£¬µ±Ç°ÕýҪȥ½ÓÊÜÐÂchunk µÄshardÕýÔÚɾ³ýÉÏÒ»´ÎÊý¾ÝÇ¨ÒÆ³öµÄÊý¾Ý£¬²»ÄܽÓÊÜÐÂChunk£¬ÓÚÊDZ¾´ÎÇ¨ÒÆÊ§°Ü¡£ÕâÖÖlogÀïÏÔʾµÄÊÇwarning£¬µ«ÓÐʱºò»á·¢ÏÖshardµÄɾ³ý³ÖÐøÁËÊ®¼¸Ì춼ûÍê³É£¬²é¿´ÈÕÖ¾£¬¿ÉÒÔ·¢ÏÖͬһ¸öchunkµÄɾ³ýÔÚ²»¶ÏÖØ¸´Ö´ÐУ¬ÖØÆôËùÓÐÎÞ·¨½ÓÊÜÐÂchunkµÄshard¿ÉÒÔ½â¾öÕâ¸öÎÊÌâ¡£

Èç¹û²ÉÓÃÁËbalancer×Ô¶¯¾ùºâ£¬ÄÇô¿ÉÒÔ¼ÓÉÏ_waitForDelete²ÎÊý£¬È磺

{ "_id" : "balancer", "activeWindow" : { "start" : "12:00", "stop" : "19:30" }, "stopped" : false, "_waitForDelete" : true }

ÕâÑù¾Í²»»áÒòdelete¶Ñ»ý¶øµ¼ÖºóÐømigrateʧ°Ü£¬µ±È»£¬ÐèÒª¿¼Âǵ½ÕâÀïµÄ×èÈûÊÇ·ñ»áÓ°Ïìµ½³ÌÐòÕý³£ÔËת£¬ÔÚʵ¼ùÖÐÉ÷ÖØ²ÉÓÃʹÓÃwaitForDelete£¬ÒòΪ·¢ÏÖ¼ÓÉÏËüÖ®ºóÇ¨ÒÆÐÔÄܷdz£²î£¬¿ÉÄܳöÏÖ¿¨×¡Ê®¼¸¸öСʱµÄÇé¿ö£¬Íâ½çÄÃסÁ˱»Ç¨ÒÆchunkµÄÓαê¾ä±ú£¬Õâʱºòɾ³ý²»ÄÜÖ´ÐУ¬×èÈûÁ˺óÐøÆäËüÇ¨ÒÆ²Ù×÷¡£

Óα걻´ò¿ª¶øµ¼Ö±»Ç¨ÒÆÊý¾ÝÎÞ·¨¼°Ê±É¾³ýʱµÄÈÕÖ¾£º

2015-03-07T10:21:20.118+0800 [RangeDeleter] rangeDeleter waiting for open cursors in: cswuyg_test.cswuyg_test,
 min: { _id: -6665031702664277348 }, max: { _id: -6651575076051867067 }, elapsedSecs: 6131244, cursors: [ 220477635588 ]

Õâ¿ÉÄܻῨס¼¸Ê®Ð¡Ê±£¬ÉõÖÁÒ»Ö±¿¨×¡£¬Ó°ÏìºóÐøµÄmoveChunk²Ù×÷£¬µ¼ÖÂÊý¾Ý²»¾ùºâ¡£

½â¾ö·½·¨»¹ÊÇ£ºÖØÆô¡£

6¡¢bson size²»Äܳ¬¹ý16MBµÄÏÞÖÆ

µ¥¸öÎĵµµÄBSON size²»Äܳ¬¹ý16MB¡£find²éѯÓÐʱ»áÓöµ½16MBµÄÏÞÖÆ£¬Æ©ÈçʹÓÃ$in ²éѯµÄʱºò£¬inÖеÄÊý×éÔªËØ²»ÄÜÌ«¶à¡£¶ÔÒ»Ð©ÌØÊâµÄÊý¾ÝÔ´×öMapReduce£¬MapReduceÖмä»á½«Êý¾Ý×éºÏΪ¡°KEY£º[VALUE1¡¢VALUE2]¡±ÕâÑùµÄ¸ñʽ£¬µ±valueÌØ±ð¶àµÄʱºò£¬Ò²¿ÉÄÜ»áÓöÉÏ16MBµÄÏÞÖÆ¡£

ÏÞÖÆÎÞ´¦²»ÔÚ£¬ÐèҪעÒ⣬¡±The issue is that the 16MB document limit applies to everything - documents you store, documents MapReduce tries to generate, documents aggregation tries to return, etc.

7¡¢ÅúÁ¿²åÈë

ÅúÁ¿²åÈë¿ÉÒÔ¼õÉÙÊý¾ÝÍù·þÎñÆ÷µÄÌá½»´ÎÊý£¬Ìá¸ßÐÔÄÜ£¬Ò»°ãÅúÁ¿Ìá½»µÄBSON size²»³¬¹ý48MB£¬Èç¹û³¬¹ýÁË£¬Çý¶¯³ÌÐò×Ô¶¯ÐÞ¸ÄΪÍùmongosµÄ¶à´ÎÌá½»¡£

8¡¢°²È«Ð´Èë½éÉܼ°ÆäÑØ¸ï

¹Ø¼ü×Ö£ºacknowledge¡¢write concern¡£

ÔÚ2012Äê11ÔÂ֮ǰ£¬MongoDBÇý¶¯¡¢shell¿Í»§¶ËĬÈÏÊDz»°²È«Ð´È룬Ҳ¾ÍÊÇfire-and-forget£¬¶¯×÷·¢³öÖ®ºó£¬²»¹ØÐÄÊÇ·ñÕæµÄдÈë³É¹¦£¬Èç¹ûÕâʱºò³öÏÖÁË_idÖØ¸´¡¢·ÇUTF8×Ö·ûµÈÒì³££¬¿Í»§¶Ë²»»áÖªµÀ¡£ÔÚ2012Äê11ÔÂÖ®ºó£¬Ä¬ÈÏΪ°²È«Ð´È룬°²È«¼¶±ðÏ൱ÓÚ²ÎÊýw=1£¬¿Í»§¶Ë¿ÉÒÔÖªµÀдÈë²Ù×÷ÊÇ·ñ³É¹¦¡£Èç¹û´úÂëʹÓÃMongo»òÕßCollectionÀ´Á¬½ÓÊý¾Ý¿â£¬Ôò˵Ã÷ËüÊÇĬÈϲ»°²È«Ð´ÈëµÄlegacy´úÂ룬°²È«Ð´ÈëÒѾ­°ÑÁ¬½ÓÊý¾Ý¿âÐÞ¸ÄΪMongoClient½Ó¿Ú¡£

°²È«Ð´Èë¿ÉÒÔ·ÖΪÈý¸ö¼¶±ð£º

µÚÒ»¼¶ÊÇĬÈϵİ²È«Ð´È룬ȷÈÏÊý¾ÝдÈëµ½ÄÚ´æÖоͷµ»Ø£¨w=NÊôÓÚÕâÒ»¼¶£©£»

µÚ¶þ¼¶ÊÇJournal save£¬Êý¾ÝÔÚдÈëµ½DB´ÅÅÌÎļþ֮ǰ£¬MongoDB»áÏȰѲÙ×÷дÈëµ½JournalÎļþ£¬ÕâÒ»¼¶Ö¸µÄÊÇÈ·ÈÏдÈëÁËJournalÎļþ¾Í·µ»Ø£»

µÚÈý¼¶ÊÇfysnc£¬ËùÓÐÊý¾Ýˢдµ½µ½DB´ÅÅÌÎļþ²Å·µ»Ø¡£

Ò»°ãµÚÒ»¼¶¾Í×ã¹»ÁË£¬µÚ¶þ¼¶ÊÇΪÁ˱£Ö¤ÔÚ»úÆ÷Òì³£¶ÏµçµÄÇé¿öÏÂÒ²²»»á¶ªÊ§Êý¾Ý¡£°²È«Ð´ÈëÒª¸¶³öÐÔÄܵĴúÂ룺²»°²È«Ð´ÈëµÄÐÔÄÜ´ó¸ÅÊÇĬÈϰ²È«Ð´ÈëµÄ3±¶¡£Ê¹ÓÃfync²ÎÊýÔòÐÔÄܸü²î£¬Ò»°ã²»Ê¹Óá£

Èç¹ûÊǸ±±¾¼¯£¨replica set£©£¬Æäw=N²ÎÊý£¬N±íʾ°²È«Ð´Èëµ½¶àÉÙ¸ö¸±±¾¼¯²Å·µ»Ø¡£

9¡¢ÉÆÓÃË÷Òý¡ª¡ª¿ÉÄܸúÄãÒÔΪµÄ²»Ò»Ñù

ʹÓÃ×éºÏË÷ÒýµÄʱºò£¬Èç¹ûÓÐÁ½×éË÷Òý£¬ÔÚÏÞÁ¿²éѯµÄÇé¿öÏ£¬¿ÉÄܸú³£¹æµÄÈÏʶ²»Í¬£º

ÀûÓÃ×éºÏË÷Òý×öµÄ²éѯ£¬ÔÚ²»Í¬ÊýÁ¿¼¶Ï»áÓв»Í¬ÐÔÄÜ£º

×éºÏË÷ÒýA£º {"age": 1, "username": 1}

×éºÏË÷ÒýB£º {"username": 1, "age": 1}

È«Á¿²éѯ£º db.user.find({"age": {"$gte": 21, "$lte": 30}}).sort({"username" :1})£¬Ê¹ÓÃË÷ÒýAµÄÐÔÄÜÓÅÓÚË÷ÒýB¡£

ÏÞÁ¿²éѯ£º db.user.find({"age": {"$gte": 21, "$lte": 30}}).sort({"username": 1}).limit(1000)£¬Ê¹ÓÃË÷ÒýBµÄÐÔÄÜÓÅÓÚË÷ÒýA¡£

ÕâÁ½¸ö²éѯÔÚʹÓÃË÷ÒýAµÄʱºò£¬ÊÇÏȸù¾ÝageË÷ÒýÕÒµ½·ûºÏageµÄÊý¾Ý£¬È»ºóÔÙ¶ÔÕâЩ½á¹û×öÅÅÐò¡£Ê¹ÓÃË÷ÒýBµÄʱºò£¬ÊDZéÀúname£¬¶ÔÓ¦µÄÊý¾ÝÅжÏage£¬È»ºóµÃµ½µÄ½á¹ûÊÇnameÓÐÐòµÄ¡£

ÓÅÏÈʹÓÃsort keyË÷Òý£¬ÔÚ´ó¶àÊýÓ¦ÓÃÉÏÖ´Ðеúܺá£

10¡¢²éѯʱË÷ÒýλÖõÄÎÞ˳ÐòÐÔ

×öfindµÄʱºò£¬²¢²»ÒªÇóË÷ÒýÒ»¶¨ÒªÔÚÇ°Ãæ£¬

Æ©È磺

db.test¼¯ºÏÖжÔRÓÐË÷Òý
db.test.find({R:"AA", "H": "BB"}).limit(100).explain()
db.test.find({"H":"BB", R" : "AA"}).limit(100).explain()

ÕâÁ½¸ö²éÕÒÐÔÄÜÒ»Ñù£¬Ëü¶¼»áʹÓÃRË÷Òý¡£

11¡¢Ê¹ÓÃ×éºÏË÷Òý×öshard key¿ÉÒÔ´ó·ù¶ÈÌá¸ß¼¯ÈºÐÔÄÜ

¡°¹Ì¶¨Öµ+ÔöÁ¿Öµ¡± Á½×Ö¶Î×ö×éºÏË÷Òý¿ÉÒÔÓÐЧµÄʵÏÖ·Ö²¼Ê½¼¯ÈºÖеķÖÉ¢¶àÈȵãдÈë¡¢¶ÁÈ¡¡£ÒÔÏÂΪ¶ÁÊé±Ê¼Ç£ºÔÚµ¥¸öMongoDBʵÀýÉÏ£¬×î¸ßЧµÄдÈëÊÇ˳ÐòдÈ룬¶øMongoDB¼¯ÈºÔòÒªÇóдÈëÄÜËæ»ú£¬ÒÔ±ãÆ½¾ù·ÖÉ¢µ½¶à¸öMongoDBʵÀý¡£ËùÒÔ×î¸ßЧµÄдÈëÊÇÓÐ

¶à¸ö¾Ö²¿Èȵ㣺ÔÚ¶à¸öMongoDBʵÀýÖ®¼äÊÇ·ÖɢдÈ룬ÔÚʵÀýÄÚ²¿ÊÇ˳ÐòдÈë¡£ ҪʵÏÖÕâÒ»µã£¬ÎÒÃDzÉÓÃ×éºÏË÷Òý¡£

ÀýÈ磺shardkeyµÄµÚÒ»²¿·ÖÊǺֲܴڵģ¬¿ÉÑ¡¼¯ºÜÉÙµÄ×ֶΣ¬Ë÷ÒýµÄµÚ¶þ²¿·ÖÊǵÝÔö×ֶΣ¬µ±Êý¾ÝÔö¼Óµ½Ò»¶¨³Ì¶Èʱ£¬»á³öÏֺܶàµÚÒ»²¿·ÖÏàͬµÚ¶þ²¿·Ö²»Í¬µÄchunk£¬Êý¾ÝÖ»»áÔÚ×îºóÒ»¸öchunkÀïдÈëÊý¾Ý£¬µ±µÚÒ»²¿·Ö²»Í¬µÄchunk·ÖÉ¢ÔÚ¶à¸öshardÉÏ£¬¾ÍʵÏÖÁ˶àÈȵãµÄдÈë¡£Èç¹ûÔÚÒ»¸öshardÉÏ£¬²»Ö¹Ò»¸öchunk¿ÉÒÔдÈëÊý¾Ý£¬ÄÇÒ²¾ÍÊÇ˵²»Ö¹Ò»¸öÈȵ㣬µ±Èȵã·Ç³£¶àµÄʱºò£¬Ò²¾ÍµÈͬÓÚÎÞÈȵãµÄËæ»úдÈë¡£µ±Ò»¸öchunk·ÖÁÑÖ®ºó£¬Ö»ÄÜÓÐÒ»¸ö³ÉΪÈȵ㣬ÁíÒ»¸ö²»ÄÜÔÙ±»Ð´È룬·ñÔò¾Í»á²úÉúÁ½¸öÈȵ㣬²»ÔÙдÈëµÄchunkÒ²¾ÍÊÇËÀµôÁË£¬ºóÐøÖ»»á¶ÔËüÓжÁ²Ù×÷¡£

ÎÒÔÚʵ¼ùÖгýÁËÊéÖн²µ½µÄ×éºÏ¼ü·½Ê½Í⣬»¹¼ÓÉÏÁËÔ¤·ÖƬ²ßÂÔ£¬±ÜÃâÁËÔçÆÚÊý¾ÝÔö³¤¹ý³ÌÖÐµÄ·ÖÆ¬ºÍÊý¾ÝÇ¨ÒÆ¡£ÁíÍ⻹¾¡¿ÉÄܵÄÖÆÔìÄÜÀûÓþֲ¿ÐÔÔ­ÀíµÄÊý¾ÝдÈ룬ÀýÈçÔÚÊý¾ÝдÈë֮ǰÏȶÔÊý¾ÝÅÅÐò£¬ÓдóÔ¼30%×óÓÒµÄupdateÐÔÄÜÌáÉý¡£

Ô¤·ÖƬÊÇÕâÑù×Ó×öµÄ£º¸ù¾Ý×éºÏshardkeyÐÅÏ¢ÏÈ·ÖÁѺÃchunk£¬°ÑÕâЩ¿ÕchunkÒÆ¶¯µ½¸÷¸öshardÉÏ£¬±ÜÃâÁ˺óÐø×Ô¶¯·ÖÁÑÒýÆðµÄÊý¾ÝÇ¨ÒÆ¡£

12¡¢Ôõô½¨Ë÷Òý¸üÄÜÌá¸ß²éѯÐÔÄÜ£¿

ÔÚ²éѯʱ£¬Ë÷ÒýÊÇ·ñ¸ßЧ£¬Òª×¢ÒâËüµÄcardinality£¨cardinalityÔ½¸ß±íʾ¸Ã¼ü¿ÉÑ¡ÔñµÄÖµÔ½¶à£©£¬ÔÚ×éºÏË÷ÒýÖУ¬ÈÃcardinality¸ßµÄ·ÅÔÚÇ°Ãæ¡£×¢ÒâÕâÀï¸ú·Ö²¼Ê½»·¾³Ñ¡Ôñshard keyµÄ²»Í¬¡£ÒÔÏÂΪ¶ÁÊé±Ê¼Ç£º

index cardinality£¨Ë÷ÒýÉ¢Áг̶ȣ©£¬±íʾµÄÊÇÒ»¸öË÷ÒýËù¶ÔÓ¦µ½µÄÖµµÄ¶àÉÙ£¬É¢Áг̶ÈÔ½µÍ£¬ÔòÒ»¸öË÷Òý¶ÔÓ¦µÄÖµÔ½¶à£¬Ë÷ÒýЧ¹ûÔ½²î£ºÔÚʹÓÃË÷Òýʱ£¬¸ßÉ¢Áг̶ȵÄË÷Òý¿ÉÒÔ¸ü¶àµÄÅųý²»·ûºÏÌõ¼þµÄÎĵµ£¬ÈúóÐøµÄ±È½ÏÔÚÒ»¸ö¸üСµÄ¼¯ºÏÖÐÖ´ÐУ¬Õâ¸ü¸ßЧ¡£ËùÒÔÒ»°ãÑ¡Ôñ¸ßÉ¢Áг̶ȵļü×öË÷Òý£¬»òÕßÔÚ×éºÏË÷ÒýÖУ¬°Ñ¸ßÉ¢Áг̶ȵļü·ÅÔÚÇ°Ãæ¡£

13¡¢·ÇÔ­µØupdate£¬ÐÔÄÜ»áºÜ²î

updateÎĵµÊ±£¬Èç¹ûÐÂÎĵµµÄ¿Õ¼äÕ¼ÓôóÓÚ¾ÉÎĵµ¼ÓÉÏËüÖÜΧpaddingµÄ¿Õ¼ä£¬ÄÇô¾Í»á·ÅÆúÔ­À´µÄλÖ㬰ÑÊý¾Ý¿½±´µ½Ð¿ռ䡣

14¡¢ÎÞ·¨ÔÚË÷Òý½¨Á¢Ö®ºóÔÙÈ¥Ôö¼ÓË÷ÒýµÄ¹ýÆÚʱ¼ä

Èç¹ûË÷Òý½¨Á¢Ö¸¶¨Á˹ýÆÚʱ¼ä£¬ºóÐøÒªupdate¹ýÆÚʱ¼ä¿ÉÒÔÕâÑù×Ó£ºdb.runCommand({"collMod":"a", index:{keyPattern:{"_":-1}, expireAfterSeconds: 60}})¡£

×¢Ò⣬ͨ¹ýcollModÄÜÐ޸ĹýÆÚʱ¼äµÄǰÌáÊÇ£ºÕâ¸öË÷ÒýÓйýÆÚʱ¼ä£¬Èç¹ûÕâ¸öË÷Òý֮ǰûÓÐÉèÖùýÆÚʱ¼ä£¬ÄÇôÎÞ·¨update£¬Ö»ÄÜɾÁËË÷Òý£¬Öؽ¨Ë÷Òý²¢Ö¸¶¨¹ýÆÚʱ¼ä¡£

15¡¢_idË÷ÒýÎÞ·¨É¾³ý

16¡¢paddingFactorÊÇʲô£¿

ËüÊÇ´æ´¢¿Õ¼äÈßÓàϵÊý£¬1.0±íʾûÓÐÈßÓ࣬1.5±íʾ50%µÄÈßÓà¿Õ¼ä£¬ÓÐÁËÈßÓà¿Õ¼ä£¬¿ÉÒÔÈúóÐøÒý·¢sizeÔö¼ÓµÄ²Ù×÷¸ü¿ì£¨²»»áµ¼ÖÂÖØÐ·ÖÅä´ÅÅ̿ռäºÍÎĵµÇ¨ÒÆ£©£¬Ò»°ãÊÇÔÚ1µ½4Ö®¼ä¡£¿ÉÒÔͨ¹ýdb.collection.stats()¿´µ½collectionµÄ¸ÃÖµ¡°paddingFactor¡±¡£

¸ÃÖµÊÇMongoDB×Ô¼º´¦ÀíµÄ£¬Ê¹ÓÃÕßÎÞ·¨ÉèÖÃpaddingFactor¡£ÎÒÃÇ¿ÉÒÔÔÚcompactµÄʱºò¶ÔÒѾ­ÓеÄÎĵµÖ¸¶¨¸ÃÖµ£¬µ«Õâ¸öpaddingFactorÖµ²»Ó°ÏìºóÐøÐ²åÈëµÄÎĵµ¡£

repairDatabase¸úcompactÀàËÆ£¬Ò²ÄÜÒÆ³ýÈßÓà¼õÉÙ´æ´¢¿Õ¼ä£¬µ«ÈßÓà¿Õ¼äÉÙÁ˻ᵼÖºóÐøÔö¼ÓÎĵµsizeµÄupdate²Ù×÷±äÂý¡£

ËäÈ»ÎÒÃÇÎÞ·¨ÉèÖÃpaddingFactor£¬µ«ÊÇ¿ÉÒÔʹÓÃusePowerOf2Sizes±£Ö¤·ÖÅäµÄ¿Õ¼äÊÇ2µÄ±¶Êý£¬ÕâÑùÒ²¿ÉÒÔÆðµ½×÷Óã¨MongoDB2.6°æ±¾ÆðĬÈÏÆôÓÃusePowerOf2Size£©¡£

»òÕßÊÖ¶¯ÊµÏÖpadding£ºÔÚ²åÈëÎĵµµÄʱºòÏÈÓÃĬÈÏ×Ö·ûÕ¼ÓÃÒ»¿é¿Õ¼ä£¬µÈµ½ÕæÊµÊý¾ÝдÈëʱ£¬ÔÙunsetµôËü¡£

17¡¢usePowerOf2SizeÊÇʲô

ÕâÊÇΪ¸üÓÐЧµÄ¸´ÓôÅÅÌ¿Õ¼ä¶øÉèÖõIJÎÊý£º·ÖÅäµÄ´ÅÅ̿ռäÊÇ2µÄ±¶Êý£¬Èç¹û³¬¹ýÁË4MB£¬ÔòÊǾàÀë¼ÆËãÖµ×î½üµÄÇÒ´óÓÚËüµÄÍêÕûMBÊý¡£
¿ÉÒÔͨ¹ýdb.collections.stats()¿´µ½¸ÃÖµ¡°userFlags¡±¡£
MongoDB2.6Ö®ºóĬÈÏ¿ªÆôusePowerOf2Size²ÎÊý
ʹÓúóµÄЧ¹û¿ÉÒÔ¿´ÕâÀïµÄPPT£ºhttp://www.slideshare.net/mongodb/use-powerof2sizes-27300759

18¡¢aggregate pipeline Ö¸¶¨ÔËËãÍê³ÉÊä³öÎĵµ¸úMapReduceÏà±ÈÓв»×ã

£¨»ùÓÚMongoDB2.6°æ±¾£©MapReduce¿ÉÒÔÖ¸¶¨Êä³öµ½Ìض¨µÄdb.collectionÖУ¬ÀýÈ磺out_put = bson.SON([("replace", "collection_name" ), ("db", "xx_db")])

aggregate pipelineÖ»ÄÜÖ¸¶¨collectionÃû×Ö£¬Ò²¾ÍÒâζ×ÅÊý¾ÝÖ»ÄÜдÈëµ½±¾db£¬Í¬Ê±½á¹û²»ÄÜдÈëµ½capped collection¡¢shard collectionÖС£

Ïà±È֮ϣ¬aggregate pipelineÏÞÖÆÊDZȽ϶àµÄ£¬Èç¹ûÎÒÃÇÐèÒª°Ñ½á¹û·Åµ½Ä³¸öDBÏ£¬ÔòÐèÒªÔÙ×öÒ»´ÎÇ¨ÒÆ£º
db.runCommand({renameCollection:"sourcedb.mycol",to:"targetdb.mycol"})

µ«ÊÇ£¡£¡ÉÏÃæµÄÕâÌõÃüÁîÒªÇóÔÚadminÏÂÖ´ÐУ¬ÇÒÖ»ÄÜÇ¨ÒÆÍùͬshardϵÄDB£¬ÇÒ±»Ç¨ÒƵÄcollection²»ÄÜÊÇshardµÄ¡£

¸½´íÎóÂëÐÅÏ¢£º

https://github.com/mongodb/mongo/blob/master/src/mongo/s/commands_public.cpp#L778
uassert(13140, "Don't recognize source or target DB", confFrom && confTo);
uassert(13138, "You can't rename a sharded collection", !confFrom->isSharded(fullnsFrom));
uassert(13139, "You can't rename to a sharded collection", !confTo->isSharded(fullnsTo));
uassert(13137, "Source and destination collections must be on same shard", shardFrom == shardTo);

19¡¢É±µôMongoD½ø³ÌµÄ¼¸ÖÖ·½Ê½

1£©½øÈëµ½MongoDµÄÃüÁîÐÐģʽִÐÐshutdown£¬

$ mongo --port 10001
> use admin
> db.shutdownServer()

2£©1·½Ê½µÄ¼ò»¯£º

eg£ºmongo admin --port 10001 --eval "db.shutdownServer()"

3£©Ê¹ÓÃMongoDÃüÁîÐйرգ¬ÐèÒªÖ¸¶¨db·¾¶£º

mongod --dbpath ./data/db --shutdown

20¡¢¼¯ÈºµÄshard keyÉ÷ÖØ²ÉÓÃhash

Èç¹ûÄãµÄÈÕÖ¾ÊÇÓÐÈÕÆÚÊôÐԵģ¬ÄÇôshard key²»ÒªÊ¹ÓÃhash£¬·ñÔòɾ³ý¹ýÆÚÈÕ־ʱÎÞ·¨³É¿éɾ³ý£»ÔÚ¸üÐÂÈÕÖ¾µÄʱºò£¬Ò²²»ÄÜÀûÓþֲ¿ÐÔÔ­Àí£¬²éÕÒ¡¢¸üС¢²åÈëÊý¾Ý¶¼»áÒò´Ë¶ø±äÂý¡£Ò»°ãÀ´Ëµ£¬hash idÓ¦¸¶Ð¡Êý¾ÝÁ¿Ê±Ñ¹Á¦²»´ó£¬µ«ÔÚÊý¾ÝÁ¿½Ï´ó£¨ÈÈÊý¾Ý´óÓÚ¿ÉÓÃÄÚ´æÈÝÁ¿£©Ê±£¬CRUDÐÔÄܼ«²î£¬ÇÒ»á·Å´óË鯬¶ÔÐÔÄܵÄÓ°Ï죺Êý¾Ý·Ç³£·ÖÉ¢£¬µ±ÓйýÆÚÈÕÖ¾±»É¾³ýºó£¬ÕâЩɾ³ýºóµÄ¿Õ¼ä³ÉΪË鯬£¬¿ÉÄÜ»áÒòΪ´ÅÅÌÔ¤¶Á²ßÂÔ±»¼ÓÔØµ½ÄÚ´æÖС£ÁíÍ⣬²ÉÓÃhash shard key»¹»áÀ˷ѵôÒ»¸öË÷Òý£¬À˷Ѳ»Éٿռ䡣

21¡¢¸±±¾ÊýÒ²²»ÓÃÌ«¶à

Èç¹ûÄãµÄ¸±±¾ÊýÁ¿³¬¹ýÁË12¸ö£¨MongoDB3.0.0³¬¹ýÁË50¸ö£©£¬ÄÇô¾ÍҪѡÔñʹÓà master-slave £¬µ«ÕâÑù»áʧȥ¹ÊÕÏ×Ô»Ö¸´¹¦ÄÜ£¬Ö÷½Úµã¹ÊÕÏʱ£¬ÐèÒªÊÖ¶¯È¥Çл»µ½ÎÞ¹ÊÕϽڵ㡣

22¡¢mongosµÄconfig serverÅäÖÃÐÅÏ¢Öв»ÒªÊ¹ÓÃlocalhost¡¢127.0.0.1

Æô¶¯mongosʱ£¬config serverµÄÅäÖÃÐÅÏ¢²»µÃʹÓÃlocalhost¡¢127.0.0.1£¬·ñÔòÌí¼ÓÆäËü»úÆ÷µÄshardʱ£¬»á³öÏÖ´íÎóÌáʾ£º
"can¡¯t use localhost as a shard since all shards need to communicate. either use all shards and configdbs in localhost or all in actual IPs host: xxxxx isLocalHost"

ÒÔеÄconfig serverÆô¶¯mongos£¬Ò²ÐèÒªÖØÆôconfig server£¬·ñÔò»áÓдíÎóÌáʾ£º
¡°could not verify config servers were active and reachable before write¡±

Èç¹û¸ÄÍêºóÃæÓÖ³öÏÖ ¡°mongos specified a different config database string¡± ´íÎó£¬ÄÇô»¹ÐèÒªÖØÆômongod£¬

ÐÞ¸ÄÁËconfig server ¼¸ºõÊÇҪȫ²¿ÊµÀýÖØÆô¡£ÁíÍ⣬ÔÚÅäÖÃreplica setʱҲ²»µÃʹÓÃlocalhost¡¢127.0.0.1¡£

23¡¢shard keyµÄÑ¡Ôñ¸úupdateÐÔÄܽôÃܹØÁª

·Ö²¼Ê½MongoDB£¬shard keyµÄÑ¡Ôñ¸úupdateÐÔÄÜ£¬ÉõÖÁÊÇupdate¿ÉÓÃÐÔÓкܴó¹ØÏµ£¬ÐèҪעÒâ¡£

1¡¢ÔÚ¶ÔÎĵµ¸ö±ð×Ö¶Îupdateʱ£¬Èç¹ûquery²¿·ÖûÓдøÉÏshard key£¬ÐÔÄÜ»áºÜ²î£¬ÒòΪmongosÐèÒª°ÑÕâÌõupdateÓï¾äÅÉ·¢¸øËùÓеÄshard ʵÀý¡£

2¡¢µ±update µÄupsert²ÎÊýΪtrueʱ£¬query²¿·Ö±ØÐë´øÉÏ shard key£¬·ñÔòÓï¾äÖ´Ðгö´í£¬Àý×Ó£º

mongos> db.test.update({"_id":".7269993106A92327A89ABCD70D46AD5"}, 
{"$set":{"P": "aaa"}, "$setOnInsert":{"TEST":"a"}}, true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 61,
"errmsg" : "upsert { q: { _id: \".7269993106A92327A89ABCD70D46AD5\" }, u: { $set: { P: "aaa" }, $setOnInsert: { TEST: \"a\" } }, multi: false, upsert: true } does not contain shard key for pattern { _: 1.0, B: 1.0 }"
}
})

ÕâÊÇÒòΪÈç¹ûûÓÐshard key£¬mongos¼È²»ÄÜÔÚËùÓÐshardʵÀýÉÏÖ´ÐÐÕâÌõÓï¾ä£¨¿ÉÄܻᵼÖÂÿ¸öshard¶¼²åÈëÊý¾Ý£©£¬Ò²ÎÞ·¨Ñ¡ÔñÔÚij¸öshardÉÏÖ´ÐÐÕâÌõÓï¾ä£¬ÓÚÊdzö´íÁË¡£

ÁíÍ⣬ÐèÒªÌØ±ð×¢Ò⣬Èç¹ûʹÓÃpymongoÒýÇæ£¬Ëü²»»á¸æËßÄã³ö´íÁË£¬Ö»ÊǺ¯Êýµ÷ÓÃÏÝÈë²»·µ»Ø£¬ÔÚshellÏÂÖ´ÐвÅÄÜ¿´µ½´íÎóÐÅÏ¢¡£

¸½£º

It's actually not clear to me that this is something we can support - problem is this:

> db.coll.update({ _id : 1 }, { }, true);
> db.coll.find()
{ "_id" : ObjectId("53176700a2bc4d46c176f14a") }

Upserts generate new _ids in response to this operation, and therefore we can't actually target this correctly in a sharded environment. The shard on which we need to perform the query may not be the shard on which the new _id is placed.

Òâ˼ÊÇ˵£¬upsert²úÉúÁËеÄ_id£¬_id¾ÍÊÇshard key£¬µ«ÊÇÈç¹ûqueryÀïûÓÐshard key£¬ËüÃDz»ÖªµÀÒªµ½ÄĸöshardÉÏÖ´ÐÐÕâ¸öÃüÁupsert²úÉúµÄshard key¿ÉÄܲ¢²»ÊÇÖ´ÐÐÕâÌõÃüÁîµÄshardµÄ¡£

ÁíÍ⣬Èç¹û_id²»ÊÇshard keyÎÒÃǵÄÀý×ÓÒ²ÊDz»Äܳɹ¦µÄ£¬ÒòΪûÓÐshard key£¬ÕâÌõupsertÒªÔÚÄĸöshardÉÏÖ´ÐÐÄØ£¿²»ÄÜÏñÆÕͨupdateÄÇÑù¸øËùÓеÄshardÈ¥×ö£¬·ñÔò¿ÉÄܵ¼Ö²åÈë¶àÌõ¡£

24¡¢Í¨¹ýrepairDatabaseÌá¸ßÐÔÄÜ

´Ódb.stats()ÖпÉÒÔ¿´µ½¼¸¸ö¸úË鯬Ïà¹ØµÄ¹Ø¼ü×ֶΣ¬dataSize£¬±íʾÊý¾ÝµÄ´óС£¬Ëü°üº¬ÁËpaddingµÄ¿Õ¼ä£»storageSize£¬±íʾÕâЩÊý¾Ý´æ´¢Õ¼ÓõĿռ䣬°üº¬ÁËdataSizeºÍ±»É¾³ýÊý¾ÝËùÕ¼¿Õ¼ä£¬¿ÉÒÔÈÏΪstorageSize/dataSize¾ÍÊÇ´ÅÅÌË鯬±ÈÀý£¬µ±É¾³ý¡¢updateÎĵµ±È½Ï¶àºó£¬Ëü»á±ä´ó£¬¿¼ÂÇ×örepairDatabase£¬ÒÔ¼õÉÙË鯬ÈÃÊý¾Ý¸ü½ô´Õ£¬ÔÚʵ¼ùÖУ¬Õâ¶ÔÌá¸ßCURDÐÔÄܼ«ÆäÓÐÓá£repairDatabaseʱÐèҪעÒ⣺ËüÊǰÑÊý¾Ý¿½±´µ½Ðµĵط½£¬È»ºóÔÙ×ö´¦Àí£¬ËùÒÔrepair֮ǰÔÚDBĿ¼ËùÔÚ´ÅÅÌÐèÒªÔ¤ÁôÒ»±¶µÄ¿ÕÏдÅÅ̿ռ䣬Èç¹ûÄã·¢ÏÖ´ÅÅ̿ռ䲻×㣬¿ÉÒÔÍ£Ö¹·þÎñ£¬È»ºóÔö¼ÓÒ»¿éдÅÅÌ£¬ÔÙÖ´ÐÐʵÀý¼¶±ðµÄrepair£¬²¢Ö¸¶¨--repairpathΪдÅÅÌ·¾¶£¬eg£ºmongod --dbpath /path/to/corrupt/data --repair --repairpath /media/external-hd/data/db£¬ÊµÀýµÄÊý¾Ý»á¿½±´µ½/media/external-hd/data/dbÉÏ×ö´¦Àí¡£

25¡¢Ë÷Òý×ֶεij¤¶È²»ÄÜ´óÓÚ1024×Ö½Ú

Ë÷Òý×ֶεij¤¶È²»ÄÜ´óÓÚ1024×Ö½Ú£¬·ñÔòshellÏ»áÓвåÈë´íÎóÌáʾ£º"errmsg" : "insertDocument :: caused by :: 17280 Btree::insert: key too large to index¡±¡£

ʹÓÃpymongoµÄ¡°continue_on_error¡±²ÎÊý£¬²»»á·¢³ö´íÎóÌáʾ£¬Òª×¢Òâ¡£

²Î¿¼£ºhttp://docs.mongodb.org/manual/reference/limits/#Index-Key-Limit

26¡¢ÐÞ¸ÄË÷ÒýµÄexpireAfterSecondsÖ®ºó£¬¸ºÔؾùºâʧ°Ü

ÐÞ¸ÄË÷ÒýµÄexpireAfterSecondsÖ®ºó£¬¸ºÔؾùºâʧ°Ü£¬³öÏÖ´íÎóÌáʾ

¡°2015-06-05T09:59:49.056+0800 [migrateThread] warning: failed to create index before migrating data. 
 idx: { v: 1, key: { _: -1 }, name: "__-1", ns: "cswuyg_test.cswuyg_test", 
expireAfterSeconds: 5227200 } error: IndexOptionsConflict Index with name: __-1 already exists with different options

¼ì²é·¢ÉúmoveChunkµÄÁ½¸öshard£¬²¢Ã»Óз¢ÏÖ²»Ò»Ö£¬»³ÒÉ´æÔÚ»º´æ£¬ÖØÆôËùÓÐshard½â¾ö¡£

27¡¢config DBÎÞ·¨Ð´Èë

Òòconfig DBÎÞ·¨Ð޸ģ¬Ö»¿É¶Á£¬µ¼ÖÂdrop¡¢enableshardingʧ°Ü£º

config server Ïà¹ØÈÕÖ¾£º2015-06-11T16:51:19.078+0800 [replmaster] local.oplog.$main Assertion failure isOk() src/mongo/db/storage/extent.h 80

mongos Ïà¹ØÈÕÖ¾£º [LockPinger] warning: pinging failed for distributed lock pinger 'xxx:1234/xxx:1235:1433993544:1804289383'. : : caused by :: isOk()

ÕâÊÇͬÊÂÓöµ½µÄÎÊÌ⣬²»È·¶¨ÊÇʲô²Ù×÷ÒýÆðµÄ¡£ÖØÆô¡¢configdb×örepair¾ùÎÞ·¨½â¾ö¡£

×îºóͨ¹ýdump¡¢restore½â¾ö£º

£¨1£©°Ñ¾Éconfigdb dump³öÀ´£»

£¨2£©restoreµ½ÐµÄconfigure server£»

£¨3£©mongos²ÉÓÃеÄconfigure server£»

£¨4£©ÖØÆôÈ«²¿mongod¡£

   
7239 ´Îä¯ÀÀ       29
Ïà¹ØÎÄÕÂ

»ùÓÚEAµÄÊý¾Ý¿â½¨Ä£
Êý¾ÝÁ÷½¨Ä££¨EAÖ¸ÄÏ£©
¡°Êý¾Ýºþ¡±£º¸ÅÄî¡¢ÌØÕ÷¡¢¼Ü¹¹Óë°¸Àý
ÔÚÏßÉ̳ÇÊý¾Ý¿âϵͳÉè¼Æ ˼·+Ч¹û
 
Ïà¹ØÎĵµ

GreenplumÊý¾Ý¿â»ù´¡Åàѵ
MySQL5.1ÐÔÄÜÓÅ»¯·½°¸
ijµçÉÌÊý¾ÝÖÐ̨¼Ü¹¹Êµ¼ù
MySQL¸ßÀ©Õ¹¼Ü¹¹Éè¼Æ
Ïà¹Ø¿Î³Ì

Êý¾ÝÖÎÀí¡¢Êý¾Ý¼Ü¹¹¼°Êý¾Ý±ê×¼
MongoDBʵս¿Î³Ì
²¢·¢¡¢´óÈÝÁ¿¡¢¸ßÐÔÄÜÊý¾Ý¿âÉè¼ÆÓëÓÅ»¯
PostgreSQLÊý¾Ý¿âʵսÅàѵ
×îл¼Æ»®
DeepSeekÔÚÈí¼þ²âÊÔÓ¦ÓÃʵ¼ù 4-12[ÔÚÏß]
DeepSeek´óÄ£ÐÍÓ¦Óÿª·¢Êµ¼ù 4-19[ÔÚÏß]
UAF¼Ü¹¹ÌåϵÓëʵ¼ù 4-11[±±¾©]
AIÖÇÄÜ»¯Èí¼þ²âÊÔ·½·¨Óëʵ¼ù 5-23[ÉϺ£]
»ùÓÚ UML ºÍEA½øÐзÖÎöÉè¼Æ 4-26[±±¾©]
ÒµÎñ¼Ü¹¹Éè¼ÆÓ뽨ģ 4-18[±±¾©]

MySQLË÷Òý±³ºóµÄÊý¾Ý½á¹¹
MySQLÐÔÄܵ÷ÓÅÓë¼Ü¹¹Éè¼Æ
SQL ServerÊý¾Ý¿â±¸·ÝÓë»Ö¸´
ÈÃÊý¾Ý¿â·ÉÆðÀ´ 10´óDB2ÓÅ»¯
oracleµÄÁÙʱ±í¿Õ¼äдÂú´ÅÅÌ
Êý¾Ý¿âµÄ¿çƽ̨Éè¼Æ

²¢·¢¡¢´óÈÝÁ¿¡¢¸ßÐÔÄÜÊý¾Ý¿â
¸ß¼¶Êý¾Ý¿â¼Ü¹¹Éè¼ÆÊ¦
HadoopÔ­ÀíÓëʵ¼ù
Oracle Êý¾Ý²Ö¿â
Êý¾Ý²Ö¿âºÍÊý¾ÝÍÚ¾ò
OracleÊý¾Ý¿â¿ª·¢Óë¹ÜÀí

GE Çø¿éÁ´¼¼ÊõÓëʵÏÖÅàѵ
º½Ìì¿Æ¹¤Ä³×Ó¹«Ë¾ Nodejs¸ß¼¶Ó¦Óÿª·¢
ÖÐÊ¢Òæ»ª ׿Խ¹ÜÀíÕß±ØÐë¾ß±¸µÄÎåÏîÄÜÁ¦
ijÐÅÏ¢¼¼Êõ¹«Ë¾ PythonÅàѵ
ij²©²ÊITϵͳ³§ÉÌ Ò×ÓÃÐÔ²âÊÔÓëÆÀ¹À
ÖйúÓÊ´¢ÒøÐÐ ²âÊÔ³ÉÊì¶ÈÄ£Ðͼ¯³É(TMMI)
ÖÐÎïÔº ²úÆ·¾­ÀíÓë²úÆ·¹ÜÀí