mongodb数据的导入导出,备份恢复

来源:互联网 发布:网络解析被劫持修复 编辑:程序博客网 时间:2024/05/29 10:34
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 输出备份文件的目录


例子
mongodump -h 192.168.1.103 -d citys -o /backup/

备份出来的数据是二进制的,已经经过压缩。比实际数据库要小很多


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先删掉。

例子:
mongorestore -h 127.0.0.1 --directoryperdb /back/


另外mongodb还提供了mongoexport和mongoimport这两个命令导出和导入数据库 导出文件给json格式,也可以实现备份和恢复的功能
mongoexport -d mydb -c mycoll -q '{"uid":"1"}' > mongo.bson
mongoimport -d mydb -c mycoll --file mongo.bson

0 0
原创粉丝点击