无法连接到 MongoDB(远程服务器)
Failed to connect to MongoDB ( remote server )
我正在尝试在我刚购买的服务器上部署一个 Angular/NodeJS 应用程序。
(http://o2switch.fr 是提供者)
我设法部署了 angular 部分,但我无法连接到 mongoDB
当我在终端中启动 app.js 时,它显示:
(node:30382) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-00.cgjma.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 15.237.158.132:27017]
at Pool.<anonymous> (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:441:11)
at Pool.emit (events.js:198:13)
at createConnection (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:564:14)
at connect (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:1014:9)
at makeConnection (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:32:7)
at callback (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:300:5)
at TLSSocket.err (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:330:7)
at Object.onceWrapper (events.js:286:20)
at TLSSocket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
(node:30382) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:30382) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
基本上,它无法连接到 MongoDB,即使在本地主机上也能正常工作。
app.js :
mongoose.connect(config.DB_URI, function(err, db){
if (err) throw err;
console.log('Connected to database');
}, { useNewUrlParser: true });
config/server.js :
module.exports = {
DB_URI : 'mongodb+srv://pseudo:password@cluster0.cgjma.mongodb.net/rank'
}
Mongo 连接失败。所以你应该处理承诺拒绝。您可以使用 .catch()
方法来做到这一点。您可以查看 this link.
我正在尝试在我刚购买的服务器上部署一个 Angular/NodeJS 应用程序。 (http://o2switch.fr 是提供者) 我设法部署了 angular 部分,但我无法连接到 mongoDB
当我在终端中启动 app.js 时,它显示:
(node:30382) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-00.cgjma.mongodb.net:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 15.237.158.132:27017]
at Pool.<anonymous> (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:441:11)
at Pool.emit (events.js:198:13)
at createConnection (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:564:14)
at connect (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:1014:9)
at makeConnection (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:32:7)
at callback (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:300:5)
at TLSSocket.err (/home/ewhc6963/nodevenv/node/10/lib/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:330:7)
at Object.onceWrapper (events.js:286:20)
at TLSSocket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
(node:30382) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:30382) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
基本上,它无法连接到 MongoDB,即使在本地主机上也能正常工作。
app.js :
mongoose.connect(config.DB_URI, function(err, db){
if (err) throw err;
console.log('Connected to database');
}, { useNewUrlParser: true });
config/server.js :
module.exports = {
DB_URI : 'mongodb+srv://pseudo:password@cluster0.cgjma.mongodb.net/rank'
}
Mongo 连接失败。所以你应该处理承诺拒绝。您可以使用 .catch()
方法来做到这一点。您可以查看 this link.