MongoError:首次连接时无法连接到服务器[<docker-machine>:<port>]

时间:2019-02-18 10:52:51

标签: node.js mongodb docker mongoose replicaset

尝试使用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)
})

注意:上面的代码在我的本地计算机上/在我的本地计算机上都可以正常工作并显示成功消息。

1 个答案:

答案 0 :(得分:0)

这是DNS解析错误。

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