广告
返回顶部
首页 > 资讯 > 数据库 >mongodb导出导入
  • 501
分享到

mongodb导出导入

2024-04-02 19:04:59 501人浏览 八月长安
摘要

数据库都分为冷备份与热备份,所谓的冷备份就是停库,然后把数据拷贝;热备份就是使用相应工具,在数据库运行时做备份。每种库都会有自己的热备份工具,mongoDB也不例外。monGodb的备份主要有热备和冷备。一

数据库都分为冷备份与热备份,所谓的冷备份就是停库,然后把数据拷贝;热备份就是使用相应工具,在数据库运行时做备份。每种库都会有自己的热备份工具,mongoDB也不例外。

monGodb的备份主要有热备和冷备。

一,冷备:

停止mongodb服务,直接copy数据目录;启动mongodb时,可以用--dbpath指定自己设置的数据库存储目录。

 

二,热备:

用mongodump来做MongoDB的库或表级别的热备份。


认识一下mongodump,

[mongo@tstdb-25-220 mongodb]$ bin/mongodump --help
Usage:
  mongodump <options>

Export the content of a running server into .bson files.

Specify a database with -d and a collection with -c to only dump that database or collection.

See Http://docs.mongodb.org/manual/reference/program/mongodump/ for more infORMation.

general options:
      --help                                                print usage
      --version                                             print the tool version and exit

verbosity options:
  -v, --verbose=<level>                                     more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric
                                                            value, e.g. --verbose=N)
      --quiet                                               hide all log output

connection options:
  -h, --host=<hostname>                                     mongodb host to connect to (setname/host1,host2 for replica sets)
      --port=<port>                                         server port (can also use --host hostname:port)

authentication options:
  -u, --username=<username>                                 username for authentication
  -p, --passWord=<password>                                 password for authentication
      --authenticationDatabase=<database-name>              database that holds the user's credentials
      --authenticationMechanism=<mechanism>                 authentication mechanism to use

namespace options:
  -d, --db=<database-name>                                  database to use
  -c, --collection=<collection-name>                        collection to use

query options:
  -q, --query=                                              query filter, as a JSON string, e.g., '{x:{$gt:1}}'
      --queryFile=                                          path to a file containing a query filter (jsON)
      --readPreference=<string>|<json>                      specify either a preference name or a preference json object
      --forceTableScan                                      force a table scan

output options:
  -o, --out=<directory-path>                                output directory, or '-' for stdout (defaults to 'dump')
      --gzip                                                compress arcHive our collection output with Gzip
      --repair                                              try to recover documents from damaged data files (not supported by all storage engines)
      --oplog                                               use oplog for taking a point-in-time snapshot
      --archive=<file-path>                                 dump as an archive to the specified path. If flag is specified without a value, archive is written to
                                                            stdout
      --dumpDbUsersAndRoles                                 dump user and role definitions for the specified database
      --excludeCollection=<collection-name>                 collection to exclude from the dump (may be specified multiple times to exclude additional collections)
      --excludeCollectionsWithPrefix=<collection-prefix>    exclude all collections from the dump that have the given prefix (may be specified multiple times to
                                                            exclude additional prefixes)
  -j, --numParallelCollections=                             number of collections to dump in parallel (4 by default)

看下导出过程

[mongo@tstdb-25-220 mongodb]$ bin/mongodump -h 172.16.25.220 -d sykdb -o /usr/local/mongodb/backup
2016-10-31T09:41:03.521+0800    writing sykdb.system.indexes to 
2016-10-31T09:41:03.522+0800    done dumping sykdb.system.indexes (2 documents)
2016-10-31T09:41:03.523+0800    writing sykdb.system.profile to 
2016-10-31T09:41:03.523+0800    writing sykdb.sykdb to 
2016-10-31T09:41:03.523+0800    writing sykdb.table_syk to 
2016-10-31T09:41:03.524+0800    done dumping sykdb.system.profile (2 documents)
2016-10-31T09:41:03.524+0800    done dumping sykdb.sykdb (2 documents)
2016-10-31T09:41:03.524+0800    done dumping sykdb.table_syk (1 document)

导出后的文件如下:

[mongo@tstdb-25-220 mongodb]$ cd backup/
[mongo@tstdb-25-220 backup]$ ll
总用量 4
drwxr-xr-x. 2 mongo dbmon 4096 10月 31 09:41 sykdb
[mongo@tstdb-25-220 backup]$ cd sykdb/
[mongo@tstdb-25-220 sykdb]$ ll
总用量 28
-rw-r--r--. 1 mongo dbmon   75 10月 31 09:41 sykdb.bson
-rw-r--r--. 1 mongo dbmon   83 10月 31 09:41 sykdb.metadata.json
-rw-r--r--. 1 mongo dbmon  136 10月 31 09:41 system.indexes.bson
-rw-r--r--. 1 mongo dbmon 1529 10月 31 09:41 system.profile.bson
-rw-r--r--. 1 mongo dbmon   55 10月 31 09:41 system.profile.metadata.json
-rw-r--r--. 1 mongo dbmon   36 10月 31 09:41 table_syk.bson
-rw-r--r--. 1 mongo dbmon   87 10月 31 09:41 table_syk.metadata.json

下面看一下导入(导出与导入可以看做是备份与恢复,所有的备份都是为了恢复而做)

[mongo@tstdb-25-220 mongodb]$ bin/mongorestore --help
Usage:
  mongorestore <options> <directory or file to restore>

Restore backups generated with mongodump to a running server.

Specify a database with -d to restore a single database from the target directory,
or use -d and -c to restore a single collection from a single .bson file.

See http://docs.mongodb.org/manual/reference/program/mongorestore/ for more information.

general options:
      --help                                      print usage
      --version                                   print the tool version and exit

verbosity options:
  -v, --verbose=<level>                           more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g.
                                                  --verbose=N)
      --quiet                                     hide all log output

connection options:
  -h, --host=<hostname>                           mongodb host to connect to (setname/host1,host2 for replica sets)
      --port=<port>                               server port (can also use --host hostname:port)

authentication options:
  -u, --username=<username>                       username for authentication
  -p, --password=<password>                       password for authentication
      --authenticationDatabase=<database-name>    database that holds the user's credentials
      --authenticationMechanism=<mechanism>       authentication mechanism to use

namespace options:
  -d, --db=<database-name>                        database to use
  -c, --collection=<collection-name>              collection to use

input options:
      --objcheck                                  validate all objects before inserting
      --oplogReplay                               replay oplog for point-in-time restore
      --oplogLimit=<seconds>[:ordinal]            only include oplog entries before the provided Timestamp
      --archive=<filename>                        restore dump from the specified archive file.  If flag is specified without a value, archive is read from stdin
      --restoreDbUsersAndRoles                    restore user and role definitions for the given database
      --dir=<directory-name>                      input directory, use '-' for stdin
      --gzip                                      decompress gzipped input

restore options:
      --drop                                      drop each collection before import
      --writeConcern=<write-concern>              write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}'
                                                  (defaults to 'majority')
      --noIndexRestore                            don't restore indexes
      --noOptionsRestore                          don't restore collection options
      --keepIndexVersion                          don't update index version
      --maintainInsertionOrder                    preserve order of documents during restoration
  -j, --numParallelCollections=                   number of collections to restore in parallel (4 by default)
      --numInsertionWorkersPerCollection=         number of insert operations to run concurrently per collection (1 by default)
      --stopOnError                               stop restoring if an error is encountered on insert (off by default)
      --bypassDocumentValidation                  bypass document validation

--oplogReplay这个参数,大家注意一下,mongodb的导出导入可以做的基本时间点的恢复

我们再insert一行,然后测试一下:

moe:PRIMARY> db.sykdb.insert ({"job":"dba"})

然后导入:

[mongo@tstdb-25-220 mongodb]$ bin/mongorestore -h 172.16.25.220 -d sykdb backup/sykdb
2016-10-31T10:28:21.049+0800    building a list of collections to restore from backup/sykdb dir
2016-10-31T10:28:21.051+0800    reading metadata for sykdb.sykdb from backup/sykdb/sykdb.metadata.json
2016-10-31T10:28:21.051+0800    reading metadata for sykdb.table_syk from backup/sykdb/table_syk.metadata.json
2016-10-31T10:28:21.051+0800    reading metadata for sykdb.system.profile from backup/sykdb/system.profile.metadata.json
2016-10-31T10:28:21.051+0800    restoring sykdb.sykdb from backup/sykdb/sykdb.bson
2016-10-31T10:28:21.051+0800    no indexes to restore
2016-10-31T10:28:21.052+0800    finished restoring sykdb.system.profile (0 documents)
2016-10-31T10:28:21.052+0800    restoring sykdb.table_syk from backup/sykdb/table_syk.bson
2016-10-31T10:28:21.086+0800    restoring indexes for collection sykdb.sykdb from metadata
2016-10-31T10:28:21.086+0800    restoring indexes for collection sykdb.table_syk from metadata
2016-10-31T10:28:21.086+0800    finished restoring sykdb.sykdb (2 documents)
2016-10-31T10:28:21.086+0800    finished restoring sykdb.table_syk (1 document)
2016-10-31T10:28:21.087+0800    done

查询一下,看数据是否回来了

moe:PRIMARY> db.sykdb.find().limit(100);
{ "_id" : ObjectId("5816ac26d37a70860b091c1f"), "job" : "dba" }
{ "_id" : ObjectId("5813041a56c55d778812e689"), "name" : "syk" }
{ "_id" : ObjectId("5813065356c55d778812e68a"), "loc" : "beijing" }

介绍一对命令:mongoexport/mongoimport

[mongo@tstdb-25-220 mongodb]$ bin/mongoexport -h 172.16.25.220 -d sykdb -c table_syk -o table_syk.dat
2016-10-31T11:43:40.893+0800    connected to: 172.16.25.220
2016-10-31T11:43:40.894+0800    exported 1 record

查看导出来的文件:

[mongo@tstdb-25-220 mongodb]$ strings table_syk.dat 
{"_id":{"$oid":"581313a856c55d778812e68b"},"name":"syk"}
[mongo@tstdb-25-220 mongodb]$ file table_syk.dat 
table_syk.dat: ASCII text

看来这个工具相当于其他数据库中的逻辑导出

把表清空

moe:PRIMARY> db.table_syk.drop()

导入

[mongo@tstdb-25-220 mongodb]$ bin/mongoimport -h 172.16.25.220 -d sykdb -c table_syk table_syk.dat
2016-10-31T11:45:22.499+0800    connected to: 172.16.25.220
2016-10-31T11:45:22.540+0800    imported 1 document

数据回来了。

您可能感兴趣的文档:

--结束END--

本文标题: mongodb导出导入

本文链接: https://www.lsjlt.com/news/35461.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • mongodb导出导入
    数据库都分为冷备份与热备份,所谓的冷备份就是停库,然后把数据拷贝;热备份就是使用相应工具,在数据库运行时做备份。每种库都会有自己的热备份工具,mongodb也不例外。mongodb的备份主要有热备和冷备。一...
    99+
    2022-10-18
  • mongodb导出与导入
    Mongodb导出与导入 1: 导入/导出可以操作的是本地的mongodb服务器,也可以是远程的.所以,都有如下通用选项:-h host 主机--port port 端口-u username 用...
    99+
    2022-10-18
  • MongoDB数据导入导出(8)
    导出数据 导入/导出可以操作的是本地的mongodb服务器,也可以是远程的,所以,都有如下通用选项。 利用mongoexport -h host 主机 -port 端口 -d 知名使用的库 -c 指明要导出的集合 -o 指明要导出...
    99+
    2014-12-22
    MongoDB数据导入导出(8)
  • mongodb如何导入或导出数据库
    MongoDB提供了多种方法来导入或导出数据库。 使用mongodump和mongorestore命令行工具: mongodu...
    99+
    2023-10-27
    mongodb 数据库
  • MySQL和MongoDB的导入和导出方法
    这篇文章主要讲解了“MySQL和MongoDB的导入和导出方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MySQL和MongoDB的导入和导出方法”吧!...
    99+
    2022-10-18
  • Mongodb 数据导入导出,备份及恢复
    搭建测试环境时,为了保证线上数据不被破坏,或避免引起误操作,数据库要建立本机数据库。将线上数据做成备份,然后恢复到本地测试环境下。主要是mongoexport, mongoimport, mongodump...
    99+
    2022-10-18
  • tp6 thinkphp6 excel导入导出 PHPExcel导入导出excel
    tp6 thinkphp6 Excel导入导出,主要是使用了PHPExcel类库。 php导出excel数字太长尾数变000解决方法 导出时为什么数字字段要加“\t”。是因为,由于数字超过15位,会被显示成0或者加小数点处理。造成这种情况是...
    99+
    2023-08-31
    php 数据库 服务器
  • 教你使用MongoDB导入导出备份数据
    目录导出数据mongodumpmongoexport恢复mongorestoremongoimport需要提前安装MongoDB-database-tools参考:Centos离线安装mongodb-database-t...
    99+
    2022-11-13
  • 搭建 MongoDB 服务器,MongoDB 基本使用,数据导入导出
    MongoDB 1.1软件介绍 • 介于关系数据库和非关系数据库之间的产品 – 一个基于分布式文件存储的数据库。 – 由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。 – M...
    99+
    2022-10-18
  • oracle11g导入导出
    导出: exp sss/123456@myorcl owner=sss file=d:\sss.dmp buffer=67108864 statistics=none consistent=y 导入: im...
    99+
    2022-10-18
  • mongodb数据表导入导出的方法是什么
    MongoDB提供了命令行工具mongodump和mongorestore来进行数据表的导入和导出。具体方法如下:1. 导出数据表:...
    99+
    2023-09-12
    mongodb
  • VMware - 导入/导出OVF
    OVF文件 开放虚拟机格式文件 ( Open Virtualization Format , OVF ),是一种针对 虚拟设备 (或者可以允许 软件 运行的 虚拟机 )打包和发布的 开放标准 。. 这...
    99+
    2023-10-20
    java linux ubuntu
  • expdp/impdp导出导入
    创建导出文件存放位置文件夹create directory dir_dump as '/home/oracle';给用户授权grant read, write on directory di...
    99+
    2022-10-18
  • 数据导入导出
    数据导入导出设置数据导入/导出使用的目录1.查看默认使用目录及目录是否存在。mysql>show variables like "secure_file_priv";secure_...
    99+
    2022-10-18
  • MongoDB笔记十六——Mongodb导出
    打开cmd利用mongoexport命令来导出数据参数说明          -d 要导出的数据库名称...
    99+
    2022-10-18
  • 批量备份还原导入与导出MongoDB数据方式
    前文链接传送门 mongo数据库的安装与配置 Navicat 建立数据库连接 新建数据库 导入JavaScript文件入数据库 mongodb数据备份和还原主要分为二种,一...
    99+
    2022-11-12
  • mysql dump 导入和导出
    mysql版本 5.7.30 基于安全无法通过-p密码明文方式进行执行,先修改配置 vi /etc/mysql/my.cnf [mysqldump] user=your_backup_user_name password=your...
    99+
    2021-10-16
    mysql dump 导入和导出 数据库入门 数据库基础教程
  • Oracle导入导出命令
    EXP和IMP的用法exp 用户名/密码@数据库连接地址 file=导出文件路径imp 用户名/密码@数据库连接地址 file=导入文件路径 ignore=y例如:imp scott/abcd@127.0....
    99+
    2022-10-18
  • oracle 数据导入导出
    I am the new of Oracle , and that i need to learn more about it . But as you know , here is the company...
    99+
    2022-10-18
  • 数据泵导出导入
    出于许多原因,我们期望从某个数据库中提取大量数据以及关联的对象定义,并且采用一种更容易的方式将这些数据载入到另一个数据库中。备份就是其中一个重要原因,另外可能需要在生产与测试环境之间或联机系统与数据仓库之...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作