How do I copy a database from one MongoDB server to another?

前端 未结 5 1698
逝去的感伤
逝去的感伤 2020-11-29 21:01

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
&         


        
相关标签:
5条回答
  • 2020-11-29 21:23

    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.

    0 讨论(0)
  • 2020-11-29 21:31

    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)
    
    0 讨论(0)
  • 2020-11-29 21:34

    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
    
    0 讨论(0)
  • 2020-11-29 21:37

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

    0 讨论(0)
  • 2020-11-29 21:42

    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
    
    0 讨论(0)
提交回复
热议问题