mongoDB的导出、导入、运行时的备份

来源:互联网 发布:怪奇物语 知乎 编辑:程序博客网 时间:2024/05/16 14:07

1、单个集合的导出,我用的都是带验证的开启方式   

C:\Users\Administrator>mongoexport -d text1 -c poot1 -o D:/aaaa.json -u NO1 -p 123 --authenticationDatabase admin
    -d 是数据库名

    -c是集合名

    -o是导入的存放地址

    -u是用户名,我这里用的是超级管理员

    -p是密码,

   --authenticationDatabase admin   这是是必须的,否则会报如下错误:

    2016-07-09T17:35:49.237+0800    error connecting to db server: server returned e
rror on SASL authentication step: Authentication failed.


2、导入单个的集合,我用的都是带验证的开启方式  

C:\Users\Administrator>mongoimport --db text1 --collection poot1 -u NO1 -p 123 --file D:/aaaa.json  --authenticationDatabase admin2016-07-09T18:00:28.769+0800    connected to: localhost2016-07-09T18:00:29.095+0800    imported 4 documents

上面的2个导入、导出有个很大的问题,就是在操作的时候,一切的增删改查都不能执行了,相当于是中断掉了。这是我们不可能允许的。

3、4、5、6讲的就是运行时的备份

3、整个数据库的导出,我用的都是带验证的开启方式  

C:\Users\Administrator>mongodump -d text1 -o D:/aaaa.bak -u NO1 -p 123 --authenticationDatabase admin

      和上观的解释一样一样的。

4、导入整个的数据库,我用的都是带验证的开启方式  ----------------注意,得把要导入的库找到才行 --dorp上留意

C:\Users\Administrator>mongorestore -u NO1 -p 123 -d text1 --drop D:\aaaa\text1 --authenticationDatabase admin2016-07-09T18:16:26.423+0800    building a list of collections to restore from D:\aaaa\text1 dir2016-07-09T18:16:26.427+0800    reading metadata for text1.poot1 from D:\aaaa\text1\poot1.metadata.json2016-07-09T18:16:26.428+0800    reading metadata for text1.aaa from D:\aaaa\text1\aaa.metadata.json2016-07-09T18:16:26.765+0800    restoring text1.poot1 from D:\aaaa\text1\poot1.bson2016-07-09T18:16:27.095+0800    restoring text1.aaa from D:\aaaa\text1\aaa.bson2016-07-09T18:16:27.095+0800    restoring indexes for collection text1.poot1 from metadata2016-07-09T18:16:27.096+0800    restoring indexes for collection text1.aaa frommetadata2016-07-09T18:16:27.097+0800    finished restoring text1.poot1 (4 documents)2016-07-09T18:16:27.097+0800    finished restoring text1.aaa (1 document)2016-07-09T18:16:27.098+0800    doneC:\Users\Administrator>

5、远程数据导出。----------------没有测试过

<span style="color:#FF0000;"><span style="color:#000000;">C:\Users\Administrator>mongodump --host 134.22.33.55 --port 27017 -d text1 -o D:/aaaa.bak -u NO1 -p 123 --authenticationDatabase admin</span></span>

6、远程导入整个库,我用的都是带验证的开启方式  ----------------没有测试过

C:\Users\Administrator>mongorestore --host 134.22.33.55 --port 27017 -u NO1 -p 123 -d text1 --drop D:\aaaa\text1 --authenticationDatabase admin

但运行时的备份也有问题,那就是缓冲区的数据不会被备份起来,也就是说会丢数据。下面就需要用到锁的概念,备份之前先把缓冲区的数据存到数据库再备份,备份完了再解锁。这样做出来就完美了。

7、加锁 -------------------------记住要在admin里做操作

> use adminswitched to db admin> db.runCommand({fsync:1,lock:1}){        "info" : "now locked against writes, use db.fsyncUnlock() to unlock",        "seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",        "ok" : 1}>

8、解锁

<span style="color:#000000;">> db.currentOp()</span>

9、数据库修复,在有数据不全,脏数据的情况下使用,及耗性能。不建意在生产环境使用

> db.repairDatabase()


0 0