mongodb dump and pipe to other db name

别说谁变了你拦得住时间么 提交于 2019-12-11 05:49:14

问题


Mongodb version 3.2.12. I have two local databases, "base1" and "base2"

I want to copy all data (all collections) from base1 over to base2, replacing everything there (like when dumping production to a dev environment).

Any pipe command (or other simple way) to do this?

I tried

mongodump --archive --db base1 | mongorestore --db base2 --archive

lists a lot of "writing base1.collectionname to archive on stdout", but nothing gets written to base2.

I also tried

mongodump --db base1 --gzip --archive=/path/to/file.gz
mongorestore --db base2 --gzip --archive=/path/to/file.gz

Dump works, restore just says "creating intents for archive", "done"


回答1:


I come across the same issue and after some googling and search I found this post https://stackoverflow.com/a/43810346/3785901

I tried this command mentioned:

mongodump --host HOST:PORT --db SOURCE_DB --username USERNAME --password PASSWORD --archive | mongorestore --host HOST:PORT --nsFrom 'SOURCE_DB.*' --nsTo 'TARGET_DB.*' --username USERNAME --password PASSWORD  --archive --drop

and it works like a charm. It should work in your case, good luck.




回答2:


I use following commands:

mongodump \
    --host ${mongo.host} \
    --port ${mongo.port} \
    --username ${mongo.backup_restore_user} \
    --password ${mongo.backup_restore_password} \
    --db ${mongo.db} \
    --gzip \
    --dumpDbUsersAndRoles \
    --archive=${archive}

and

mongorestore \
        --keepIndexVersion \
        --drop \
        --gzip \
        --restoreDbUsersAndRoles \
        --db ${mongo.db} \
        --host ${mongo.host} --port ${pims.mongo.port} \
        --username ${mongo.backup_restore_user} \
        --password ${mongo.backup_restore_password} \
        --archive=${archive}


来源:https://stackoverflow.com/questions/44135991/mongodb-dump-and-pipe-to-other-db-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!