AICODER大前端培训

AICODER-IT培训专家,前端培训基地,前端培训,全栈培训,大学生IT培训基地,线上IT课程免费自学,线下培训项目实战,实习,html5培训实习-AICODE程序员

在线咨询

MongoDB数据备份和导入数据

老马 发布于: 2018年12月20日 10时16分22秒 阅读784次

MongoDB进行热备份

Mongodb自带了mongodump和mongorestore这两个工具来实现对数据的备份和恢复。

mongodump能够在Mongodb运行时进行备份,它的工作原理是对运行的Mongodb做查询,然后将所有查到的文档写入磁盘。但是存在的问题时使用mongodump产生的备份不一定是数据库的实时快照,如果我们在备份时对数据库进行了写入操作,则备份出来的文件可能不完全和Mongodb实时数据相等。

mongodump 进行热备份

mongodump工具使用说明:

$ mongodump --help
Export MongoDB data to BSON files.
 
options:
  --help                   produce help message
  -v [ --verbose ]         be more verbose (include multiple times for more 
                           verbosity e.g. -vvvvv)
  --version                print the program's version and exit
  -h [ --host ] arg        mongo host to connect to ( <set name>/s1,s2 for 
                           sets)
  --port arg               server port. Can also use --host hostname:port
  --ipv6                   enable IPv6 support (disabled by default)
  -u [ --username ] arg    username
  -p [ --password ] arg    password
  --dbpath arg             directly access mongod database files in the given 
                           path, instead of connecting to a mongod  server - 
                           needs to lock the data directory, so cannot be used 
                           if a mongod is currently accessing the same path
  --directoryperdb         if dbpath specified, each db is in a separate 
                           directory
  --journal                enable journaling
  -d [ --db ] arg          database to use
  -c [ --collection ] arg  collection to use (some commands)
  -o [ --out ] arg (=dump) output directory or "-" for stdout
  -q [ --query ] arg       json query
  --oplog                  Use oplog for point-in-time snapshotting
  --repair                 try to recover a crashed database
  --forceTableScan         force a table scan (do not use $snapshot)

参数说明:

  • -h:指明数据库宿主机的IP
  • -u:指明数据库的用户名
  • -p:指明数据库的密码
  • -d:指明数据库的名字
  • -c:指明collection的名字(不指定,则所有的集合)
  • -o:指明到要导出的文件名
  • -q:指明导出数据的过滤条件
  • -u:用户名
  • -p:密码

例如:

$ mongodump -h 127.0.0.1:27021 -u 用户名 -p 密码 -d 数据库名 -o 备份文件夹名或者路径

实例:

$ mongodump -h 127.0.0.1:27021 -u xx -p xx -d kk -o bak
2018-12-20T09:58:51.473+0800    writing kk.xxxx to 
2018-12-20T09:58:51.473+0800    writing kk.x2 to 
2018-12-20T09:58:51.473+0800    writing kk.x3 to 
2018-12-20T09:58:51.548+0800    done dumping kk.xxxx (1 document)
2018-12-20T09:58:51.550+0800    done dumping kk.x2 (29 documents)
2018-12-20T09:58:51.550+0800    done dumping kk.x3 (34 documents)

mongorestore 进行恢复数据

mongorestore 使用说明。

mongorestore --help
usage: ./bin/mongorestore [options] [directory or filename to restore from]
options:
  --help                  produce help message
  -v [ --verbose ]        be more verbose (include multiple times for more 
                          verbosity e.g. -vvvvv)
  --version               print the program's version and exit
  -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)
  --port arg              server port. Can also use --host hostname:port
  --ipv6                  enable IPv6 support (disabled by default)
  -u [ --username ] arg   username
  -p [ --password ] arg   password
  --dbpath arg            directly access mongod database files in the given 
                          path, instead of connecting to a mongod  server - 
                          needs to lock the data directory, so cannot be used 
                          if a mongod is currently accessing the same path
  --directoryperdb        if dbpath specified, each db is in a separate 
                          directory
  --journal               enable journaling
  -d [ --db ] arg         database to use
  -c [ --collection ] arg collection to use (some commands)
  --objcheck              validate object before inserting
  --filter arg            filter to apply before inserting
  --drop                  drop each collection before import
  --oplogReplay           replay oplog for point-in-time restore
  --keepIndexVersion      don't upgrade indexes to newest version

参数说明:

  • -h:指明数据库宿主机的IP
  • -u:指明数据库的用户名
  • -p:指明数据库的密码
  • -d:指明数据库的名字
  • -c:指明collection的名字
  • -o:指明到要备份的文件名
  • -q:指明备份数据的过滤条件
  • -u:用户名
  • -p:密码

例如:

mongodump -h 127.0.0.1:27021 -u xx -p xxx -d kk -o path/to/bakdir

# mongorestore -h 服务器ip:27021 -u 用户名 -p 密码 -d 数据库名 --drop  备份文件目录(要定位到数据名字对应的目录)

实例:

mongorestore -h 127.0.0.1:27021 -u admin -p aicoder_com -d kk --drop ./20181220.bak/kk
2018-12-20T09:49:13.501+0800    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2018-12-20T09:49:13.502+0800    building a list of collections to restore from 20181220.bak/kk dir
2018-12-20T09:49:13.522+0800    reading metadata for kk.ss from 20181220.bak/kk/ss.metadata.json
2018-12-20T09:49:13.543+0800    reading metadata for kk.users from 20181220.bak/kk/users.metadata.json
2018-12-20T09:49:13.641+0800    restoring kk.ss from 20181220.bak/kk/ss.bson
2018-12-20T09:49:13.643+0800    reading metadata for kk.busers from 20181220.bak/kk/busers.metadata.json
2018-12-20T09:49:13.713+0800    restoring kk.users from 20181220.bak/kk/users.bson
2018-12-20T09:49:13.778+0800    restoring kk.busers from 20181220.bak/kk/busers.bson
2018-12-20T09:49:13.782+0800    no indexes to restore
2018-12-20T09:49:13.782+0800    finished restoring kk.users (34 documents)
2018-12-20T09:49:13.788+0800    no indexes to restore
2018-12-20T09:49:13.788+0800    finished restoring kk.ss (29 documents)
2018-12-20T09:49:13.804+0800    no indexes to restore
2018-12-20T09:49:13.804+0800    finished restoring kk.busers (1 document)
2018-12-20T09:49:13.804+0800    done

参考链接

  1. Mongodb数据处理(备份mongodump、恢复mongorestore、导入mongoimport、导出mongoexport)
  2. Mongodb数据导出工具mongoexport和导入工具mongoimport介绍

AICODER编辑推荐:



报名就业班送基础班

最近分享