{"id":199,"date":"2019-03-20T14:27:02","date_gmt":"2019-03-20T06:27:02","guid":{"rendered":"https:\/\/www.aiqinqin.info\/?p=199"},"modified":"2019-03-28T11:15:11","modified_gmt":"2019-03-28T03:15:11","slug":"mongodb%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.aiqinqin.info\/?p=199","title":{"rendered":"mongodb\u7684\u4f7f\u7528"},"content":{"rendered":"<blockquote><p>\n  \u6700\u8fd1\u7528\u5230\u4e86mongodb\uff0c\u6b63\u597d\u91cd\u65b0\u6574\u7406\u4e00\u4e0bmongodb\u7684\u76f8\u5173\u77e5\u8bc6\n<\/p><\/blockquote>\n<ul>\n<li>\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ul>\n<pre><code class=\"\">## \u8bbe\u7f6e\u7ba1\u7406\u5458\nuse admin\ndb.createUser(\n  {\n    user: \"username\", \n    pwd: \"passwd\", \n    roles: [ { role: \"userAdminAnyDatabase\", db: \"admin\" } ] \n  }\n)\n\nmongo --host hostname --port portNumber -u username -p password --authenticationDatabase \"admin\"  ## \u8fdb\u5165mongo\u6570\u636e\u5e93\n\ndb.version(); ## \u5f53\u524ddb\u7248\u672c\ndb.stats(); ## \u663e\u793a\u5f53\u524ddb\u72b6\u6001\nshow users\uff1a## \u663e\u793a\u7528\u6237\nshow dbs;   ## \u67e5\u8be2\u6240\u6709\u6570\u636e\u5e93\nuse databasename ; ## \u5207\u6362\u6570\u636e\u5e93\uff0c\u5982\u679c\u8be5\u6570\u636e\u5e93\u4e0d\u5b58\u5728\uff0c\u5c31\u521b\u5efa\u8be5\u6570\u636e\u5e93\ndb.getName(); ## \u67e5\u770b\u5f53\u524d\u4f7f\u7528\u7684\u6570\u636e\u5e93\nshow collections\uff1a## \u663e\u793a\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u7684\u96c6\u5408\uff08\u7c7b\u4f3c\u5173\u7cfb\u6570\u636e\u5e93\u4e2d\u7684\u8868\uff09\ndb.collectionName.drop() ## \u5220\u9664collectionName\u8fd9\u4e2a\u96c6\u5408\ndb.dropDatabase(); ## \u5220\u9664\u5f53\u524d\u4f7f\u7528\u6570\u636e\u5e93\n<\/code><\/pre>\n<ul>\n<li>\u5907\u4efd\u4e0e\u6062\u590d<\/li>\n<\/ul>\n<pre><code class=\"\">mongodump  --host hostname --port portNumber -u username -p password --authenticationDatabase=admin -d databasename -o \/opt\/backup\/backupname ## \u5907\u4efd\u4e00\u4e2a\u6570\u636e\u5e93\n\nmongorestore --host hostname --port portNumber -u username -p password --authenticationDatabase=admin -d databasename \/opt\/backup\/backupname ## \u8fd8\u539f\u4e00\u4e2a\u6570\u636e\u5e93\n\nPS: mongodump\u5bfc\u51fa\u7684\u662fbson\u683c\u5f0f\uff0c\u662f\u4e8c\u8fdb\u5236\u5f62\u5f0f\uff0c\u4e0d\u8fc7\u53ef\u4ee5\u4f7f\u7528mongo\u81ea\u5e26\u7684bsondump\u547d\u4ee4\u67e5\u770b\u91cc\u9762\u7684\u6570\u636e\uff0c\u800cmongoexport\u5bfc\u51fa\u7684\u5219\u662f\u6587\u672c\uff0c\u53ef\u4ee5\u662fcsv\u3001json\u683c\u5f0f\u3002JSON\u53ef\u8bfb\u6027\u5f3a\u4f46\u4f53\u79ef\u8f83\u5927\uff0cBSON\u5219\u662f\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u4f53\u79ef\u5c0f\u4f46\u5bf9\u4eba\u7c7b\u51e0\u4e4e\u6ca1\u6709\u53ef\u8bfb\u6027\u3002\u5728\u4e00\u4e9bmongodb\u7248\u672c\u4e4b\u95f4\uff0cBSON\u683c\u5f0f\u53ef\u80fd\u4f1a\u968f\u7248\u672c\u4e0d\u540c\u800c\u6709\u6240\u4e0d\u540c\uff0c\u6240\u4ee5\u4e0d\u540c\u7248\u672c\u4e4b\u95f4\u7528mongodump\/mongorestore\u53ef\u80fd\u4e0d\u4f1a\u6210\u529f\uff0c\u5177\u4f53\u8981\u770b\u7248\u672c\u4e4b\u95f4\u7684\u517c\u5bb9\u6027\u3002\u5f53\u65e0\u6cd5\u4f7f\u7528BSON\u8fdb\u884c\u8de8\u7248\u672c\u7684\u6570\u636e\u8fc1\u79fb\u7684\u65f6\u5019\uff0c\u4f7f\u7528JSON\u683c\u5f0f\u5373mongoexport\/mongoimport\u662f\u4e00\u4e2a\u53ef\u9009\u9879\u3002\u8de8\u7248\u672c\u7684mongodump\/mongorestore\u4e2a\u4eba\u5e76\u4e0d\u63a8\u8350\uff0c\u5b9e\u5728\u8981\u505a\u8bf7\u5148\u68c0\u67e5\u6587\u6863\u770b\u4e24\u4e2a\u7248\u672c\u662f\u5426\u517c\u5bb9\uff08\u5927\u90e8\u5206\u65f6\u5019\u662f\u7684\uff09\u3002JSON\u867d\u7136\u5177\u6709\u8f83\u597d\u7684\u8de8\u7248\u672c\u901a\u7528\u6027\uff0c\u4f46\u5176\u53ea\u4fdd\u7559\u4e86\u6570\u636e\u90e8\u5206\uff0c\u4e0d\u4fdd\u7559\u7d22\u5f15\uff0c\u8d26\u6237\u7b49\u5176\u4ed6\u57fa\u7840\u4fe1\u606f\u3002\u4f7f\u7528\u65f6\u5e94\u8be5\u6ce8\u610f\n<\/code><\/pre>\n<blockquote><p>\n  \u53c2\u8003\u6587\u7ae0\uff1a<br \/>\n  https:\/\/www.cnblogs.com\/xiaotengyi\/p\/6393972.html<br \/>\n  https:\/\/www.cnblogs.com\/wpjzh\/p\/5999363.html\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u7528\u5230\u4e86mongodb\uff0c\u6b63\u597d\u91cd\u65b0\u6574\u7406\u4e00\u4e0bmongodb\u7684\u76f8\u5173\u77e5\u8bc6 \u57fa\u672c\u4f7f\u7528 ## \u8bbe\u7f6e\u7ba1\u7406\u5458 use adm [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,4],"tags":[],"class_list":["post-199","post","type-post","status-publish","format-standard","hentry","category-mongodb","category-database"],"_links":{"self":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts\/199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=199"}],"version-history":[{"count":8,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts\/199\/revisions"}],"predecessor-version":[{"id":277,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=\/wp\/v2\/posts\/199\/revisions\/277"}],"wp:attachment":[{"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiqinqin.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}