尝试使用mongoose库将MongoDB与copysetSet连接字符串连接。但是没有在生产服务器上连接,它使我返回了一个错误:

name: 'MongoError',
 message: 'failed to connect to server [test-machine:27018] on first connect [MongoError: getaddrinfo ENOTFOUND test-machine:27018 test-machine:27018]'
var mongoose = require('mongoose') //version 4.13.6

var options = { "db": {
        "readPreference": "secondaryPreferred"
    },
    "replset": {
        "rs_name": "testReplica",
    }
}

var connection = mongoose.createConnection(`mongodb://localhost:27018/`,options);

connection.on('open', function () {
    console.log('We are connected')
})

connection.on('error', function (err) {
    console.log('Something went wrong')
    console.log(err)
})

注意:上面的代码在我的本地机器上/与我的本地机器一起运行时均能正常显示成功消息。

最佳答案

这是 DNS 解析错误。

将测试机添加到我的/ etc / hosts文件中

关于node.js - MongoError:首次连接时无法连接到服务器[<docker-machine>:<port>],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54745617/

10-12 12:28
查看更多