{"id":1080,"date":"2022-04-02T16:26:33","date_gmt":"2022-04-02T08:26:33","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=1080"},"modified":"2022-04-04T15:49:18","modified_gmt":"2022-04-04T07:49:18","slug":"5-mongodb%e5%a4%87%e4%bb%bd%e6%81%a2%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=1080","title":{"rendered":"5.MongoDB\u5907\u4efd\u6062\u590d"},"content":{"rendered":"\n<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6\u7fa4\u67b6\u6784\u4e0e\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u66fe\u8d1f\u8d23\u56fd\u5185\u67d0\u5927\u578b\u535a\u5ba2\u7f51\u7ad9\u8fd0\u7ef4\u5de5\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">1.\u5907\u4efd\u6062\u590d\u5de5\u5177\u4ecb\u7ecd<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff081\uff09**   mongoexport\/mongoimport\n\uff082\uff09***** mongodump\/mongorestore\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.\u5907\u4efd\u5de5\u5177\u533a\u522b\u5728\u54ea\u91cc<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>mongoexport\/mongoimport:json csv \n1\u3001\u5f02\u6784\u5e73\u53f0\u8fc1\u79fb  mysql  &lt;---&gt; mongodb\n2\u3001\u540c\u5e73\u53f0\uff0c\u8de8\u5927\u7248\u672c\uff1amongodb 2  ----&gt; mongodb 3\nmongodump\/mongorestore\n\u65e5\u5e38\u5907\u4efd\u6062\u590d\u65f6\u4f7f\u7528.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">3.\u5bfc\u51fa\u5de5\u5177mongoexport<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>mongoexport\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\u6240\u793a\uff1a\n$ mongoexport --help  \n\u53c2\u6570\u8bf4\u660e\uff1a\n-h:\u6307\u660e\u6570\u636e\u5e93\u5bbf\u4e3b\u673a\u7684IP\n-u:\u6307\u660e\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\n-p:\u6307\u660e\u6570\u636e\u5e93\u7684\u5bc6\u7801\n-d:\u6307\u660e\u6570\u636e\u5e93\u7684\u540d\u5b57\n-c:\u6307\u660ecollection\u7684\u540d\u5b57\n-f:\u6307\u660e\u8981\u5bfc\u51fa\u90a3\u4e9b\u5217\n-o:\u6307\u660e\u5230\u8981\u5bfc\u51fa\u7684\u6587\u4ef6\u540d\n-q:\u6307\u660e\u5bfc\u51fa\u6570\u636e\u7684\u8fc7\u6ee4\u6761\u4ef6\n--authenticationDatabase admin\n\n1.\u5355\u8868\u5907\u4efd\u81f3json\u683c\u5f0f\nmongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldboy -c log -o \/mongodb\/log.json\n\u6ce8\uff1a\u5907\u4efd\u6587\u4ef6\u7684\u540d\u5b57\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff0c\u9ed8\u8ba4\u5bfc\u51fa\u4e86JSON\u683c\u5f0f\u7684\u6570\u636e\u3002\n\n2. \u5355\u8868\u5907\u4efd\u81f3csv\u683c\u5f0f\n\u5982\u679c\u6211\u4eec\u9700\u8981\u5bfc\u51faCSV\u683c\u5f0f\u7684\u6570\u636e\uff0c\u5219\u9700\u8981\u4f7f\u7528----type=csv\u53c2\u6570\uff1a\n mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldboy -c log --type=csv -f uid,name,age,date  -o \/mongodb\/log.csv\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">4.\u5bfc\u5165\u5de5\u5177mongoimport<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mongoimport --help\n\u53c2\u6570\u8bf4\u660e\uff1a\n-h:\u6307\u660e\u6570\u636e\u5e93\u5bbf\u4e3b\u673a\u7684IP\n-u:\u6307\u660e\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\n-p:\u6307\u660e\u6570\u636e\u5e93\u7684\u5bc6\u7801\n-d:\u6307\u660e\u6570\u636e\u5e93\u7684\u540d\u5b57\n-c:\u6307\u660ecollection\u7684\u540d\u5b57\n-f:\u6307\u660e\u8981\u5bfc\u5165\u90a3\u4e9b\u5217\n-j, --numInsertionWorkers=&lt;number&gt;  number of insert operations to run concurrently                                                  (defaults to 1)\n\/\/\u5e76\u884c\n\u6570\u636e\u6062\u590d:\n1.\u6062\u590djson\u683c\u5f0f\u8868\u6570\u636e\u5230log1\nmongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldboy -c log1 \/mongodb\/log.json\n2.\u6062\u590dcsv\u683c\u5f0f\u7684\u6587\u4ef6\u5230log2\n\u4e0a\u9762\u6f14\u793a\u7684\u662f\u5bfc\u5165JSON\u683c\u5f0f\u7684\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u5982\u679c\u8981\u5bfc\u5165CSV\u683c\u5f0f\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u5219\u9700\u8981\u901a\u8fc7--type\u53c2\u6570\u6307\u5b9a\u5bfc\u5165\u683c\u5f0f\uff0c\u5177\u4f53\u5982\u4e0b\u6240\u793a\uff1a\n\u9519\u8bef\u7684\u6062\u590d\n\n\u6ce8\u610f\uff1a\n\uff081\uff09csv\u683c\u5f0f\u7684\u6587\u4ef6\u5934\u884c\uff0c\u6709\u5217\u540d\u5b57\nmongoimport   -uroot -proot123 --port 27017 --authenticationDatabase admin   -d oldboy -c log2 --type=csv --headerline --file  \/mongodb\/log.csv\n\uff082\uff09csv\u683c\u5f0f\u7684\u6587\u4ef6\u5934\u884c\uff0c\u6ca1\u6709\u5217\u540d\u5b57\nmongoimport   -uroot -proot123 --port 27017 --authenticationDatabase admin   -d oldboy -c log3 --type=csv -f id,name,age,date --file  \/mongodb\/log.csv\n--headerline:\u6307\u660e\u7b2c\u4e00\u884c\u662f\u5217\u540d\uff0c\u4e0d\u9700\u8981\u5bfc\u5165\u3002\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">5.MySQL\u6570\u636e\u8fc1\u79fbMongoDB\u6848\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>world\u6570\u636e\u5e93\u4e0bcity\u8868\u8fdb\u884c\u5bfc\u51fa\uff0c\u5bfc\u5165\u5230mongodb\n\n\uff081\uff09mysql\u5f00\u542f\u5b89\u5168\u8def\u5f84\nvim \/etc\/my.cnf   ---&gt;\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\nsecure-file-priv=\/tmp\n\n--\u91cd\u542f\u6570\u636e\u5e93\u751f\u6548\n\/etc\/init.d\/mysqld restart\n\n\uff082\uff09\u5bfc\u51famysql\u7684city\u8868\u6570\u636e\nsource \/root\/world.sql\n\nselect * from world.city into outfile '\/tmp\/city1.csv' fields terminated by ',';\n\n\uff083\uff09\u5904\u7406\u5907\u4efd\u6587\u4ef6\ndesc world.city\n  ID          | int(11)  | NO   | PRI | NULL    | auto_increment |\n| Name        | char(35) | NO   |     |         |                |\n| CountryCode | char(3)  | NO   | MUL |         |                |\n| District    | char(20) | NO   |     |         |                |\n| Population\n\nvim \/tmp\/city.csv   ----&gt; \u6dfb\u52a0\u7b2c\u4e00\u884c\u5217\u540d\u4fe1\u606f\n\nID,Name,CountryCode,District,Population\n\n(4)\u5728mongodb\u4e2d\u5bfc\u5165\u5907\u4efd\nmongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d world  -c city --type=csv -f ID,Name,CountryCode,District,Population --file  \/tmp\/city1.csv\n\nuse world\ndb.city.find({CountryCode:\"CHN\"});\n\nps:world\u5171100\u5f20\u8868\uff0c\u5168\u90e8\u8fc1\u79fb\u5230mongodb\n\nselect table_name ,group_concat(column_name) from columns where table_schema='world' group by table_name;\n\nselect * from world.city into outfile '\/tmp\/world_city.csv' fields terminated by ',';\n\nselect concat(\"select * from \",table_schema,\".\",table_name ,\" into outfile '\/tmp\/\",table_schema,\"_\",table_name,\".csv' fields terminated by ',';\")\nfrom information_schema.tables where table_schema ='world';\n\n\u5bfc\u5165\uff1a\n\u63d0\u793a\uff0c\u4f7f\u7528infomation_schema.columns + information_schema.tables\n\nmysql\u5bfc\u51facsv\uff1a\nselect * from test_info   \ninto outfile '\/tmp\/test.csv'   \nfields terminated by ','\u3000\u3000\u3000 ------\u5b57\u6bb5\u95f4\u4ee5,\u53f7\u5206\u9694\noptionally enclosed by '\"'\u3000\u3000 ------\u5b57\u6bb5\u7528\"\u53f7\u62ec\u8d77\nescaped by '\"'   \u3000\u3000\u3000\u3000\u3000\u3000  ------\u5b57\u6bb5\u4e2d\u4f7f\u7528\u7684\u8f6c\u4e49\u7b26\u4e3a\"\nlines terminated by '\\r\\n';\u3000\u3000------\u884c\u4ee5\\r\\n\u7ed3\u675f\n\nmysql\u5bfc\u5165csv\uff1a\nload data infile '\/tmp\/test.csv'   \ninto table test_info    \nfields terminated by ','  \noptionally enclosed by '\"' \nescaped by '\"'   \nlines terminated by '\\r\\n';\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">6.mongodump\u548cmongorestore\u5907\u4efd\u6062\u590d\u5de5\u5177<\/h2>\n\n\n\n<p><strong><em>1.\u4ecb\u7ecd<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongodump\u80fd\u591f\u5728Mongodb\u8fd0\u884c\u65f6\u8fdb\u884c\u5907\u4efd\uff0c\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u662f\u5bf9\u8fd0\u884c\u7684Mongodb\u505a\u67e5\u8be2\uff0c\u7136\u540e\u5c06\u6240\u6709\u67e5\u5230\u7684\u6587\u6863\u5199\u5165\u78c1\u76d8\u3002\n\u4f46\u662f\u5b58\u5728\u7684\u95ee\u9898\u65f6\u4f7f\u7528mongodump\u4ea7\u751f\u7684\u5907\u4efd\u4e0d\u4e00\u5b9a\u662f\u6570\u636e\u5e93\u7684\u5b9e\u65f6\u5feb\u7167\uff0c\u5982\u679c\u6211\u4eec\u5728\u5907\u4efd\u65f6\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u4e86\u5199\u5165\u64cd\u4f5c\uff0c\n\u5219\u5907\u4efd\u51fa\u6765\u7684\u6587\u4ef6\u53ef\u80fd\u4e0d\u5b8c\u5168\u548cMongodb\u5b9e\u65f6\u6570\u636e\u76f8\u7b49\u3002\u53e6\u5916\u5728\u5907\u4efd\u65f6\u53ef\u80fd\u4f1a\u5bf9\u5176\u5b83\u5ba2\u6237\u7aef\u6027\u80fd\u4ea7\u751f\u4e0d\u5229\u7684\u5f71\u54cd\u3002\n<\/code><\/pre>\n\n\n\n<p><strong><em>2.mongodump\u7528\u6cd5\u5982\u4e0b<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mongodump --help\n\u53c2\u6570\u8bf4\u660e\uff1a\n-h:\u6307\u660e\u6570\u636e\u5e93\u5bbf\u4e3b\u673a\u7684IP\n-u:\u6307\u660e\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\n-p:\u6307\u660e\u6570\u636e\u5e93\u7684\u5bc6\u7801\n-d:\u6307\u660e\u6570\u636e\u5e93\u7684\u540d\u5b57\n-c:\u6307\u660ecollection\u7684\u540d\u5b57\n-o:\u6307\u660e\u5230\u8981\u5bfc\u51fa\u7684\u6587\u4ef6\u540d\n-q:\u6307\u660e\u5bfc\u51fa\u6570\u636e\u7684\u8fc7\u6ee4\u6761\u4ef6\n-j, --numParallelCollections=  number of collections to dump in parallel (4 by default)\n--oplog  \u5907\u4efd\u7684\u540c\u65f6\u5907\u4efdoplog\n# \u5168\u5e93\u5907\u4efd\nmkdir \/mongodb\/backup\nmongodump  -uroot -proot123 --port 27017 --authenticationDatabase admin -o \/mongodb\/backup\n\n# \u5907\u4efdtest\u5e93\nmongodump   -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -o \/mongodb\/backup\/\n\n# \u5907\u4efdoldboy\u5e93\u4e0b\u7684log\u96c6\u5408\nmongodump   -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldboy -c log -o \/mongodb\/backup\/\n\n# \u538b\u7f29\u5907\u4efd\n\nmongodump   -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -o \/mongodb\/backup\/ --gzip\n<\/code><\/pre>\n\n\n\n<p><strong><em>3.mongorestore\u6062\u590d\u5de5\u5177<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mongod@db01 ~]$ mongorestore   -uroot -proot123 --port 27017 --authenticationDatabase admin  \/mongodb\/backup\/ --gzip\n\n# \u5168\u5e93\u6062\u590d \n&#91;mongod@db01 ~]$ mongorestore   -uroot -proot123 --port 27017 --authenticationDatabase admin  \/mongodb\/backup\/ --gzip --drop \n\n# \u6062\u590dtest\u5e93\n&#91;mongod@db01 ~]$ mongorestore   -uroot -proot123 --port 27017 --authenticationDatabase admin  -d sfy \/mongodb\/backup\/test  --gzip --drop \n\n\u6062\u590dtest\u5e93\u4e0b\u7684t100w\u96c6\u5408\n\n&#91;mongod@db01 ~]$ mongorestore   -uroot -proot123 --port 27017 --authenticationDatabase admin  -d sfy  -c t1  \/mongodb\/backup\/test\/t100w.bson.gz  --gzip \n\ndrop\u8868\u793a\u6062\u590d\u7684\u65f6\u5019\u628a\u4e4b\u524d\u7684\u96c6\u5408drop\u6389(\u5371\u9669)\n\n$ mongorestore  -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldboy --drop\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.mongodump\u548cmongorestore\u9ad8\u7ea7\u4f01\u4e1a\u5e94\u7528\uff08--oplog\uff09<\/h2>\n\n\n\n<p><strong><em>1.oplog\u4ecb\u7ecd<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6ce8\u610f\uff1a\u8fd9\u662freplica set \u6a21\u5f0f\u4e13\u7528\n--oplog\n use oplog for taking a point-in-time snapshot\n \n\u5728replica set\u4e2doplog\u662f\u4e00\u4e2a\u5b9a\u5bb9\u96c6\u5408\uff08capped collection\uff09\uff0c\u5b83\u7684\u9ed8\u8ba4\u5927\u5c0f\u662f\u78c1\u76d8\u7a7a\u95f4\u76845%\uff08\u53ef\u4ee5\u901a\u8fc7--oplogSizeMB\u53c2\u6570\u4fee\u6539\uff09.\n\u4f4d\u4e8elocal\u5e93\u7684db.oplog.rs\uff0c\u6709\u5174\u8da3\u53ef\u4ee5\u770b\u770b\u91cc\u9762\u5230\u5e95\u6709\u4e9b\u4ec0\u4e48\u5185\u5bb9\u3002\n\u5176\u4e2d\u8bb0\u5f55\u7684\u662f\u6574\u4e2amongod\u5b9e\u4f8b\u4e00\u6bb5\u65f6\u95f4\u5185\u6570\u636e\u5e93\u7684\u6240\u6709\u53d8\u66f4\uff08\u63d2\u5165\/\u66f4\u65b0\/\u5220\u9664\uff09\u64cd\u4f5c\u3002\n\u5f53\u7a7a\u95f4\u7528\u5b8c\u65f6\u65b0\u8bb0\u5f55\u81ea\u52a8\u8986\u76d6\u6700\u8001\u7684\u8bb0\u5f55\u3002\n\u5176\u8986\u76d6\u8303\u56f4\u88ab\u79f0\u4f5coplog\u65f6\u95f4\u7a97\u53e3\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u56e0\u4e3aoplog\u662f\u4e00\u4e2a\u5b9a\u5bb9\u96c6\u5408\uff0c\n\u6240\u4ee5\u65f6\u95f4\u7a97\u53e3\u80fd\u8986\u76d6\u7684\u8303\u56f4\u4f1a\u56e0\u4e3a\u4f60\u5355\u4f4d\u65f6\u95f4\u5185\u7684\u66f4\u65b0\u6b21\u6570\u4e0d\u540c\u800c\u53d8\u5316\u3002\n\u60f3\u8981\u67e5\u770b\u5f53\u524d\u7684oplog\u65f6\u95f4\u7a97\u53e3\u9884\u8ba1\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a\n\n mongod -f \/mongodb\/28017\/conf\/mongod.conf \n mongod -f \/mongodb\/28018\/conf\/mongod.conf \n mongod -f \/mongodb\/28019\/conf\/mongod.conf \n mongod -f \/mongodb\/28020\/conf\/mongod.conf \n \n \n use local \n db.oplog.rs.find().pretty()\n\"ts\" : Timestamp(1553597844, 1),\n\"op\" : \"n\"\n\"o\"  :\n\n\"i\": insert\n\"u\": update\n\"d\": delete\n\"c\": db cmd\n\ntest:PRIMARY&gt; rs.printReplicationInfo()\nconfigured oplog size:   1561.5615234375MB &lt;--\u96c6\u5408\u5927\u5c0f\nlog length start to end: 423849secs (117.74hrs) &lt;--\u9884\u8ba1\u7a97\u53e3\u8986\u76d6\u65f6\u95f4\noplog first event time:  Wed Sep 09 2015 17:39:50 GMT+0800 (CST)\noplog last event time:   Mon Sep 14 2015 15:23:59 GMT+0800 (CST)\nnow:                     Mon Sep 14 2015 16:37:30 GMT+0800 (CST)\n<\/code><\/pre>\n\n\n\n<p><strong><em>2.oplog\u4f01\u4e1a\u7ea7\u5e94\u7528<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff081\uff09\u5b9e\u73b0\u70ed\u5907\uff0c\u5728\u5907\u4efd\u65f6\u4f7f\u7528--oplog\u9009\u9879\n\n\uff082\uff09\u51c6\u5907\u6d4b\u8bd5\u6570\u636e\n&#91;mongod@db01 conf]$ mongo --port 28018\n\nuse oldboy\nfor(var i = 1 ;i &lt; 100; i++) {\n    db.foo.insert({a:i});\n}\n\nmy_repl:PRIMARY&gt; db.oplog.rs.find({\"op\":\"i\"}).pretty()\n\noplog \u914d\u5408mongodump\u5b9e\u73b0\u70ed\u5907\nmongodump --port 28018 --oplog -o \/mongodb\/backup\n\u4f5c\u7528\u4ecb\u7ecd\uff1a--oplog \u4f1a\u8bb0\u5f55\u5907\u4efd\u8fc7\u7a0b\u4e2d\u7684\u6570\u636e\u53d8\u5316\u3002\u4f1a\u4ee5oplog.bson\u4fdd\u5b58\u4e0b\u6765\n\u6062\u590d\nmongorestore  --port 28017 --oplogReplay \/mongodb\/bak --drop\n<\/code><\/pre>\n\n\n\n<p><strong><em>3.oplog\u9ad8\u7ea7\u5e94\u7528<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u80cc\u666f\uff1a\u6bcf\u592911\u70b9\u5168\u5907\uff0coplog\u6062\u590d\u7a97\u53e3\u4e3a48\u5c0f\u65f6\n\u67d0\u5929\uff0c\u4e0a\u534810\u70b9world.city \u4e1a\u52a1\u8868\u88ab\u8bef\u5220\u9664\u3002\n\u6062\u590d\u601d\u8def\uff1a\n    0\u3001\u505c\u5e94\u7528\n    2\u3001\u627e\u6d4b\u8bd5\u5e93\n    3\u3001\u6062\u590d\u6628\u5929\u665a\u4e0a\u5168\u5907\n    4\u3001\u622a\u53d6\u5168\u5907\u4e4b\u540e\u5230world.city\u8bef\u5220\u9664\u65f6\u95f4\u70b9\u7684oplog\uff0c\u5e76\u6062\u590d\u5230\u6d4b\u8bd5\u5e93\n    5\u3001\u5c06\u8bef\u5220\u9664\u8868\u5bfc\u51fa\uff0c\u6062\u590d\u5230\u751f\u4ea7\u5e93\n\n\u6062\u590d\u6b65\u9aa4\uff1a\n\u6a21\u62df\u6545\u969c\u73af\u5883\uff1a\n\n1\u3001\u5168\u5907\u6570\u636e\u5e93\n\u6a21\u62df\u539f\u59cb\u6570\u636e\n\nmongo --port 28017\nuse test\nfor(var i = 1 ;i &lt; 100; i++) {\n    db.a.insert({a: i});\n}\n\n\u5168\u5907:\nrm -rf \/mongodb\/backup\/*\nmongodump --port 28017 --oplog -o \/mongodb\/backup\n\n--oplog\u529f\u80fd:\u5728\u5907\u4efd\u540c\u65f6,\u5c06\u5907\u4efd\u8fc7\u7a0b\u4e2d\u4ea7\u751f\u7684\u65e5\u5fd7\u8fdb\u884c\u5907\u4efd\n\u6587\u4ef6\u5fc5\u987b\u5b58\u653e\u5728\/mongodb\/backup\u4e0b,\u81ea\u52a8\u547d\u4ee4\u4e3aoplog.bson\n\n\u518d\u6b21\u6a21\u62df\u6570\u636e\ndb.b.insert({id:1})\ndb.c.insert({id:2})\n\n2\u3001\u4e0a\u534810\u70b9\uff1a\u5220\u9664wo\u5e93\u4e0b\u7684ci\u8868\n10:00\u65f6\u523b,\u8bef\u5220\u9664\n\n3\u3001\u5907\u4efd\u73b0\u6709\u7684oplog.rs\u8868\nmongodump --port 28017 -d local -c oplog.rs  -o \/mongodb\/bak\n\n4\u3001\u622a\u53d6oplog\u5e76\u6062\u590d\u5230drop\u4e4b\u524d\u7684\u4f4d\u7f6e\n\u66f4\u5408\u7406\u7684\u65b9\u6cd5\uff1a\u767b\u9646\u5230\u539f\u6570\u636e\u5e93\n&#91;mongod@db03 local]$\t\nmy_repl:PRIMARY&gt; use local\ndb.oplog.rs.find({op:\"c\"}).pretty();\n\n{\n\t\"ts\" : Timestamp(1606212278, 1),\n\t\"t\" : NumberLong(3),\n\t\"h\" : NumberLong(0),\n\t\"v\" : 2,\n\t\"op\" : \"c\",\n\t\"ns\" : \"test.$cmd\",\n\t\"ui\" : UUID(\"091af5ca-20c2-4ea4-a015-7b42de975220\"),\n\t\"o2\" : {\n\t\t\"numRecords\" : 1\n\t},\n\t\"wall\" : ISODate(\"2020-11-24T10:04:38.310Z\"),\n\t\"o\" : {\n\t\t\"drop\" : \"b\"\n\t}\n}\n\u83b7\u53d6\u5230oplog\u8bef\u5220\u9664\u65f6\u95f4\u70b9\u4f4d\u7f6e:\n\t\"ts\" : Timestamp(1606212278, 1),\n\n 5\u3001\u6062\u590d\u5907\u4efd+\u5e94\u7528oplog\n&#91;mongod@db01 backup]$ cp \/mongodb\/bak\/local\/oplog.rs.bson .\/oplog.bson \nmongorestore --port 28017  --oplogReplay --oplogLimit \"1606212278:1\"  --drop   \/mongodb\/backup\/\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/1080"}],"collection":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1080"}],"version-history":[{"count":2,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/1080\/revisions"}],"predecessor-version":[{"id":1082,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/1080\/revisions\/1082"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}