如何将转储恢复到正在运行的 mongodb
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18931668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to restore the dump into your running mongodb
提问by Sandeep Singh
I want to load data/restore dump data in mongoDB using mongorestore. I am trying to command
我想使用 mongorestore 在 mongoDB 中加载数据/恢复转储数据。我试图命令
mongorestore dump
but it giving me error
但它给了我错误
Sat Sep 21 16:12:33.403 JavaScript execution failed: SyntaxError: Unexpected identifier
How can we restore or put data into mongoDB?? Please give me the steps.
我们如何将数据恢复或放入 mongoDB??请给我步骤。
回答by Arman Ortega
mongodump:To dump all the records:
mongodump:转储所有记录:
mongodump -db databasename
To limit the amount of data included in the database dump, you can specify --db and --collection as options to mongodump. For example:
要限制数据库转储中包含的数据量,您可以指定 --db 和 --collection 作为 mongodump 的选项。例如:
mongodump --collection myCollection --db test
This operation creates a dump of the collection named myCollection from the database 'test' in a dump/ subdirectory of the current working directory. NOTE: mongodump overwrites output files if they exist in the backup data folder.
此操作从当前工作目录的 dump/ 子目录中的数据库“test”创建名为 myCollection 的集合的转储。注意: mongodump 会覆盖备份数据文件夹中存在的输出文件。
mongorestore:To restore all data
mongorestore:恢复所有数据
1) mongorestore --verbose \path\dump
or
或者
2) mongorestore --db databasename --verbose \path\dump\<dumpfolder>
Note: Both requires mongod instances.
注意:两者都需要 mongod 实例。
回答by Soham Krishna Paul
Dump DB by mongodump
通过mongodump转储数据库
mongodump --host <database-host> -d <database-name> --port <database-port> --out directory
Restore DB by mongorestore
通过mongorestore恢复数据库
With Index Restore
使用索引还原
mongorestore --host <database-host> -d <database-name> --port <database-port> foldername
Without Index Restore
无索引还原
mongorestore --noIndexRestore --host <database-host> -d <database-name> --port <database-port> foldername
Import Single Collection from CSV [1st Column will be treat as Col/Key Name]
从 CSV 导入单个集合 [第一列将被视为列/键名称]
mongoimport --db <database-name> --port <database-port> --collection <collection-name> --type csv --headerline --file /path/to/myfile.csv
Import Single Collection from JSON
从 JSON 导入单个集合
mongoimport --db <database-name> --port <database-port> --collection <collection-name> --file input.json
回答by Nazneen
The directory should be named 'dump' and this directory should have a directory which contains the .bson and .json files. This directory should be named as your db name.
该目录应命名为“转储”,并且该目录应有一个包含 .bson 和 .json 文件的目录。此目录应命名为您的数据库名称。
eg: if your db name is institution then the second directory name should be institution.
例如:如果您的数据库名称是机构,那么第二个目录名称应该是机构。
After this step, go the directory enclosing the dump folder in the terminal, and run the command
这一步之后,进入终端中转储文件夹所在的目录,并运行命令
mongorestore --drop.
mongorestore --drop。
Do see to it that mongo is up and running.
请务必确保 mongo 已启动并运行。
This should work fine.
这应该可以正常工作。
回答by SamanthPadala
- Start mongod
- Navigate to folder where you have extracted "enron.zip" in OS shell(cmd in case of windows)
- Then type ">mongorestore -d your_db_name dump/enron"
- 启动mongod
- 导航到您在 OS shell 中提取“enron.zip”的文件夹(在 Windows 的情况下为 cmd)
- 然后输入“> mongorestore -d your_db_name dump/enron”
回答by Tjs
You can take a dump to your local machine using this command:
您可以使用以下命令将转储到本地计算机:
mongodump -h <host>:<port> -u <username> -p <password> -d ubertower-new -o /path/to/destination/directory
You can restore from the local machine to your Mongo DB using this command
您可以使用此命令从本地机器恢复到您的 Mongo DB
mongorestore -h <host>:<port> -u <username> -p <password> -d <DBNAME> /path/to/destination/directory/<DBNAME>
回答by manoj tiwari
For mongoDB database restore use this command here
对于 mongoDB 数据库还原,请在此处使用此命令
mongorestore --db databasename --drop dump file path
回答by martinho
To restore a single database:
恢复单个数据库:
1. Backup the 'users' database
$ mongodump --db users
2. Restore the 'users' database to a new database called 'users2'
$ mongorestore --db users2 dump/users
回答by aman raj
Follow this path.
走这条路。
C:\Program Files\MongoDB\Server.2\bin
Run the cmd in bin folder and paste the below command
在 bin 文件夹中运行 cmd 并粘贴以下命令
mongorestore --db <name-your-database-want-to-restore-as> <path-of-dumped-database>
For Example:
例如:
mongorestore --db testDb D:\Documents\Dump\myDb
回答by Yi Xiang Chong
You can also restore your downloaded Atlas Backup .wt
WiredTiger files (which unzips or untar as a restore
folder) to your local MongoDB.
您还可以将下载的 Atlas Backup .wt
WiredTiger 文件(作为restore
文件夹解压缩或解压)恢复到本地 MongoDB。
First, make a backup of your /data/db
path. Call it /data_20200407/db
. Second, copy paste all the .wt
files from your Atlas Backup restore folder into your local /data/db
path. Restart your Ubuntu or MongoDB server. Start your Mongo shell and you should have those restored files there.
首先,备份您的/data/db
路径。叫它/data_20200407/db
。其次,将.wt
Atlas Backup 还原文件夹中的所有文件复制粘贴到本地/data/db
路径中。重新启动您的 Ubuntu 或 MongoDB 服务器。启动你的 Mongo shell,你应该在那里有那些恢复的文件。
回答by Muhammad Usman Ali Baloch
I have been through a lot of trouble so I came up with my own solution, I created this script, just set the path inside script and db name and run it, it will do the trick
我遇到了很多麻烦所以我想出了自己的解决方案,我创建了这个脚本,只需在脚本和数据库名称中设置路径并运行它,它就可以解决问题
#!/bin/bash
FILES= #absolute or relative path to dump directory
DB=`db` #db name
for file in $FILES
do
name=$(basename $file)
collection="${name%.*}"
echo `mongoimport --db "$DB" --file "$name" --collection "$collection"`
done