Quick Tip: Copy DBs Between Servers

in Administration, Quick Tip

Inspired by this Stackoverflow question I thought it would be worth while to post this quick tip …

If you ever need to copy (or “clone”) a MongoDB databse between two MongoDB servers its as easy as running the copyDatabase() command.

First, change into your admin db …

Then, run copyDatabase() supplying the needed information, the “from_db” (the database to copy) the “to_db” and the host name of the database you are copying …

> use admin;
> db.copyDatabase(…);

You’ll need to run the copyDatabase() command on the sever you want to “copy” the database to, aka the “destantion” server …

> db.copyDatabase(from_db, to_db, from_host);

If you are running MongoDB with accounts/password via –auth you’ll need to add that info on there too …

> db.copyDatabase(from_db, to_db, from_host, username, password);

A simple example for a database named “weyoun” on a host called “delta.quad.com” would be like so …

> db.copyDatabase('weyoun', 'weyoun', 'delta.quad.com');

There ya go, you should be good to go copying your MongoDB database!


  • Ann

    What happens to existing collection (with the same name) on destination db?

  • Jim

    Awesome thanks!

  • Kech

    Great article!

  • Eric

    Thanks, this saved me a lot of time and a headache.

  • Pingback: DBA_MYSQL_LINUX_Perl » MongoDB迁移小记