- MongoDB Tutorials >
- Copy Databases Between Instances
Copy Databases Between Instances¶
On this page
Synopsis¶
MongoDB provides the copydb
and clone
database commands to support migrations of
entire logical databases between mongod
instances. With
these commands you can copy data between instances with a simple
interface without the need for an intermediate stage. The
db.cloneDatabase()
and db.copyDatabase()
provide
helpers for these operations in the mongo
shell.
Data migrations that require an intermediate stage or that involve
more than one database instance are beyond the scope of this
tutorial. copydb
and clone
are more ideal
for use cases that resemble the following use cases:
- data migrations,
- data warehousing, and
- seeding test environments.
Also consider the Backup Strategies for MongoDB Systems and Importing and Exporting MongoDB Data documentation for more related information.
Considerations¶
- You must run
copydb
orclone
on the destination server. - You cannot use
copydb
orclone
with databases that have a sharded collection in a sharded cluster, or any database via amongos
. - You can use
copydb
orclone
with databases that do not have sharded collections in a cluster when you’re connected directly to themongod
instance. - You can run
copydb
orclone
commands on a secondary member of a replica set, with properly configured read preference. - Each destination
mongod
instance must have enough free disk space on the destination server for the database you are copying. Use thedb.stats()
operation to check the size of the database on the sourcemongod
instance. For more information on the output ofdb.stats()
see Database Statistics Reference document.
Processes¶
Copy and Rename a Database¶
To copy a database from one MongoDB instance to another and rename
the database in the process, use the copydb
command, or
the db.copyDatabase()
helper in the mongo
shell.
Use the following procedure to copy the database named test
on
server db0.example.net
to the server named db1.example.net
and
rename it to records
in the process:
Rename a Database¶
You can also use copydb
or the
db.copyDatabase()
helper to:
- rename a database within a single MongoDB instance or
- create a duplicate database for testing purposes.
Use the following procedure to rename the test
database
records
on a single mongod
instance:
Copy a Database with Authentication¶
To copy a database from a source MongoDB instance that has
authentication enabled, you can specify authentication credentials to
the copydb
command or the db.copyDatabase()
helper in the mongo
shell.
In the following operation, you will copy the test
database from
the mongod
running on db0.example.net
to the
records
database on the local instance (e.g. db1.example.net
.)
Because the mongod
instance running on db0.example.net
requires authentication for all connections, you will need to pass
db.copyDatabase()
authentication credentials, as in the
following procedure:
Connect to the destination
mongod
instance running on thedb1.example.net
host using themongo
shell.Issue the following command:
Replace <username>
and <password>
with your authentication
credentials.
Clone a Database¶
The clone
command copies a database between
mongod
instances like copydb
; however,
clone
preserves the database name from the source
instance on the destination mongod
.
For many operations, clone
is functionally equivalent to
copydb
, but it has a more simple syntax and a more narrow
use. The mongo
shell provides the
db.cloneDatabase()
helper as a wrapper around
clone
.
You can use the following procedure to clone a database from the
mongod
instance running on db0.example.net
to the
mongod
running on db1.example.net
: