mongodb导出导入
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,数据库都分为冷备份与热备份,所谓的冷备份就是停库,然后把数据拷贝;热备份就是使用相应工具,在数据库运行时做备份。每种库都会有自己的热备份工具,mongodb也不例外。mongodb的备份主要有热备和冷
千家信息网最后更新 2025年11月07日mongodb导出导入
数据库都分为冷备份与热备份,所谓的冷备份就是停库,然后把数据拷贝;热备份就是使用相应工具,在数据库运行时做备份。每种库都会有自己的热备份工具,mongodb也不例外。
mongodb的备份主要有热备和冷备。
一,冷备:
停止mongodb服务,直接copy数据目录;启动mongodb时,可以用--dbpath指定自己设置的数据库存储目录。
二,热备:
用mongodump来做MongoDB的库或表级别的热备份。
认识一下mongodump,
[mongo@tstdb-25-220 mongodb]$ bin/mongodump --helpUsage: mongodumpExport 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 exitverbosity options: -v, --verbose= 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 outputconnection options: -h, --host= mongodb host to connect to (setname/host1,host2 for replica sets) --port= server port (can also use --host hostname:port)authentication options: -u, --username= username for authentication -p, --password= password for authentication --authenticationDatabase= database that holds the user's credentials --authenticationMechanism= authentication mechanism to usenamespace options: -d, --db= database to use -c, --collection= collection to usequery 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= | specify either a preference name or a preference json object --forceTableScan force a table scanoutput options: -o, --out= 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= 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 to exclude from the dump (may be specified multiple times to exclude additional collections) --excludeCollectionsWithPrefix= 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/backup2016-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总用量 4drwxr-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 --helpUsage: mongorestoreRestore 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 exitverbosity options: -v, --verbose= 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 outputconnection options: -h, --host= mongodb host to connect to (setname/host1,host2 for replica sets) --port= server port (can also use --host hostname:port)authentication options: -u, --username= username for authentication -p, --password= password for authentication --authenticationDatabase= database that holds the user's credentials --authenticationMechanism= authentication mechanism to usenamespace options: -d, --db= database to use -c, --collection= collection to useinput options: --objcheck validate all objects before inserting --oplogReplay replay oplog for point-in-time restore --oplogLimit= [:ordinal] only include oplog entries before the provided Timestamp --archive= 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= input directory, use '-' for stdin --gzip decompress gzipped inputrestore options: --drop drop each collection before import --writeConcern= 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/sykdb2016-10-31T10:28:21.049+0800 building a list of collections to restore from backup/sykdb dir2016-10-31T10:28:21.051+0800 reading metadata for sykdb.sykdb from backup/sykdb/sykdb.metadata.json2016-10-31T10:28:21.051+0800 reading metadata for sykdb.table_syk from backup/sykdb/table_syk.metadata.json2016-10-31T10:28:21.051+0800 reading metadata for sykdb.system.profile from backup/sykdb/system.profile.metadata.json2016-10-31T10:28:21.051+0800 restoring sykdb.sykdb from backup/sykdb/sykdb.bson2016-10-31T10:28:21.051+0800 no indexes to restore2016-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.bson2016-10-31T10:28:21.086+0800 restoring indexes for collection sykdb.sykdb from metadata2016-10-31T10:28:21.086+0800 restoring indexes for collection sykdb.table_syk from metadata2016-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.dat2016-10-31T11:43:40.893+0800 connected to: 172.16.25.2202016-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.dat2016-10-31T11:45:22.499+0800 connected to: 172.16.25.2202016-10-31T11:45:22.540+0800 imported 1 document
数据回来了。
备份
数据
e.g.
数据库
工具
就是
文件
用量
目录
一行
参数
命令
备份工具
拷贝
时间
级别
过程
逻辑
存储
服务
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
校园网络安全防范方案
linux服务器防止cdn攻击
计算机网络技术需要什么学科好
网络安全执法专业发展史
软件开发集成电路股
淄博手机软件开发哪家便宜
花粉蛋白数据库
密码学与网络安全研讨
玩mc服务器
ps数据库编程
服务器怎么找
数据库编辑gis
成年人学习网络安全教育
数据库实现登录注册
还原数据库提示操作系统错误
学习通网络安全与形势答案
服务器集群中交换机的搭建
怎么找到我的世界服务器地址
对软件开发行业的理解
根据网络安全法国家采取措施
我的世界网易版困难的服务器
爱酷neo3数据库
知网里面科技成果数据库怎么下载
海淀区专业网络技术常见问题
网络安全界面无法切换输入法
北京工业软件开发如何收费
网络安全法治政府建设
小白智能摄像头连接服务器失败
周口互联网在线科技有限公司
数据库sga