zoukankan      html  css  js  c++  java
  • MongoDB 备份(mongodump)与恢复(mongorestore)

    MongoDB  备份(mongodump)与恢复(mongorestore)

    备份:使用mongodump命令导出所有数据库到指定目录

    参数说明:
       --host:MongoDB所在服务器IP。
       --port:MongoDB所在服务器端口。
       -d:需要备份的数据库实例。
       -o:备份的数据存放位置。
     -u : 指定登录用户
    -p : 指定登录用户的密码
    -c : 指定要备份的集合
    --authenticationDatabase 验证数据库名称



    如果备份出现这个错误
     Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed

    看这个帖子 mongodb 使用mongodump备份

    1.备份指定的库

     执行命令备份成功, -d dbname

    [root@MongoDB ~]# mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -o /tmp/mongobak/ --authenticationDatabase admin
    writing db1.user to 
    writing db1.chat to 
    done dumping db1.user (3 documents)
    done dumping db1.chat (3 documents)
    [root@MongoDB ~]# lsdb1

    2.备份指定的集合

     -d dbname 
    -c collection_name

    [root@MongoDB mongobak]# mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -c chat -o /tmp/mongobak/ --authenticationDatabase admin
    writing db1.chat to 
    done dumping db1.chat (3 documents)
    [root@MongoDB mongobak]# ls
    db1
    
    
    

    3.备份所有的库

    不加 -d -c 参数

    [root@MongoDB mongobak]# mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -o /tmp/mongobak/ --authenticationDatabase admin
    2019-04-14T00:49:41.752+0800    writing admin.system.users to 
    2019-04-14T00:49:41.778+0800    done dumping admin.system.users (1 document)
    2019-04-14T00:49:41.778+0800    writing admin.system.version to 
    2019-04-14T00:49:41.806+0800    done dumping admin.system.version (2 documents)
    2019-04-14T00:49:41.806+0800    writing db1.user to 
    2019-04-14T00:49:41.806+0800    writing db1.chat to 
    2019-04-14T00:49:41.839+0800    done dumping db1.user (3 documents)
    2019-04-14T00:49:41.840+0800    done dumping db1.chat (3 documents)
    [root@MongoDB mongobak]# ls
    admin  db1

    恢复:使用mongorestore命令来导入备份的数据。

    参数说明:
       -h --host:MongoDB所在服务器IP。
       --port:MongoDB所在服务器端口。
       -d:需要恢复的数据库实例。
     -u : 指定登录用户
    -p : 指定登录用户的密码
    -c : 指定要恢复的集合
     --drop :恢复的时候把之前集合drop掉

    不用-o ,直接指定存放备份monggo数据的目录

    如果恢复命令出现这个错误

    Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed
    在命令加上 --authenticationDatabase admin
    --authenticationDatabase 验证数据库名称



    1.恢复所有的库

    [root@MongoDB mongobak]# mongorestore --host 127.0.0.1 --port 27017 -u admin -p 123456  /tmp/mongobak/ 
    
    

    2.恢复指定的库

    -d 指定恢复的库名字

    删除数据库

    > use db1
    switched to db db1
    > db.dropDatabase()
    { "dropped" : "db1", "ok" : 1 }
    [root@MongoDB mongobak]# mongorestore --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1  /tmp/mongobak/db1/ --authenticationDatabase admin
    > use db1
    switched to db db1
    > show tables
    chat
    user
    
    > db.user.find()
    { "_id" : ObjectId("5ca7a4b0219efd687462f965"), "id" : 1, "name" : "jack", "age" : 73 }
    { "_id" : ObjectId("5ca7a4c4219efd687462f968"), "id" : 4, "name" : "xiaogang", "age" : 34, "hobby" : [ "篮球" ] }
    { "_id" : ObjectId("5ca7a4c4219efd687462f969"), "id" : 5, "name" : "ben", "age" : 24 }

    3.恢复指定的集合

    -c 指定恢复的集合名字

    删除集合先

    > use db1
    switched to db db1
    > db.user.drop()
    true
    > show tables
    chat

    恢复命令

    
    
    [root@MongoDB mongobak]# mongorestore --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -c user /tmp/mongobak/db1/user.bson --authenticationDatabase admin
    
    

    恢复

    > use db1
    switched to db db1
    > show tables
    chat
    user
     
  • 相关阅读:
    raw_input() 与 input()对比
    你很熟悉CSS,却没掌握这些CSS技巧
    CSS样式设置
    javascript基本语法和变量(转)
    手机/移动前端开发需要注意的20个要点
    移动端”宴席知多少
    git第一次提交代码到远程仓库
    java对过反射调用方法
    站点收集
    别人抢红包,我们研究一下红包算法
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/10701752.html
Copyright © 2011-2022 走看看