{"id":63898,"date":"2024-10-14T03:24:15","date_gmt":"2024-10-13T19:24:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/63898\/"},"modified":"2024-10-14T03:24:15","modified_gmt":"2024-10-13T19:24:15","slug":"mongodb%e8%a4%87%e8%a3%bd%e9%9b%86%e9%85%8d%e7%bd%ae%ef%bc%9a%e6%93%81%e6%9c%89%e6%9b%b4%e5%a4%9a%e7%a9%a9%e5%ae%9a%e5%8f%af%e9%9d%a0%e6%80%a7%ef%bc%88mongodb%e8%a4%87%e8%a3%bd%e9%9b%86%e9%85%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/63898\/","title":{"rendered":"MongoDB\u8907\u88fd\u96c6\u914d\u7f6e\uff1a\u64c1\u6709\u66f4\u591a\u7a69\u5b9a\u53ef\u9760\u6027\uff08mongodb\u8907\u88fd\u96c6\u914d\u7f6e\uff09"},"content":{"rendered":"<h1 id=\"mongodb%e8%a4%87%e8%a3%bd%e9%9b%86%e9%85%8d%e7%bd%ae%ef%bc%9a%e6%93%81%e6%9c%89%e6%9b%b4%e5%a4%9a%e7%a9%a9%e5%ae%9a%e5%8f%af%e9%9d%a0%e6%80%a7-SycIkbOXEZ\">MongoDB\u8907\u88fd\u96c6\u914d\u7f6e\uff1a\u64c1\u6709\u66f4\u591a\u7a69\u5b9a\u53ef\u9760\u6027<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u9760\u6027\u81f3\u95dc\u91cd\u8981\u3002MongoDB\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684NoSQL\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u529f\u80fd\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u6301\u4e45\u6027\u548c\u53ef\u7528\u6027\u3002\u5176\u4e2d\uff0c\u8907\u88fd\u96c6\uff08Replica Set\uff09\u662fMongoDB\u7684\u4e00\u500b\u6838\u5fc3\u7279\u6027\uff0c\u80fd\u5920\u63d0\u9ad8\u6578\u64da\u7684\u53ef\u9760\u6027\u548c\u53ef\u7528\u6027\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMongoDB\u8907\u88fd\u96c6\u7684\u914d\u7f6e\u53ca\u5176\u5e36\u4f86\u7684\u7a69\u5b9a\u6027\u512a\u52e2\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afmongodb%e8%a4%87%e8%a3%bd%e9%9b%86%ef%bc%9f-SycIkbOXEZ\">\u4ec0\u9ebc\u662fMongoDB\u8907\u88fd\u96c6\uff1f<\/h2>\n<p>MongoDB\u7684\u8907\u88fd\u96c6\u662f\u4e00\u7d44MongoDB\u4f3a\u670d\u5668\u7684\u96c6\u5408\uff0c\u9019\u4e9b\u4f3a\u670d\u5668\u5171\u540c\u7dad\u8b77\u76f8\u540c\u7684\u6578\u64da\u96c6\u3002\u8907\u88fd\u96c6\u4e2d\u7684\u4e00\u500b\u4f3a\u670d\u5668\u88ab\u6307\u5b9a\u70ba\u4e3b\u4f3a\u670d\u5668\uff08Primary\uff09\uff0c\u5176\u9918\u7684\u4f3a\u670d\u5668\u5247\u70ba\u5f9e\u4f3a\u670d\u5668\uff08Secondary\uff09\u3002\u4e3b\u4f3a\u670d\u5668\u8ca0\u8cac\u8655\u7406\u6240\u6709\u7684\u5beb\u5165\u8acb\u6c42\uff0c\u800c\u5f9e\u4f3a\u670d\u5668\u5247\u901a\u904e\u8907\u88fd\u4e3b\u4f3a\u670d\u5668\u7684\u6578\u64da\u4f86\u4fdd\u6301\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<h2 id=\"%e8%a4%87%e8%a3%bd%e9%9b%86%e7%9a%84%e5%84%aa%e5%8b%a2-SycIkbOXEZ\">\u8907\u88fd\u96c6\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u9ad8\u53ef\u7528\u6027\uff1a<\/strong>\u7576\u4e3b\u4f3a\u670d\u5668\u767c\u751f\u6545\u969c\u6642\uff0c\u8907\u88fd\u96c6\u53ef\u4ee5\u81ea\u52d5\u9078\u8209\u4e00\u500b\u65b0\u7684\u4e3b\u4f3a\u670d\u5668\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7684\u6301\u7e8c\u904b\u884c\u3002<\/li>\n<li><strong>\u6578\u64da\u5197\u9918\uff1a<\/strong>\u6578\u64da\u5728\u591a\u500b\u4f3a\u670d\u5668\u4e0a\u9032\u884c\u8907\u88fd\uff0c\u9019\u6a23\u5373\u4f7f\u67d0\u500b\u4f3a\u670d\u5668\u51fa\u73fe\u554f\u984c\uff0c\u6578\u64da\u4ecd\u7136\u53ef\u4ee5\u5f9e\u5176\u4ed6\u4f3a\u670d\u5668\u4e2d\u6062\u5fa9\u3002<\/li>\n<li><strong>\u8b80\u53d6\u64f4\u5c55\uff1a<\/strong>\u5f9e\u4f3a\u670d\u5668\u53ef\u4ee5\u7528\u65bc\u8655\u7406\u8b80\u53d6\u8acb\u6c42\uff0c\u5f9e\u800c\u6e1b\u8f15\u4e3b\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\uff0c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%aemongodb%e8%a4%87%e8%a3%bd%e9%9b%86-SycIkbOXEZ\">\u5982\u4f55\u914d\u7f6eMongoDB\u8907\u88fd\u96c6<\/h2>\n<p>\u914d\u7f6eMongoDB\u8907\u88fd\u96c6\u7684\u904e\u7a0b\u76f8\u5c0d\u7c21\u55ae\uff0c\u4ee5\u4e0b\u662f\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"%e6%ad%a5%e9%a9%9f1%ef%bc%9a%e5%ae%89%e8%a3%9dmongodb-SycIkbOXEZ\">\u6b65\u9a5f1\uff1a\u5b89\u88ddMongoDB<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u5728\u6240\u6709\u4f3a\u670d\u5668\u4e0a\u5b89\u88dd\u4e86MongoDB\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt-get install -y mongodb<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f2%ef%bc%9a%e5%95%9f%e5%8b%95mongodb%e5%af%a6%e4%be%8b-SycIkbOXEZ\">\u6b65\u9a5f2\uff1a\u555f\u52d5MongoDB\u5be6\u4f8b<\/h3>\n<p>\u5728\u6bcf\u500b\u4f3a\u670d\u5668\u4e0a\u555f\u52d5MongoDB\u5be6\u4f8b\uff0c\u4e26\u78ba\u4fdd\u5b83\u5011\u53ef\u4ee5\u76f8\u4e92\u901a\u4fe1\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5MongoDB\uff1a<\/p>\n<pre><code>sudo service mongodb start<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f3%ef%bc%9a%e9%85%8d%e7%bd%ae%e8%a4%87%e8%a3%bd%e9%9b%86-SycIkbOXEZ\">\u6b65\u9a5f3\uff1a\u914d\u7f6e\u8907\u88fd\u96c6<\/h3>\n<p>\u5728\u4e3b\u4f3a\u670d\u5668\u4e0a\uff0c\u4f7f\u7528MongoDB shell\u9032\u884c\u8907\u88fd\u96c6\u7684\u914d\u7f6e\u3002\u9996\u5148\uff0c\u9032\u5165MongoDB shell\uff1a<\/p>\n<pre><code>mongo<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521d\u59cb\u5316\u8907\u88fd\u96c6\uff1a<\/p>\n<pre><code>rs.initiate()<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6dfb\u52a0\u5f9e\u4f3a\u670d\u5668\u5230\u8907\u88fd\u96c6\u4e2d\uff1a<\/p>\n<pre><code>rs.add(\"secondary1:27017\")<\/code><\/pre>\n<pre><code>rs.add(\"secondary2:27017\")<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f4%ef%bc%9a%e6%aa%a2%e6%9f%a5%e8%a4%87%e8%a3%bd%e9%9b%86%e7%8b%80%e6%85%8b-SycIkbOXEZ\">\u6b65\u9a5f4\uff1a\u6aa2\u67e5\u8907\u88fd\u96c6\u72c0\u614b<\/h3>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5\u8907\u88fd\u96c6\u7684\u72c0\u614b\uff1a<\/p>\n<pre><code>rs.status()<\/code><\/pre>\n<p>\u9019\u5c07\u986f\u793a\u8907\u88fd\u96c6\u7684\u7576\u524d\u72c0\u614b\uff0c\u5305\u62ec\u4e3b\u4f3a\u670d\u5668\u548c\u5f9e\u4f3a\u670d\u5668\u7684\u5065\u5eb7\u72c0\u6cc1\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-SycIkbOXEZ\">\u7d50\u8ad6<\/h2>\n<p>MongoDB\u7684\u8907\u88fd\u96c6\u914d\u7f6e\u4e0d\u50c5\u80fd\u63d0\u9ad8\u6578\u64da\u7684\u53ef\u9760\u6027\uff0c\u9084\u80fd\u78ba\u4fdd\u7cfb\u7d71\u7684\u9ad8\u53ef\u7528\u6027\u3002\u901a\u904e\u5408\u7406\u7684\u914d\u7f6e\uff0c\u4f01\u696d\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\uff0c\u6e1b\u5c11\u6545\u969c\u5e36\u4f86\u7684\u5f71\u97ff\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u53ef\u9760\u6578\u64da\u5b58\u5132\u7684\u61c9\u7528\u4f86\u8aaa\uff0cMongoDB\u7684\u8907\u88fd\u96c6\u7121\u7591\u662f\u4e00\u500b\u7406\u60f3\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684MongoDB\u8907\u88fd\u96c6\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u5be6\u73fe\u6700\u4f73\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MongoDB\u8907\u88fd\u96c6\u914d\u7f6e\uff0c\u63d0\u5347\u6578\u64da\u7a69\u5b9a\u6027\u8207\u53ef\u9760\u6027\uff0c\u78ba\u4fdd\u9ad8\u53ef\u7528\u6027\u8207\u6545\u969c\u6062\u5fa9\u80fd\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-63898","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63898","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=63898"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63898\/revisions"}],"predecessor-version":[{"id":63899,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63898\/revisions\/63899"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=63898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=63898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=63898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}