I need to copy my Mongo database along with its data. I have tried
db.copyDatabase( "Old_db", "new_db", "localhost" )
But the problem is it only copies a blank db, not with the previous data.
Feb 2019
since db.copyDatabase()
was deprecated in v4.0, you should use mongodump
and mongorestore
instead:
mongodump
--host <source host:port>
--ssl
--username <username>
--password <password>
--authenticationDatabase admin
--db <sourceDbName>
--collection <collection-name>
mongodump
command will export the whole database into a local folder named dump/<sourceDbName>
by default, then use mongorestore
command to import to your target database:
mongorestore
--host <target host:port>
--ssl
--username <username>
--password <password>
--authenticationDatabase admin
--db <targetDbName>
--collection <collection-name>
<dump folder/file>
Examples:
# backup the whole db (mydb-old):
mongodump -h Cluster0-shard-0/sample-shard-00-00-xyz.mongodb.net:27017 \
--ssl -u user1 -p 123123 --authenticationDatabase admin \
-d mydb-old
# backup only one collection (mydb-old.users):
mongodump -h Cluster0-shard-0/sample-shard-00-00-xyz.mongodb.net:27017 \
--ssl -u user1 -p 123123 --authenticationDatabase admin \
-d mydb-old -c users
# restore the whole db (mydb-old) to mydb-new:
mongorestore -h Cluster0-shard-0/sample-shard-00-00-xyz.mongodb.net:27017 \
--ssl -u user1 -p 123123 --authenticationDatabase admin \
-d mydb-new dump/mydb-old
# restore only one collection (mydb-old.users) to mydb-new.users:
mongorestore -h Cluster0-shard-0/sample-shard-00-00-xyz.mongodb.net:27017 \
--ssl -u user1 -p 123123 --authenticationDatabase admin \
-d mydb-new -c users dump/mydb-old/users.bson
Find out more:
mongodump -d old_db_name -o <path>
and mongorestore -d new_db_name <path>/old_db_name
mongodump --archive --db=olddb | mongorestore --archive --nsFrom='olddb.*' --nsTo='newdb.*'
You can use copydb or clone, or their helpers copyDatabase()
or cloneDatabase()
, but there are some considerations:
http://docs.mongodb.org/v2.2/tutorial/copy-databases-between-instances/#considerations
Maybe you are triggering some of them.
I just tested
db.copyDatabase("test","testCopy","127.0.0.1")
and it worked perfectly.
Let us know if you got success.
Older copyDatabase
and cloneDatabase
are deprecated. You should use a combination of mongodump
and mongorestore
instead.
Source DB: [ name = olddb
(host = localhost
, port = 27017
) ]
Dest. DB: [ name = newdb
(host = localhost
, port = 27017
) ]
1- Run mongodump to dump the olddb
database to an archive mongodump-old-db
:
mongodump --archive="mongodump-old-db" --db=olddb --host=localhost --port=27017
2- Run mongorestore with --nsFrom
and --nsTo
to restore (with database name change) from the archive:
mongorestore --archive="mongodump-old-db" --nsFrom='olddb.*' --nsTo='newdb.*' --host=localhost --port=27017
instead of using an archive file, you can mongodump the olddb
database to the standard output stream and pipe into mongorestore.
Just run:
mongodump --archive --db=olddb --host=localhost --port=27017 | mongorestore --archive --nsFrom='olddb.*' --nsTo='newdb.*' --host=localhost --port=27017
Tip: You may include additional options as necessary, such as the uri (--uri) or host (--host), port (--port), username (--username), password (--password) or other options.
If you need to authenticate:
db.copyDatabase('crm', 'crm_copy', 'localhost', 'admin', '123456')
I just copied the data/db file from a co-worker and it worked perfectly. I imagine it's a good idea to shut down any connections/mongod before doing so.
I would recommend to have a look at the daemon(s). It happened to me that I had to reinitialize it/them (in my case I had a replica set)
use MongoChef is very easy to do, create the same database locally, then copy collection of the source, then past to local newly created db.
mongodump
(docs.mongodb.org/manual/reference/mongodump) andmongoexport
(docs.mongodb.org/manual/reference/mongoexport)?