我需要复制我的 Mongo 数据库及其数据。我试过了
db.copyDatabase( "Old_db", "new_db", "localhost" )
但问题是它只复制一个空白数据库,而不是以前的数据。
最佳答案
2019 年 2 月
自从 db.copyDatabase()
在 v4.0 中已弃用,您应该使用 mongodump
和 mongorestore
而是:
mongodump
--host <source host:port>
--ssl
--username <username>
--password <password>
--authenticationDatabase admin
--db <sourceDbName>
--collection <collection-name>
mongodump
命令会将整个数据库导出到名为 dump/<sourceDbName>
的本地文件夹中。默认情况下,然后使用 mongorestore
导入目标数据库的命令:
mongorestore
--host <target host:port>
--ssl
--username <username>
--password <password>
--authenticationDatabase admin
--db <targetDbName>
--collection <collection-name>
<dump folder/file>
例子:
# 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
了解更多:
https://stackoverflow.com/questions/14846712/