62

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?

Justin Jenkins
  • 26,590
  • 6
  • 68
  • 1,285
Freewind
  • 193,756
  • 157
  • 432
  • 708

5 Answers5

95

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 ...

Justin Jenkins
  • 26,590
  • 6
  • 68
  • 1,285
  • 9
    The username and password mentioned is for database not for host name PS: I made this mistake hence mentioning it :) – shammerw0w Apr 21 '14 at 18:36
  • Thank you! That was super simple. For my case I did not have a username or pass so I had to leave those parameters off. – Austin Lovell Aug 02 '16 at 18:06
  • 1
    can't i somehow copy the data base into mongo's datafolder ? – eran otzap Jan 24 '18 at 10:06
  • @eranotzap you can shutdown one database, copy the datafiles and transfer them to another server (which you also probably want to shutdown first) but in my opinion it's not only much easier much easier to use copyDatabase (no shutdown/file copy/transfer needed) ... but it's also a lot safer as it works more like replication to a slave. – Justin Jenkins Jan 25 '18 at 02:32
  • thanks. @JustinJenkins Sometimes the db is very large how do i know which files belong to which db ? – eran otzap Jan 25 '18 at 06:16
  • am getting below error **{ "ok" : 0, "errmsg" : "not master", "code" : 10107 }** . command used `db.copyDatabase('qa_db','qa_db','10.xx.xx.xxx')` . I am not using auth hence not using username/pwd options – Soubhik Banerjee May 24 '19 at 08:59
  • db.copyDatabase is deprecated for the version > 4.0. Does any know how to copy db from a server to server for the version > 4.0 ? – BSM Aug 04 '20 at 05:53
  • @BSM https://docs.mongodb.com/database-tools/mongodump/#mongodump-example-copy-clone-database – Justin Jenkins Aug 12 '20 at 23:22
  • 3
    Getting `WARNING: db.copyDatabase will only function with MongoDB 4.0 and below. See http://dochub.mongodb.org/core/4.2-copydb-clone` – Just Mohit Jun 25 '21 at 08:36
  • 1
    Starting in version 4.2, MongoDB removes the deprecated copydb command and clone command. – Oleksandr G Aug 25 '21 at 12:33
  • the documentation says: from version 4.2 use mongodump: https://www.mongodb.com/docs/database-tools/mongodump/#copy-and-clone-databases – Diego Lope Loyola Nov 14 '22 at 14:12
51

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:

creates a tunnel to the remote mongodb server and tunnels it through port 27117 to the local client

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
OLATOMIDE
  • 93
  • 1
  • 7
Mike Shauneu
  • 3,201
  • 19
  • 21
24

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)
marcelde
  • 271
  • 2
  • 7
  • 9
    Just to clarify these instructions a bit for those who have never created an SSH tunnel before: 27018 here is an arbitrary local port that is not used. To ensure it is not used, use "lsof -ti:9000". If processes are returned, use lsof to find an unused port. The second 27017 port in this answer is the port mongo is running on, on the remote host you are copying from. To ensure this is correct, sign in to the remote host and confirm mongo is running on this port with: "lsof -iTCP -sTCP:LISTEN | grep mongo". – huwiler Mar 02 '17 at 16:04
13

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):

mongodump --uri="mongodb://$sourceUser:$sourcePwd@$sourceHost/$sourceDb" --gzip --archive | mongorestore --uri="mongodb://$targetUser:$targetPwd@$targetHost/$targetDb" --nsFrom="$sourceDb.*" --nsTo="$targetDb.*" --gzip --archive
user1587520
  • 3,777
  • 1
  • 21
  • 20
  • This is currently the most flexible and accurate answer (see https://www.mongodb.com/docs/database-tools/mongorestore/#copy-clone-a-database), also because the `--db` option is now deprecated – Stefano Jul 31 '23 at 19:56
1

The following worked for me to copy from Ubuntu Server to another Ubuntu Server, both running MongoDB v4+:

  1. The following command should be executed from the server, where you want to restore the database:
   ssh -fN -L 27018:127.0.0.1:27017 <remote_host_ip>
  1. Now we have mapped our remote server database to our local server port 27018. Next, we can copy from remote server to our destination server using MongoDB's mongodump/mongorestore pipeline:
   mongodump --port 27018 --db <remote_db_name> --username <remote_db_username> --password <remote_db_password> --archive | mongorestore --username <destination_db_username> --password <destination_db_password> --archive
  1. [OPTIONAL] You can check your restored database as followings:
   mongo --authenticationDatabase "admin" -u <destination_db_username> -p
   show dbs

Check if your database exists in the list.

  • 1
    This is actually a clever trick, piping a dump to a restore. Tho it works with small dbs otherwise you need a pretty solid connection, any broken connection will make the restoration fail. – Ariel M. Jan 17 '21 at 05:25