【发布时间】:2011-10-09 20:59:08
【问题描述】:
我已经使用mongodump 命令在服务器上导出了数据库,并且转储存储在.bson 文件中。我需要使用mongorestore 命令将其导入本地服务器。但是它不起作用。什么是正确的mongorestore 命令,另一个是什么tools to restore db?
【问题讨论】:
标签: mongodb
我已经使用mongodump 命令在服务器上导出了数据库,并且转储存储在.bson 文件中。我需要使用mongorestore 命令将其导入本地服务器。但是它不起作用。什么是正确的mongorestore 命令,另一个是什么tools to restore db?
【问题讨论】:
标签: mongodb
mongorestore 是用于导入由mongodump 转储的 bson 文件的工具。
来自docs:
mongorestore 从 mongodump 获取输出并恢复它。
例子:
# On the server run dump, it will create 2 files per collection
# in ./dump directory:
# ./dump/my-collection.bson
# ./dump/my-collection.metadata.json
mongodump -h 127.0.0.1 -d my-db -c my-collection
# Locally, copy this structure and run restore.
# All collections from ./dump directory are picked up.
scp user@server:~/dump/**/* ./
mongorestore -h 127.0.0.1 -d my-db
【讨论】:
dump/dbName/collectionName.bson 文件夹结构吗?我使用了mongodump,但现在我想在远程Linux 机器上使用mongorestore 导入它。
mongorestore --collection people --db accounts myDump/accounts/people.bson
bsondump collection.bson > collection.json
然后
mongoimport -d <dbname> -c <collection> < collection.json
【讨论】:
mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.
导入.bson文件非常简单:
mongorestore -d db_name -c collection_name /path/file.bson
仅适用于单个集合。试试这个:
mongorestore --drop -d db_name -c collection_name /path/file.bson
用于恢复mongodump导出的完整文件夹:
mongorestore -d db_name /path/
注意:如果您启用了身份验证,请使用以下语法:
mongorestore -u username --authenticationDatabase admin -d db_name -c collection_name /path/file.bson
【讨论】:
mongoexport 导出的 bson 文件。 mongoimport 给了我“无效字符”错误。谢谢!
Mongorestore -h [host] -u [user] -p [pass] -d [database] -c [collection] [bson file]
你必须通过 cmd 运行这个 mongorestore 命令,而不是在 Mongo Shell 上...看看下面的命令...
在 cmd 上运行此命令(而不是在 Mongo shell 上)
>path\to\mongorestore.exe -d dbname -c collection_name path\to\same\collection.bson
这里path\to\mongorestore.exe是mongorestore.exe在mongodb的bin文件夹中的路径。 dbname 是数据库的名称。 collection_name 是 collection.bson 的名称。 path\to\same\collection.bson 是该集合的路径。
现在从 mongo shell 你可以验证数据库是否被创建(如果它不存在,同名的数据库将与集合一起创建)。
【讨论】:
我用过这个:
mongorestore -d databasename -c file.bson fullpath/file.bson
1.从属性中复制文件路径和文件名(尝试将所有bson文件放在不同的文件夹中), 2.一次又一次地使用这个,只改变文件名。
【讨论】:
从命令行运行以下命令,你应该在 Mongo bin 目录中。
mongorestore -d db_name -c collection_name path/file.bson
【讨论】:
如果您可以远程访问,您可以做到
对于 bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people "/home/${USER}/people.bson"
对于以 .gz (gzip) 格式压缩的 bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people --gzip --dir "/home/${USER}/people.bson.gz"
【讨论】:
如果有人还在为 mongorestore 苦苦挣扎,仅供参考。
您必须在终端/命令提示符下运行 monogorestore,而不是在 mongo 控制台中。
$ mongorestore -d db_name /path_to_mongo_dump/
更多细节可以访问官方文档
https://docs.mongodb.com/manual/reference/program/mongorestore/
【讨论】:
mongorestore -d db_name /path/
确保在 mongoDb 的 bin 文件夹中运行此查询
C:\Program Files\MongoDB\Server\4.2\bin -
然后运行上面的命令。
【讨论】:
在 mongodb 3.0 或以上版本中,我们可以指定要恢复的数据库名称。假设你站在包含 bson 文件的根目录
./
a.bson
b.metadata.bson
...
脚本是
for FILENAME in *; do mongorestore -d <db_name> -c "${FILENAME%.*}" $FILENAME; done
【讨论】: