[mongodb]数据库备份与还原

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介:
mongodb提供了两个命令来备份(mongodump )和恢复(mongorestore )数据库。 

1.备份(mongodump ) 

用法 : 
[root@web3 3]# mongodump --help 
options: 
--help                   produce help message 
-v [ --verbose ]         be more verbose (include multiple times for more 
verbosity e.g. -vvvvv) 
-h [ --host ] arg        mongo host to connect to ("left,right" for pairs) 
-d [ --db ] arg          database to use 
-c [ --collection ] arg  collection to use (some commands) 
-u [ --username ] arg    username 
-p [ --password ] arg    password 
--dbpath arg             directly access mongod data files in the given path, 
instead of connecting to a mongod instance - 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 
-o [ --out ] arg (=dump) output directory 

例子: 

[root@web3 ~]# mongodump -h 192.168.1.103 -d citys -o /backup/mongobak/3 
connected to: 192.168.1.103 
DATABASE: citys  to     /backup/mongobak/3/citys 
citys.building to /backup/mongobak/3/citys/building.bson 
13650 objects 
citys.system.indexes to /backup/mongobak/3/citys/system.indexes.bson 
1 objects 

备份出来的数据是二进制的,已经经过压缩。比实际数据库要小很多,我的数据库显示占用了260多M,备份后只有2M。 

2.恢复(mongorestore ) 

用法: 
[root@web3 3]# mongorestore --help 
usage: 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) 
-h [ --host ] arg       mongo host to connect to ("left,right" for pairs) 
-d [ --db ] arg         database to use 
-c [ --collection ] arg collection to use (some commands) 
-u [ --username ] arg   username 
-p [ --password ] arg   password 
--dbpath arg            directly access mongod data files in the given path, 
instead of connecting to a mongod instance - 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 
--drop                  drop each collection before import 
--objcheck              validate object before inserting 

--drop参数可以在导入之前把collection先删掉。 

例子: 

[root@web3 3]# mongorestore -h 127.0.0.1 --directoryperdb /backup/mongobak/3/         
connected to: 127.0.0.1 
/backup/mongobak/3/citys/building.bson 
going into namespace [citys.building] 
13667 objects 
/backup/mongobak/3/citys/system.indexes.bson 
going into namespace [citys.system.indexes] 
1 objects 

另外mongodb还提供了mongoexport 和 mongoimport 这两个命令来导出或导入数据,导出的数据是json格式的。也可以实现备份和恢复的功能。 

例: 

mongoexport -d mixi_top_city_prod -c building_45 -q '{ "uid" : "10832545" }' > mongo_10832545.bson 

mongoimport -d mixi_top_city -c building_45 --file mongo_10832545.bson
目录
相关文章
|
1月前
|
NoSQL 网络协议 MongoDB
Windows公网远程连接MongoDB数据库【无公网IP】
Windows公网远程连接MongoDB数据库【无公网IP】
|
1月前
|
存储 NoSQL 关系型数据库
一篇文章带你搞懂非关系型数据库MongoDB
一篇文章带你搞懂非关系型数据库MongoDB
55 0
|
1月前
|
人工智能 NoSQL MongoDB
|
2月前
|
SQL NoSQL Java
文档型数据库MongoDB
文档型数据库MongoDB
|
2月前
|
JSON NoSQL MongoDB
MongoDB详解(五)——MongoDB数据库简单使用
MongoDB详解(五)——MongoDB数据库简单使用
105 1
|
2月前
|
存储 NoSQL Linux
MongoDB详解(四)——MongoDB数据库安装
MongoDB详解(四)——MongoDB数据库安装
67 2
|
2月前
|
NoSQL 关系型数据库 MySQL
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
不同系统下进行MySQL安装、MongoDB安装、Redis安装【2月更文挑战第5天】
441 5
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
|
3月前
|
存储 JSON NoSQL
【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
【1月更文挑战第26天】【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
|
3月前
|
机器学习/深度学习 自然语言处理 NoSQL
|
3月前
|
存储 JSON NoSQL
在官网免费创建一个云mongoDB数据库
MongoDB的设计目标是提供高性能、高可用性、可扩展性和易用性。它采用了文档存储模型,将数据以类似JSON的BSON(Binary JSON)格式存储,并且支持动态模式,允许应用程序更灵活地存储和查询数据。MongoDB还支持水平扩展,可以在集群中添加更多的节点以处理更大的数据量和请求负载。
65 0