如何将数据库从一台 MongoDB 服务器复制到另一台服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5495137/
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 do I copy a database from one MongoDB server to another?
提问by Freewind
I have two mongodbs in different server, both start with --auth
. Now I want to copy a db from one server to another.
我在不同的服务器上有两个 mongodb,都以--auth
. 现在我想将数据库从一台服务器复制到另一台服务器。
> mongo
> use admin
> db.copyDatabase("mydb","mydb","another_server")
It shows:
表明:
{ "errmsg" : "", "ok" : 0 }
And:
和:
> db.getLastError()
null
Seems no error, but the copy is not successful. What's the correct command to use?
好像没有错误,但是复制不成功。要使用的正确命令是什么?
回答by Justin Jenkins
If you are using --auth, you'll need to include your username/password in there...
如果您使用--auth,则需要在其中包含您的用户名/密码...
Also you mustbe on the "destination" server when you run the command.
此外,当您运行命令时,您必须在“目标”服务器上。
db.copyDatabase(<from_db>, <to_db>, <from_hostname>, <username>, <password>);
If all that doesn't work, you might want to try something like creating a slave of the database you want to copy ...
如果所有这些都不起作用,您可能想尝试创建要复制的数据库的从属...
回答by Mike Shauneu
Starting from Mongo
version 3.2
you can do it by using mongodump/mongorestore
:
从Mongo
版本开始,3.2
您可以使用mongodump/mongorestore
:
mongodump --host <from_host> --db <from_db> --archive | mongorestore --host <to_host> --archive
Additional info could be found at:
可以在以下位置找到其他信息:
https://docs.mongodb.com/manual/reference/program/mongodump/https://docs.mongodb.com/manual/reference/program/mongorestore/
https://docs.mongodb.com/manual/reference/program/mongodump/ https://docs.mongodb.com/manual/reference/program/mongorestore/
To make remote mongo reachable you can create ssh tunnel to it:
要使远程 mongo 可访问,您可以创建到它的 ssh 隧道:
creates a tunnel to the remote mongodb server and tunnels it through port 27117 to the local client
创建到远程 mongodb 服务器的隧道并通过端口 27117 将其隧道到本地客户端
ssh -fN -L 27117:localhost:27017 <remote_host>
In this case the command to run on the local machine you want to restore to could be:
在这种情况下,在要恢复到的本地计算机上运行的命令可能是:
mongodump --port 27117 --db <from_db> --archive | mongorestore --archive
回答by marcelde
In addition to the answer of Justin Jenkins keep in mind that you also can use a ssh tunnel if you don't have mongodb exposed to the network (localhost only)
除了贾斯汀詹金斯的回答,请记住,如果您没有将 mongodb 暴露给网络(仅限本地主机),您也可以使用 ssh 隧道
I use screen to switch between "tasks". for my convenience the ssh tunnel and mongo are executed in separate screen tabs.
我使用屏幕在“任务”之间切换。为方便起见,ssh 隧道和 mongo 在单独的屏幕选项卡中执行。
step 1: create a tunnel
第一步:创建隧道
ssh username@yourdomainOrIP -L 27018:localhost:27017
...Enter your password
step 2 :
第2步 :
mongo
use admin
db.copyDatabase(<fromdb>,<todb>,"localhost:27018",<username>,<password)
回答by user1587520
In addition to the answer of Mike Shauneu, if your database name on the destination server is not the same as on the source server you need to rewrite the namespace. In combination with authentication I got this to work using the --uri
option, which requires a recent mongo version (>3.4.6):
除了 Mike Shauneu 的回答之外,如果目标服务器上的数据库名称与源服务器上的数据库名称不同,则需要重写命名空间。结合身份验证,我使用该--uri
选项使其工作,该选项需要最近的 mongo 版本(> 3.4.6):
mongodump --uri="mongodb://$sourceUser:$sourcePwd@$sourceHost/$sourceDb" --gzip --archive | mongorestore --uri="mongodb://$targetUser:$targetPwd@$targetHost/$targetDb" --nsFrom="$sourceDb.*" --nsTo="$targetDb.*" --gzip --archive