复制/克隆 mongodb 数据库及其数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14846712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:03:06  来源:igfitidea点击:

Copy/Clone mongodb database along with its data

mongodb

提问by user1795109

I need to copy my Mongo database along with its data. I have tried

我需要复制我的 Mongo 数据库及其数据。我试过了

db.copyDatabase( "Old_db", "new_db", "localhost" )

But the problem is it only copies a blank db, not with the previous data.

但问题是它只复制一个空白的数据库,而不是以前的数据。

回答by Moacy Barros

You can use copydb or clone, or their helpers copyDatabase()or cloneDatabase(), but there are some considerations:

您可以使用 copydb 或 clone,或者它们的助手copyDatabase()cloneDatabase(),但有一些注意事项:

http://docs.mongodb.org/v2.2/tutorial/copy-databases-between-instances/#considerations

http://docs.mongodb.org/v2.2/tutorial/copy-databases-between-instances/#thinkations

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.

如果您成功了,请告诉我们。

回答by backslash112

Feb 2019

2019 年 2 月

since db.copyDatabase()was deprecated in v4.0, you should use mongodumpand mongorestoreinstead:

由于db.copyDatabase()在 v4.0 中已弃用,您应该使用mongodumpandmongorestore代替:

mongodump 
    --host <source host:port> 
    --ssl 
    --username <username> 
    --password <password> 
    --authenticationDatabase admin 
    --db <sourceDbName> 
    --collection <collection-name>

mongodumpcommand will export the whole database into a local folder named dump/<sourceDbName>by default, then use mongorestorecommand to import to your target database:

mongodump命令会将整个数据库导出到dump/<sourceDbName>默认命名的本地文件夹中,然后使用mongorestore命令导入到目标数据库:

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:

了解更多:

回答by huynhminhtuan

If you need to authenticate:

如果您需要进行身份验证:

db.copyDatabase('crm', 'crm_copy', 'localhost', 'admin', '123456')

回答by VSO

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.

我刚刚从同事那里复制了 data/db 文件,它运行良好。我想在这样做之前关闭任何连接/mongod 是个好主意。

回答by elachell

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)

我建议看看守护进程。我碰巧不得不重新初始化它/它们(在我的情况下,我有一个副本集)

回答by gregOh

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.

使用 MongoChef 很容易做到,在本地创建相同的数据库,然后复制源的集合,然后传递到本地新创建的数据库。