database 将 MongoDB 数据库复制到本地机器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20901898/
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
Copying MongoDB Database into Local Machine
提问by Razor
I have a MongoDB database that resides on a remote server machine whose IP address is 192.168.1.20 on a local network. For development and testing purposes, and since I am not allowed to modify or delete the database on the server for security purposes, I want to copy the database on my local machine for my personal use.
我有一个 MongoDB 数据库,它驻留在本地网络上的 IP 地址为 192.168.1.20 的远程服务器计算机上。出于开发和测试目的,并且出于安全目的,我不允许修改或删除服务器上的数据库,因此我想将数据库复制到本地计算机上以供个人使用。
Can anyone please tell me, how do I achieve this?
谁能告诉我,我如何实现这一目标?
采纳答案by sas
There is copy databasecommand which I guess should be good fit for your need.
有复制数据库命令,我想应该很适合您的需要。
db.copyDatabase("DATABASENAME", "DATABASENAME", "localhost:27018");
Alternatively, you can just stop MongoDb, copy the database files to another server and run an instance of MongoDb there.
或者,您可以停止 MongoDb,将数据库文件复制到另一台服务器并在那里运行 MongoDb 实例。
EDIT 2020-04-25
编辑 2020-04-25
Quote from MongoDB documentation
来自 MongoDB 文档的引用
MongoDB 4.0 deprecates the
copydband theclone commands and their mongo shell helpersdb.copyDatabase()anddb.cloneDatabase().As alternatives, users can use
mongodumpandmongorestore(with themongorestoreoptions--nsFromand--nsTo) or write a script using the drivers.
MongoDB 4.0 弃用了
copydb和clone 命令以及它们的 mongo shell 助手db.copyDatabase()和db.cloneDatabase().作为替代方案,用户可以使用
mongodump和mongorestore(带有mongorestore选项--nsFrom和--nsTo)或使用驱动程序编写脚本。
Reference here
参考这里
回答by malla
I do this by creating a dump of the remote db to my local machine, which I then restore:
为此,我将远程数据库的转储创建到本地计算机,然后将其还原:
Make sure you have a mongo instance up and running(eg. run
mongod.exefrom your bin folder in a terminal window. On my windows computer that's C:\mongodb\bin)Make a dump from remote db: Open a new terminal window, move to the bin folder again, run:
mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass(Change the parameters to suit your own situation.)
Restore the dumped database:Once the dump has been made, run the following command so that you have a local db:
mongorestore -d theNameYouWantForYourLocalDB dump\nameOfRemoteDB(replace nameOfRemoteDB with the name of the remote db, the same as in previous command, and replace theNameYouWantForYourLocalDB with the name that you want your new local db to have)
确保您有一个 mongo 实例启动并正在运行(例如,
mongod.exe从终端窗口中的 bin 文件夹运行。在我的 Windows 计算机上是 C:\mongodb\bin)从远程数据库转储:打开一个新的终端窗口,再次移动到 bin 文件夹,运行:
mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass(更改参数以适合您自己的情况。)
恢复转储的数据库:完成转储后,运行以下命令,以便您拥有本地数据库:
mongorestore -d theNameYouWantForYourLocalDB dump\nameOfRemoteDB(将 nameOfRemoteDB 替换为远程数据库的名称,与之前的命令相同,并将 NameYouWantForYourLocalDB 替换为您希望新本地数据库具有的名称)
回答by Carol.Z
This should be a comment to the answer of @malla, but I don't have enough reputation to comment so I'm posting it here for other's reference.
这应该是对@malla 答案的评论,但我没有足够的声誉来发表评论,所以我将其发布在这里以供其他人参考。
In step 2, When you are trying to dump file from a remote server, remember to add out option so that you can restore locally later: (in my first try, I didn't add it and it failed, saying dump\db_name was not found).I'm not sure whether my way efficient or not. But it worked for me.
在第 2 步中,当您尝试从远程服务器转储文件时,请记住添加 out 选项,以便您稍后可以在本地还原:(在我的第一次尝试中,我没有添加它并且它失败了,说 dump\db_name 是未找到)。我不确定我的方式是否有效。但它对我有用。
Step 2:
第2步:
mongodump -h example.host.com --port 21018 -d dbname --username username --password yourpass --out <path_you_want_to_dump>
Step 3:
第 3 步:
mongorestore -d theNameYouWantForYourLocalDB \<path_you_want_to_dump> + nameOfRemoteDB
回答by 10 cls
The mongoexport command: http://docs.mongodb.org/manual/core/import-export/
mongoexport 命令:http: //docs.mongodb.org/manual/core/import-export/
Or, mongodump command: http://docs.mongodb.org/manual/reference/program/mongodump/
或者,mongodump 命令:http://docs.mongodb.org/manual/reference/program/mongodump/
回答by Dominik Goltermann
mongodb has commandline tools for importing and exporting. Take a look at mongodump --collection collection --db testand mongorestore --collection people --db accounts dump/accounts/
mongodb 具有用于导入和导出的命令行工具。看看mongodump --collection collection --db test和mongorestore --collection people --db accounts dump/accounts/
http://docs.mongodb.org/v2.2/reference/mongodump/http://docs.mongodb.org/v2.2/reference/mongorestore/
http://docs.mongodb.org/v2.2/reference/mongodump/ http://docs.mongodb.org/v2.2/reference/mongorestore/
this even works over the network
这甚至可以通过网络工作
回答by Munim
You can use the mongoexportcommand to copy the database to your local machine.
您可以使用mongoexport命令将数据库复制到本地计算机。

