{"id":21708,"date":"2024-01-02T13:59:23","date_gmt":"2024-01-02T13:59:23","guid":{"rendered":"https:\/\/server.hk\/blog\/21708\/"},"modified":"2024-11-14T10:28:18","modified_gmt":"2024-11-14T02:28:18","slug":"how-to-fix-mongodb-error-code-118-namespacenotsharded","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21708\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 118 &#8211; NamespaceNotSharded"},"content":{"rendered":"<br \/>\n<h1 id=\"how-to-fix-mongodb-error-code-118-namespacenotsharded-oIBLHzMwOS\">\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 118 &#8211; NamespaceNotSharded<\/h1>\n<p>MongoDB \u662f\u4e00\u500b\u53d7\u6b61\u8fce\u7684 NoSQL \u8cc7\u6599\u5eab\uff0c\u63d0\u4f9b\u9ad8\u6548\u80fd\u3001\u53ef\u64f4\u5c55\u6027\u548c\u9748\u6d3b\u6027\u3002\u7136\u800c\uff0c\u8207\u5176\u4ed6\u8edf\u9ad4\u4e00\u6a23\uff0cMongoDB \u53ef\u80fd\u6703\u9047\u5230\u9700\u8981\u89e3\u6c7a\u7684\u932f\u8aa4\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f\u932f\u8aa4\u4ee3\u78bc &#8211; 118 &#8211; NamespaceNotSharded\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u542b\u7fa9\u4ee5\u53ca\u5982\u4f55\u4fee\u5fa9\u5b83\u3002<\/p>\n<h2 id=\"understanding-error-code-118-namespacenotsharded-oIBLHzMwOS\">\u7406\u89e3\u932f\u8aa4\u4ee3\u78bc &#8211; 118 &#8211; NamespaceNotSharded<\/h2>\n<p>\u5728\u4f7f\u7528 MongoDB \u6642\uff0c\u5206\u7247\uff08Sharding\uff09\u662f\u4e00\u7a2e\u5c07\u8cc7\u6599\u5206\u4f48\u5230\u591a\u53f0\u6a5f\u5668\u6216\u5206\u7247\u4e0a\u7684\u6280\u8853\u3002\u5b83\u5141\u8a31\u6c34\u5e73\u64f4\u5c55\u4e26\u63d0\u9ad8\u6548\u80fd\u3002\u7576\u5728\u61c9\u8a72\u5206\u7247\u7684\u96c6\u5408\u6216\u547d\u540d\u7a7a\u9593\u4e0a\u57f7\u884c\u67e5\u8a62\u800c\u5be6\u969b\u4e0a\u8a72\u96c6\u5408\u4e26\u672a\u5206\u7247\u6642\uff0c\u5c31\u6703\u51fa\u73fe NamespaceNotSharded \u932f\u8aa4\u3002<\/p>\n<p>\u9810\u8a2d\u60c5\u6cc1\u4e0b\uff0cMongoDB \u4e0d\u6703\u81ea\u52d5\u5c0d\u96c6\u5408\u9032\u884c\u5206\u7247\u3002\u60a8\u9700\u8981\u660e\u78ba\u5730\u70ba\u6307\u5b9a\u7684\u96c6\u5408\u6216\u547d\u540d\u7a7a\u9593\u555f\u7528\u5206\u7247\u3002\u5982\u679c\u60a8\u5617\u8a66\u5728\u672a\u5206\u7247\u7684\u96c6\u5408\u4e0a\u57f7\u884c\u61c9\u5206\u7247\u7684\u67e5\u8a62\uff0cMongoDB \u5c31\u6703\u62cb\u51fa\u932f\u8aa4\u4ee3\u78bc &#8211; 118 &#8211; NamespaceNotSharded\u3002<\/p>\n<h2 id=\"fixing-error-code-118-namespacenotsharded-oIBLHzMwOS\">\u4fee\u5fa9\u932f\u8aa4\u4ee3\u78bc &#8211; 118 &#8211; NamespaceNotSharded<\/h2>\n<p>\u8981\u4fee\u5fa9 NamespaceNotSharded \u932f\u8aa4\uff0c\u60a8\u9700\u8981\u70ba\u76f8\u61c9\u7684\u96c6\u5408\u6216\u547d\u540d\u7a7a\u9593\u555f\u7528\u5206\u7247\u3002\u8acb\u6309\u4ee5\u4e0b\u6b65\u9a5f\u64cd\u4f5c\uff1a<\/p>\n<h3 id=\"step-1-connect-to-mongodb-oIBLHzMwOS\">\u6b65\u9a5f 1\uff1a\u9023\u63a5\u5230 MongoDB<\/h3>\n<p>\u6253\u958b\u4e00\u500b MongoDB \u5ba2\u6236\u7aef\uff0c\u5982 MongoDB shell\uff0c\u4e26\u9023\u63a5\u5230\u60a8\u7684 MongoDB \u5be6\u4f8b\u3002<\/p>\n<pre><code>$ mongo<\/code><\/pre>\n<h3 id=\"step-2-enable-sharding-oIBLHzMwOS\">\u6b65\u9a5f 2\uff1a\u555f\u7528\u5206\u7247<\/h3>\n<p>\u5728\u70ba\u96c6\u5408\u555f\u7528\u5206\u7247\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5148\u70ba\u5305\u542b\u8a72\u96c6\u5408\u7684\u8cc7\u6599\u5eab\u555f\u7528\u5206\u7247\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u70ba\u6307\u5b9a\u7684\u8cc7\u6599\u5eab\u555f\u7528\u5206\u7247\uff1a<\/p>\n<pre><code>sh.enableSharding(\"your_database_name\")<\/code><\/pre>\n<h3 id=\"step-3-shard-the-collection-oIBLHzMwOS\">\u6b65\u9a5f 3\uff1a\u5206\u7247\u96c6\u5408<\/h3>\n<p>\u7576\u8cc7\u6599\u5eab\u555f\u7528\u5206\u7247\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5c0d\u7279\u5b9a\u96c6\u5408\u9032\u884c\u5206\u7247\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c0d\u96c6\u5408\u9032\u884c\u5206\u7247\uff1a<\/p>\n<pre><code>sh.shardCollection(\"your_database_name.your_collection_name\", {\"shard_key\": \"hashed\"})<\/code><\/pre>\n<p>\u5c07 &#8220;your_database_name&#8221; \u66ff\u63db\u70ba\u60a8\u7684\u8cc7\u6599\u5eab\u540d\u7a31\uff0c&#8221;your_collection_name&#8221; \u66ff\u63db\u70ba\u60a8\u7684\u96c6\u5408\u540d\u7a31\u3002&#8221;shard_key&#8221; \u61c9\u66ff\u63db\u70ba\u7528\u4f5c\u5206\u7247\u9375\u7684\u9069\u7576\u6b04\u4f4d\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5728 &#8220;mydb&#8221; \u8cc7\u6599\u5eab\u4e2d\u6709\u4e00\u500b\u540d\u70ba &#8220;users&#8221; \u7684\u96c6\u5408\uff0c\u4e26\u4e14\u60a8\u60f3\u6839\u64da &#8220;username&#8221; \u6b04\u4f4d\u9032\u884c\u5206\u7247\uff0c\u5247\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code>sh.shardCollection(\"mydb.users\", {\"username\": \"hashed\"})<\/code><\/pre>\n<h3 id=\"step-4-verify-sharding-oIBLHzMwOS\">\u6b65\u9a5f 4\uff1a\u9a57\u8b49\u5206\u7247<\/h3>\n<p>\u57f7\u884c shardCollection \u547d\u4ee4\u5f8c\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u9a57\u8b49\u96c6\u5408\u662f\u5426\u6210\u529f\u5206\u7247\uff1a<\/p>\n<pre><code>sh.status()<\/code><\/pre>\n<p>\u6b64\u547d\u4ee4\u5c07\u986f\u793a MongoDB \u5be6\u4f8b\u4e2d\u5206\u7247\u96c6\u5408\u7684\u76f8\u95dc\u8cc7\u8a0a\u3002<\/p>\n<h2 id=\"summary-oIBLHzMwOS\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cMongoDB \u4e2d\u7684\u932f\u8aa4\u4ee3\u78bc &#8211; 118 &#8211; NamespaceNotSharded \u767c\u751f\u5728\u5728\u672a\u5206\u7247\u7684\u96c6\u5408\u4e0a\u57f7\u884c\u61c9\u5206\u7247\u7684\u67e5\u8a62\u6642\u3002\u8981\u4fee\u5fa9\u6b64\u932f\u8aa4\uff0c\u60a8\u9700\u8981\u70ba\u76f8\u61c9\u7684\u96c6\u5408\u6216\u547d\u540d\u7a7a\u9593\u555f\u7528\u5206\u7247\u3002\u901a\u904e\u672c\u6587\u4e2d\u7684\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u6210\u529f\u89e3\u6c7a NamespaceNotSharded \u932f\u8aa4\uff0c\u4e26\u78ba\u4fdd\u60a8\u7684 MongoDB \u90e8\u7f72\u4e2d\u6b63\u78ba\u5be6\u65bd\u4e86\u5206\u7247\u3002<\/p>\n<p>\u6b32\u4e86\u89e3\u66f4\u591a\u95dc\u65bc VPS \u8a17\u7ba1\u4ee5\u53ca\u5b83\u5982\u4f55\u5e6b\u52a9\u60a8\u7684 MongoDB \u90e8\u7f72\uff0c\u8acb\u8003\u616e\u700f\u89bd <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u4ed6\u5011\u63d0\u4f9b\u7684\u9802\u7d1a VPS \u89e3\u6c7a\u65b9\u6848\u5c07\u5e6b\u52a9\u60a8\u5be6\u73fe MongoDB \u8cc7\u6599\u5eab\u7684\u6700\u4f73\u6548\u80fd\u548c\u64f4\u5c55\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-21708","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=21708"}],"version-history":[{"count":2,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21708\/revisions"}],"predecessor-version":[{"id":23690,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21708\/revisions\/23690"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}