简体   繁体   中英

How to copy database from one server to another in mongodb

I am trying to copy a database [mongodb] in production server hosted in nginx to my local dev environment setup [linux machine].

I tried the solution given in following url How do I copy a database from one MongoDB server to another?

But when I establish a ssh tunnel and try to copy, I am getting following error

db.copyDatabase("admin","testadmin","localhost:27018")
{
        "ok" : 0,
        "errmsg" : "couldn't connect to server localhost:27018, connection attempt failed"
}

if I try to copy the database directly in my destination server. I am getting the following error

db.copyDatabase("admin","testadmin","mongodb://<remotehostname>:27017")
{
        "operationTime" : Timestamp(1545126863, 1),
        "ok" : 0,
        "errmsg" : "Must specify set name for replica set ConnectionStrings",
        "code" : 9,
        "codeName" : "FailedToParse",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1545126863, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

Please help. I am stuck with issue for two days. I am not sure whether I can use mongodump and restore. I am using mongo version 3.4 in my remote server and mongo version 3.6 in my local. Could this be a problem?

I also tried adding replica set, but got below error. How to find the replica set name?

db.copyDatabase("admin","testadmin","mongodb://<remotehostname>:27017/?replicaSet=test")
{
        "operationTime" : Timestamp(1545117923, 135),
        "ok" : 0,
        "errmsg" : "Bad digit \"/\" while parsing 27017/?replicaSet=test",
        "code" : 9,
        "codeName" : "FailedToParse",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1545117923, 135),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

An alternative would probably be to use mongoDump/mongorestore.

https://stackoverflow.com/a/22201759/5107937

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM