MongoDB 备份数据( mongodump )

MongoDB mongodump 脚本命令可以用来备份 MongoDB 数据

MongoDB mongodump 脚本命令可以导出所有数据到指定目录中

语法

MongoDB mongodump 脚本命令语法如下:

mongodump -h dbhost -d dbname -o dbdirectory

参数说明

  • -h: 需要导出 MongDB 数据所在的服务器地址
    例如 127.0.0.1 ,当然也可以同时指定端口号:127.0.0.1:27017

  • -d: 需要备份的数据库,例 test

  • -o: 备份的数据存放位置,例如:/mnt/data/backup/mongodb/
    该目录需要提前建立,在备份完成后,系统自动在 mongodb 目录下建立一个 test 目录,这个目录里面存放该数据库实例的备份数据

mongodump 命令可选参数

  1. mongodump --host HOST_NAME --port PORT_NUMBER
    该命令将备份所有 MongoDB 数据
    mongodump --host db1.twle.cn --port 27017
    
  2. mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY
    该命令备份指定的 DB_PATH 数据库到 BACKUP_DIRECTORY 目录
    mongodump --dbpath /data/db/ --out /data/backup/
    
  3. mongodump --collection COLLECTION --db DB_NAME
    该命令将备份指定数据库 DB_NAME 的 COLLECTION 集合
    mongodump --collection lession --db test
    

范例

  1. 首先使用 --port 27017 启动 MongoDB 服务
  2. 打开命令提示符窗口,输入命令 mongodump

    $ mongodump
    

    执行以上命令后,客户端会连接到 ip 为 127.0.0.1 端口号为 27017 的 MongoDB 服务上,然后备份所有数据到 bin/dump/ 目录中

    命令输出结果如下

    $ mongodump
    2017-10-24T07:11:25.229+0800    writing admin.system.indexes to 
    2017-10-24T07:11:25.236+0800    done dumping admin.system.indexes (3 documents)
    2017-10-24T07:11:25.236+0800    writing admin.system.users to 
    2017-10-24T07:11:25.242+0800    done dumping admin.system.users (1 document)
    2017-10-24T07:11:25.242+0800    writing admin.system.version to 
    2017-10-24T07:11:25.244+0800    done dumping admin.system.version (1 document)
    2017-10-24T07:11:25.244+0800    writing nodebb.objects to 
    2017-10-24T07:11:25.244+0800    writing shandai.s_order_log to 
    2017-10-24T07:11:25.244+0800    writing rbtj.rb_visit_log to 
    2017-10-24T07:11:25.244+0800    writing gridfs.fs.chunks to 
    2017-10-24T07:11:25.251+0800    done dumping rbtj.rb_visit_log (92 documents)
    2017-10-24T07:11:25.251+0800    writing log.pushlog to 
    2017-10-24T07:11:25.251+0800    done dumping shandai.s_order_log (105 documents)
    2017-10-24T07:11:25.251+0800    writing test.lession to 
    2017-10-24T07:11:25.252+0800    done dumping test.lession (3 documents)
    2017-10-24T07:11:25.252+0800    writing twle.language to 
    2017-10-24T07:11:25.253+0800    done dumping log.pushlog (8 documents)
    2017-10-24T07:11:25.253+0800    writing twle.col to 
    2017-10-24T07:11:25.255+0800    done dumping nodebb.objects (327 documents)
    2017-10-24T07:11:25.255+0800    writing twle.products to 
    2017-10-24T07:11:25.255+0800    done dumping twle.language (2 documents)
    2017-10-24T07:11:25.255+0800    writing gridfs.fs.files to 
    2017-10-24T07:11:25.257+0800    done dumping twle.col (1 document)
    2017-10-24T07:11:25.257+0800    writing test.col to 
    2017-10-24T07:11:25.257+0800    done dumping twle.products (1 document)
    2017-10-24T07:11:25.257+0800    writing twle.counters to 
    2017-10-24T07:11:25.258+0800    done dumping gridfs.fs.files (1 document)
    2017-10-24T07:11:25.258+0800    writing twle.mycapped_log to 
    2017-10-24T07:11:25.259+0800    done dumping twle.counters (1 document)
    2017-10-24T07:11:25.259+0800    writing rrs_result.content to 
    2017-10-24T07:11:25.259+0800    done dumping test.col (1 document)
    2017-10-24T07:11:25.259+0800    writing nodebb.sessions to 
    2017-10-24T07:11:25.260+0800    done dumping twle.mycapped_log (0 documents)
    2017-10-24T07:11:25.260+0800    writing twle.lession to 
    2017-10-24T07:11:25.260+0800    done dumping rrs_result.content (0 documents)
    2017-10-24T07:11:25.261+0800    done dumping nodebb.sessions (0 documents)
    2017-10-24T07:11:25.261+0800    done dumping twle.lession (0 documents)
    2017-10-24T07:11:25.312+0800    done dumping gridfs.fs.chunks (19 documents)
    
关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.