{"id":21786,"date":"2024-01-02T15:17:25","date_gmt":"2024-01-02T15:17:25","guid":{"rendered":"https:\/\/server.hk\/blog\/21786\/"},"modified":"2024-11-14T10:27:03","modified_gmt":"2024-11-14T02:27:03","slug":"how-to-fix-mongodb-error-code-203-shardingstatenotinitialized","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21786\/","title":{"rendered":"\u5982\u4f55\u89e3\u6c7a MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 203 &#8211; ShardingStateNotInitialized"},"content":{"rendered":"<h1 id=\"how-to-fix-mongodb-error-code-203-shardingstatenotinitialized-aJRUvYcTph\">\u5982\u4f55\u89e3\u6c7a MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 203 &#8211; ShardingStateNotInitialized<\/h1>\n<p>MongoDB \u662f\u4e00\u500b\u53d7\u6b61\u8fce\u7684 NoSQL \u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u9ad8\u6548\u7684\u53ef\u64f4\u5c55\u6027\u548c\u9748\u6d3b\u6027\u3002\u5b83\u5141\u8a31\u60a8\u9ad8\u6548\u5730\u5b58\u5132\u548c\u7ba1\u7406\u5927\u91cf\u6578\u64da\u3002\u7136\u800c\uff0c\u50cf\u5176\u4ed6\u8edf\u4ef6\u4e00\u6a23\uff0cMongoDB \u53ef\u80fd\u6703\u9047\u5230\u6703\u5e72\u64fe\u5176\u6b63\u5e38\u64cd\u4f5c\u7684\u932f\u8aa4\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 203 &#8211; ShardingStateNotInitialized\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-the-mongodb-error-code-203-shardingstatenotinitialized-aJRUvYcTph\">\u7406\u89e3 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 203 &#8211; ShardingStateNotInitialized<\/h2>\n<p>MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 203 &#8211; ShardingStateNotInitialized \u5728\u60a8\u5617\u8a66\u5c0d sharded \u96c6\u5408\u57f7\u884c\u64cd\u4f5c\u6642\u51fa\u73fe\uff0c\u800c\u6b64\u6642\u5206\u7247\u904e\u7a0b\u5c1a\u672a\u5b8c\u6210\u3002\u5206\u7247\u662f\u5c07\u6578\u64da\u5206\u4f48\u5230\u591a\u53f0\u6a5f\u5668\u4e0a\u7684\u904e\u7a0b\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u4e26\u8655\u7406\u5927\u6578\u64da\u96c6\u3002\u7576\u60a8\u70ba\u96c6\u5408\u555f\u7528\u5206\u7247\u6642\uff0cMongoDB \u6703\u5c07\u6578\u64da\u62c6\u5206\u70ba\u591a\u500b\u584a\u4e26\u5206\u4f48\u5230\u4e0d\u540c\u7684 shards \u4e0a\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5982\u679c\u60a8\u5728\u5206\u7247\u904e\u7a0b\u672a\u5b8c\u6210\u4e4b\u524d\u5617\u8a66\u5c0d\u8a72\u96c6\u5408\u57f7\u884c\u64cd\u4f5c\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230 ShardingStateNotInitialized \u932f\u8aa4\u3002\u6b64\u932f\u8aa4\u8868\u793a\u5206\u7247\u5143\u6578\u64da\u5c1a\u672a\u5b8c\u5168\u521d\u59cb\u5316\uff0c\u8a72\u96c6\u5408\u5c1a\u672a\u6e96\u5099\u597d\u9032\u884c\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"fixing-the-mongodb-error-code-203-shardingstatenotinitialized-aJRUvYcTph\">\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 203 &#8211; ShardingStateNotInitialized<\/h2>\n<p>\u8981\u4fee\u5fa9 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 203 &#8211; ShardingStateNotInitialized\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u5206\u7247\u904e\u7a0b\u5df2\u6210\u529f\u5b8c\u6210\u3002\u8acb\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u64cd\u4f5c\uff1a<\/p>\n<h3 id=\"step-1-check-the-sharding-status-aJRUvYcTph\">\u6b65\u9a5f 1\uff1a\u6aa2\u67e5\u5206\u7247\u72c0\u614b<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u6aa2\u67e5\u5206\u7247\u904e\u7a0b\u7684\u72c0\u614b\u3002\u6253\u958b MongoDB shell\uff0c\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sh.status()<\/code><\/pre>\n<p>\u6b64\u547d\u4ee4\u5c07\u986f\u793a\u5206\u7247\u904e\u7a0b\u7684\u7576\u524d\u72c0\u614b\u3002\u5982\u679c\u904e\u7a0b\u4ecd\u5728\u9032\u884c\u4e2d\uff0c\u8acb\u7b49\u5f85\u5176\u5b8c\u6210\u3002\u5982\u679c\u5df2\u7d93\u5b8c\u6210\uff0c\u8acb\u7e7c\u7e8c\u4e0b\u4e00\u6b65\u3002<\/p>\n<h3 id=\"step-2-enable-sharding-for-the-collection-aJRUvYcTph\">\u6b65\u9a5f 2\uff1a\u70ba\u96c6\u5408\u555f\u7528\u5206\u7247<\/h3>\n<p>\u5982\u679c\u5206\u7247\u904e\u7a0b\u5df2\u5b8c\u6210\uff0c\u4f46\u60a8\u4ecd\u7136\u9047\u5230 ShardingStateNotInitialized \u932f\u8aa4\uff0c\u5247\u53ef\u80fd\u9700\u8981\u986f\u5f0f\u555f\u7528\u8a72\u96c6\u5408\u7684\u5206\u7247\u3002\u5728 MongoDB shell \u4e2d\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sh.enableSharding(\"your_database.your_collection\")<\/code><\/pre>\n<p>\u5c07 &#8220;your_database&#8221; \u66ff\u63db\u70ba\u60a8\u7684\u6578\u64da\u5eab\u540d\u7a31\uff0c\u5c07 &#8220;your_collection&#8221; \u66ff\u63db\u70ba\u60a8\u5e0c\u671b\u9032\u884c\u5206\u7247\u7684\u96c6\u5408\u540d\u7a31\u3002\u6b64\u547d\u4ee4\u5c07\u70ba\u6307\u5b9a\u7684\u96c6\u5408\u555f\u7528\u5206\u7247\u3002<\/p>\n<h3 id=\"step-3-wait-for-balancer-to-migrate-data-aJRUvYcTph\">\u6b65\u9a5f 3\uff1a\u7b49\u5f85\u5e73\u8861\u5668\u9077\u79fb\u6578\u64da<\/h3>\n<p>\u5728\u70ba\u96c6\u5408\u555f\u7528\u5206\u7247\u5f8c\uff0c\u60a8\u9700\u8981\u7b49\u5f85\u5e73\u8861\u5668\u5c07\u6578\u64da\u9077\u79fb\u5230\u5404\u500b shards\u3002\u5e73\u8861\u5668\u8ca0\u8cac\u5c07\u6578\u64da\u584a\u5747\u52fb\u5206\u4f48\u5230 shards \u4e0a\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f86\u6aa2\u67e5\u5e73\u8861\u5668\u72c0\u614b\uff1a<\/p>\n<pre><code>sh.getBalancerState()<\/code><\/pre>\n<p>\u5982\u679c\u5e73\u8861\u5668\u4ecd\u5728\u904b\u884c\uff0c\u8acb\u7b49\u5f85\u5176\u5b8c\u6210\u3002\u4e00\u65e6\u5e73\u8861\u5668\u5b8c\u6210\u6578\u64da\u9077\u79fb\uff0cShardingStateNotInitialized \u932f\u8aa4\u61c9\u8a72\u6703\u5f97\u5230\u89e3\u6c7a\u3002<\/p>\n<h2 id=\"summary-aJRUvYcTph\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a0e\u8ad6\u4e86 MongoDB \u932f\u8aa4\u4ee3\u78bc &#8211; 203 &#8211; ShardingStateNotInitialized\u3002\u6211\u4eec\u4e86\u89e3\u5230\uff0c\u5f53\u60a8\u8bd5\u56fe\u5728\u5206\u7247\u96c6\u5408\u4e0a\u6267\u884c\u64cd\u4f5c\u65f6\uff0c\u82e5\u5206\u7247\u8fc7\u7a0b\u5c1a\u672a\u5b8c\u6210\uff0c\u5c31\u4f1a\u51fa\u73b0\u6b64\u9519\u8bef\u3002\u8981\u4fee\u590d\u6b64\u9519\u8bef\uff0c\u60a8\u9700\u8981\u786e\u8ba4\u5206\u7247\u8fc7\u7a0b\u5df2\u6210\u529f\u5b8c\u6210\u3002\u6211\u5011\u63d0\u4f9b\u4e86\u9010\u6b65\u6307\u5c0e\uff0c\u8aaa\u660e\u5982\u4f55\u6aa2\u67e5\u5206\u7247\u72c0\u614b\u3001\u70ba\u96c6\u5408\u555f\u7528\u5206\u7247\u548c\u7b49\u5f85\u5e73\u8861\u5668\u9077\u79fb\u6578\u64da\u3002\u901a\u904e\u9075\u5faa\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u89e3\u6c7a ShardingStateNotInitialized \u932f\u8aa4\uff0c\u4e26\u7e7c\u7e8c\u8655\u7406\u60a8\u7684 MongoDB \u5206\u7247\u96c6\u5408\u3002<\/p>\n<p>\u6b32\u4e86\u89e3\u6709\u95dc VPS \u6258\u7ba1\u89e3\u6c7a\u65b9\u6848\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u89e3\u6c7a 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":[101],"tags":[],"class_list":["post-21786","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21786","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=21786"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21786\/revisions"}],"predecessor-version":[{"id":23585,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21786\/revisions\/23585"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}