Home > Software engineering >  Unable to copy a database from one mongodb account to another
Unable to copy a database from one mongodb account to another

Time:01-23

I usually create a copy of a database in the same cluster in the same account like this:

  1. DUMP THE ORIGINAL DATABASE

mongodump --uri 'mongodb srv://ACC_USER_NAME:[email protected]/ORIGINAL_DATABASE_NAME' --archive="DUMP_FILE_NAME" --forceTableScan

  1. RESTORE THE DATABASE FROM THE DUMP FILE

mongorestore --uri 'mongodb srv://ACC_USER_NAME:[email protected]/ORIGINAL_DATABASE_NAME' --archive="DUMP_FILE_NAME" --nsFrom='ORIGINAL_DATABASE_NAME.' --nsTo='NEW_DATABASE_NAME.'

And that works perfectly.

Now, I created a new MongoDB atlas account, and I am trying to copy the original database into the new account.

So the first step is similar:

  1. DUMP THE ORIGINAL DATABASE

mongodump --uri 'mongodb srv://ORIGINAL_ACC_USER_NAME:[email protected]/ORIGINAL_DATABASE_NAME' --archive="DUMP_FILE_NAME" --forceTableScan

  1. RESTORE THE DATABASE FROM THE DUMP FILE

Here I could not find which params to use.
In the first example, in the second step, the URI is like this:

mongodb srv://ACC_USER_NAME:[email protected]/ORIGINAL_DATABASE_NAME

So I obviously should replace

ACC_USER_NAME:ACC_USER_PASSWORD

with

NEW_ACC_USER_NAME:NEW_ACC_USER_PASSWORD

But, what about the ORIGINAL_DATABASE_NAME, I could not find an equivalent to put there. The rest of the params:

--archive="DUMP_FILE_NAME" --nsFrom='ORIGINAL_DATABASE_NAME.' --nsTo='NEW_DATABASE_NAME.'

should remain the same.

CodePudding user response:

Step 1

First, you should do mongodump. You already did that correctly.

mongodump --uri mongodb srv://<USER>:<PASSWORD>@cluster.xxx.mongodb.net/<DATABASE>

This command will create a BSON file, which we will use for restoring the database in the next step.

Step 2

Now, you can use mongorestore with new cluster connection string, and with BSON file from previous step.

mongorestore --uri mongodb srv://<USER>:<PASSWORD>@new_cluster.xxx.mongodb.net <BSON_FILE_PATH>

NOTE: You don't need to specify ORIGINAL_DATABASE_NAME with mongorestore. You should only specify the base connection string of the new cluster. BSON file include all the necessary data for restoring the database. So, the database will be created in the new cluster and will have the same name as in the old cluster.

  •  Tags:  
  • Related