I have two mongodbs in different server, both start with --auth
. Now I want to copy a db from one server to another.
> 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?
If you are using --auth, you'll need to include your username/password in there...
Also you must be 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 ...
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)
I use screen to switch between "tasks". for my convenience the ssh tunnel and mongo are executed in separate screen tabs.
step 1: create a tunnel
ssh username@yourdomainOrIP -L 27018:localhost:27017
...Enter your password
step 2 :
mongo
use admin
db.copyDatabase(<fromdb>,<todb>,"localhost:27018",<username>,<password)
Starting from Mongo
version 3.2
you can do it by using 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/
To make remote mongo reachable you can create ssh tunnel to it:
ssh -fN -L 27017:localhost:27117 <remote_host>
In this case the command could be:
mongodump --port 27117 --db <from_db> --archive | mongorestore --archive