【发布时间】:2018-07-12 16:35:06
【问题描述】:
我正在使用 MongoDB 4.0。我有一个在我的机器和不同端口上运行的副本集:
127.0.0.1:27017 (Master)
127.0.0.1:27018 (Slave)
127.0.0.1:27019 (Arbiter)
My replica name is "xdr"
现在在我的 nodejs 代码中在 localhost 上创建连接时,它将创建连接,即
const options = {
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
autoReconnect : true
};
mongoose.connect('mongodb://localhost:27017, localhost:27018, localhost:27019/my_db?replicaSet=xdr, options);
mongoose.Promise = global.Promise;
//Get the default connection
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
在我的本地连接上一切都很好,但是当我将我的 mongodb 托管在 AWS 上的单独 EC2 实例上时,它不会连接到我的副本集。
假设我的 mongodb AWS IP 是 12.12.13.12。
因此,当我创建连接时,它将无法连接。我的代码是
const options = {
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
autoReconnect : true
};
mongoose.connect('mongodb://12.12.13.12:27017, 12.12.13.12:27018, 12.12.13.12:27019/my_db?replicaSet=xdr, options);
mongoose.Promise = global.Promise;
//Get the default connection
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
它会产生一个错误,即“”
如果我在没有副本集的情况下连接,那么它将只连接到主节点,即
mongoose.connect('mongodb://12.12.13.12:27017/my_db, options);
我的代码有什么做错了吗?
【问题讨论】:
-
嗨,Abhay - 你遇到了什么错误?您的问题似乎缺少它,我想只是一个拼写错误?
标签: node.js mongodb mongoose replicaset