{"id":319,"date":"2012-05-16T20:46:10","date_gmt":"2012-05-16T12:46:10","guid":{"rendered":"http:\/\/www.royalwzy.com\/?p=319"},"modified":"2014-04-11T14:32:02","modified_gmt":"2014-04-11T06:32:02","slug":"mongodb%e5%ad%a6%e4%b9%a006-%e5%88%86%e7%89%87%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"http:\/\/www.royalwzy.com\/?p=319","title":{"rendered":"MongoDB\u5b66\u4e6006&#8211;\u5206\u7247\u6280\u672f"},"content":{"rendered":"<div>\u5206\u7247\u6280\u672f<\/p>\n<div>\n<ol>\n<li>\u5f53\u6570\u636e\u91cf\u8fbe\u5230T\u7ea7\u522b\u7684\u65f6\u5019,\u5bf9CPU,\u5185\u5b58\u548c\u78c1\u76d8\u7684\u538b\u529b\u90fd\u5f88\u5927,\u8fd9\u4e2a\u65f6\u5019\u5c31\u9700\u8981\u91c7\u7528\u5206\u7247\u6280\u672f,\u5c06MongoDB\u4e2d\u7684\u96c6\u5408\u8fdb\u884c\u62c6\u5206,\u5206\u62c5\u5230\u591a\u4e2a\u7247(\u5373\u591a\u53f0MongoDB\u670d\u52a1\u5668)\u4e0a;<\/li>\n<li>\u8de8\u670d\u52a1\u5668\u7684\u6570\u636e\u62c6\u5206\u4e2d,Sharding\u662f\u4e00\u4e2a\u6709\u6548\u7684\u65b9\u6cd5;MongoDB\u4e2d\u652f\u6301\u81ea\u52a8\u5316Sharding,\u4f46\u662f\u5bf9\u6570\u636e\u5e93\u6027\u80fd\u4f1a\u9020\u6210\u5f88\u5927\u5f71\u54cd;\u56e0\u6b64<span style=\"color: #e30000;\">\u5efa\u8bae\u7528\u6237\u5c3d\u65e9\u8fdb\u884cSharding,\u4f7f\u7528MMS:Munin (+ Mongo plugin)\u548cCloudWatch\u7b49\u5de5\u5177<\/span>\u5bf9MongoDB\u8fdb\u884c\u76d1\u63a7,\u786e\u4fdd\u7cfb\u7edf\u8d44\u6e90\u4f7f\u7528\u8fbe\u523080%\u4e4b\u524d\u5c31\u5b8c\u6210Sharding\u5de5\u4f5c;<\/li>\n<li>\u5206\u7247\u6280\u672f\u4e2d\u5404\u4e2a\u7ec4\u4ef6\u7684\u804c\u80fd\u53ca\u62d3\u6251\u56fe:\n<ol>\n<li>clients:\u5ba2\u6237\u7aef,\u5373\u53d1\u51fa\u6570\u636e\u8bf7\u6c42,\u5e76\u5f97\u5230\u8fd4\u56de\u7ed3\u679c;<\/li>\n<li>config:\u4fdd\u5b58\u6570\u636e\u548c\u7247\u7684\u5bf9\u5e94\u5173\u7cfb\u4ee5\u53ca\u76f8\u5e94\u7684\u914d\u7f6e\u4fe1\u606f,\u914d\u7f6e\u8282\u70b9\u63a8\u8350\u4f7f\u7528\u4f7f\u7528\u4e3b\u5907,\u9632\u6b62\u5355\u70b9\u6545\u969c;<\/li>\n<li>mongos:\u5373\u8def\u7531\u670d\u52a1\u5668,\u6839\u636e\u8bbe\u7f6e\u7684\u7247\u952e(\u5373\u96c6\u5408\u62c6\u5206\u7684\u4f9d\u636e)\u5c06\u6570\u636e\u5206\u644a\u5230\u4e0d\u540c\u7684mongod\u96c6\u7fa4;<\/li>\n<li>mongod:\u666e\u901a\u7684MongoDB\u670d\u52a1\u5668,\u5c31\u662f\u6240\u8c13\u7684\u7247;<\/li>\n<li>\u62d3\u6251\u56fe; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"file:\/\/\/C:\/TEMP\/enhtmlclip\/Image(176).png\" width=\"525\" height=\"297\" \/><\/li>\n<\/ol>\n<\/li>\n<li>\u4f7f\u7528\u4e0d\u540c\u7684\u76ee\u5f55\u548c\u7aef\u53e3\u6765\u6a21\u62df\u4e0d\u540c\u7684\u670d\u52a1\u5668:\n<ol>\n<li>config:mongod &#8211;dbpath=\/mongo\/config &#8211;port=20001;<\/li>\n<li>mongos:mongos &#8211;port=20002 &#8211;configdb=127.0.0.1:20001;<\/li>\n<li>mongod1:mongod &#8211;dbpath=\/mongo\/mongod1 &#8211;port=30001;<\/li>\n<li>mongod2:mongod &#8211;dbpath=\/mongo\/mongod2 &#8211;port=30002;<\/li>\n<li>mongod3:mongod &#8211;dbpath=\/mongo\/mongod3 &#8211;port=30003;<\/li>\n<\/ol>\n<\/li>\n<li>\u5f00\u542fconfig\u670d\u52a1\u5668:mongod &#8211;dbpath=\/mongo\/config &#8211;port=20001; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"file:\/\/\/C:\/TEMP\/enhtmlclip\/Image(177).png\" width=\"818\" height=\"456\" \/><\/li>\n<li>\u542f\u52a8mongos\u670d\u52a1\u5668(\u4e0d\u9700\u8981\u6307\u5b9a\u6570\u636e\u76ee\u5f55,\u56e0\u4e3a\u5b83\u53ea\u662f\u8d77\u5230\u8def\u7531\u5668\u7684\u529f\u80fd),\u540c\u65f6\u8981\u6307\u5b9aconfig\u670d\u52a1\u5668,\u56e0\u4e3a\u8981\u5199\u5165\u914d\u7f6e\u4fe1\u606f:mongos &#8211;port=20002 &#8211;configdb=127.0.0.1:20001;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"file:\/\/\/C:\/TEMP\/enhtmlclip\/Image(178).png\" width=\"815\" height=\"385\" \/><\/li>\n<li>\u5206\u522b\u542f\u52a8\u4e09\u53f0mongod\u670d\u52a1\u5668:mongod &#8211;dbpath=\/mongo\/mongodX &#8211;port=3000X;<\/li>\n<li>\u914d\u7f6emongos\u670d\u52a1:\n<ol>\n<li>\u8fde\u63a5mongos\u670d\u52a1\u5e76\u6dfb\u52a0\u5206\u7247\u8282\u70b9:mongo 127.0.0.1:20002\/admin; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"file:\/\/\/C:\/TEMP\/enhtmlclip\/Image(179).png\" width=\"813\" height=\"182\" \/><\/li>\n<li>\u5f00\u542f\u6570\u636e\u5e93\u7684\u5206\u7247\u529f\u80fd,\u4ee5test\u6570\u636e\u5e93\u4e3a\u4f8b:db.runCommand({&#8220;enablesharding&#8221;:&#8221;test&#8221;}); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"file:\/\/\/C:\/TEMP\/enhtmlclip\/Image(180).png\" width=\"817\" height=\"37\" \/><\/li>\n<li>\u6307\u5b9a\u96c6\u5408\u7684\u5206\u7247\u952e,\u4ee5user\u96c6\u5408\u7684name\u5217\u4e3a\u4f8b: \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"file:\/\/\/C:\/TEMP\/enhtmlclip\/Image(181).png\" width=\"815\" height=\"39\" \/><\/li>\n<\/ol>\n<\/li>\n<li>\u67e5\u770b\u6548\u679c:\n<ol>\n<li>\u901a\u8fc7mongos\u63d2\u516510w\u6761\u6d4b\u8bd5\u6570\u636e; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"file:\/\/\/C:\/TEMP\/enhtmlclip\/Image(182).png\" width=\"816\" height=\"122\" \/><\/li>\n<li>\u67e5\u770b\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5:db.printShardingStatus(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"file:\/\/\/C:\/TEMP\/enhtmlclip\/Image(183).png\" width=\"1049\" height=\"335\" \/><\/li>\n<li>\u7ed3\u679c\u5206\u6790:\n<ol>\n<li>shards:\u67e5\u770b\u5230\u5df2\u7ecf\u5206\u6210\u4e86shard0000,shard0001,shard0002\u4e09\u4e2a\u7247;<\/li>\n<li>databases:\u770b\u5230test\u7684partitioned\u5c5e\u6027\u4e3atrue;<\/li>\n<li>chundks:\u88ab\u5206\u6210\u4e86$minKey(\u65e0\u7a77\u5c0f)-&gt;user0, user0-&gt;user9999, user9999-&gt;$maxKey(\u65e0\u7a77\u5927)\u4e09\u6bb5;<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<div><\/div>\n<\/div>\n<div><\/div>\n<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; \u914d\u7f6emongos\u670d\u52a1\u5668\u00a0&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/div>\n<div>&#8212; 1.\u8fde\u63a5mongos\u670d\u52a1\u5668;<\/div>\n<div>mongo 127.0.0.1:20002\/admin<\/div>\n<div>&#8212; 2.\u6dfb\u52a0\u5206\u7247;<\/div>\n<div>db.runCommand({addshard:&#8221;127.0.0.1:30001&#8243;, allowLocal:true})<\/div>\n<div>db.runCommand({addshard:&#8221;127.0.0.1:30002&#8243;, allowLocal:true})<\/div>\n<div>db.runCommand({addshard:&#8221;127.0.0.1:30003&#8243;, allowLocal:true})<\/div>\n<div>&#8212; 3.\u5f00\u542f\u6570\u636e\u5e93\u7684\u5206\u7247\u529f\u80fd;<\/div>\n<div>db.runCommand({&#8220;enablesharding&#8221;:&#8221;test&#8221;})<\/div>\n<div>&#8212; 4.\u6307\u5b9a\u7247\u952e;<\/div>\n<div>db.runCommand({&#8220;shardcollection&#8221;:&#8221;test.user&#8221;, &#8220;key&#8221;:{&#8220;name&#8221;:1}})<\/div>\n<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; \u914d\u7f6emongos\u670d\u52a1\u5668\u00a0&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/div>\n<div><\/div>\n<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; \u63d2\u5165\u6d4b\u8bd5\u6570\u636e\u5e76\u67e5\u770b\u7ed3\u679c\u00a0&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/div>\n<div>&#8212; 1.\u8fde\u63a5mongos\u670d\u52a1\u5668\u7684test\u6570\u636e\u5e93;<\/div>\n<div>mongo 127.0.0.1:20002\/test<\/div>\n<div>&#8212; 2.\u63d2\u516510w\u6761\u6d4b\u8bd5\u6570\u636e;<\/div>\n<div>for(var i = 0; i&lt; 100000; i++){ db.user.insert({name:&#8221;user&#8221;+i, age:i}) }<\/div>\n<div>&#8212; 3.\u67e5\u770b\u6570\u636e\u5206\u5e03\u60c5\u51b5;<\/div>\n<div>db.printShardingStatus()<\/div>\n<div>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; \u63d2\u5165\u6d4b\u8bd5\u6570\u636e\u5e76\u67e5\u770b\u7ed3\u679c\u00a0&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u7247\u6280\u672f \u5f53\u6570\u636e\u91cf\u8fbe\u5230T\u7ea7\u522b\u7684\u65f6\u5019,\u5bf9CPU,\u5185\u5b58\u548c\u78c1\u76d8\u7684\u538b\u529b\u90fd\u5f88\u5927,\u8fd9\u4e2a\u65f6\u5019\u5c31\u9700\u8981\u91c7\u7528\u5206\u7247\u6280\u672f,\u5c06Mongo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[24,25],"class_list":["post-319","post","type-post","status-publish","format-standard","hentry","category-nosql","tag-mongodb","tag-nosql"],"_links":{"self":[{"href":"http:\/\/www.royalwzy.com\/index.php?rest_route=\/wp\/v2\/posts\/319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.royalwzy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.royalwzy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.royalwzy.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.royalwzy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=319"}],"version-history":[{"count":0,"href":"http:\/\/www.royalwzy.com\/index.php?rest_route=\/wp\/v2\/posts\/319\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.royalwzy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.royalwzy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.royalwzy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}