【发布时间】:2019-01-02 02:13:07
【问题描述】:
我的 MongoDB 服务器托管在 google-cloud VM 上。我希望创建 App Engine 微服务。测试连通性,
我的 server.js 看起来像
const MongoClient = require('mongodb').MongoClient;
const test = require('assert');
// Connection url
const url = 'mongodb://testmongodb:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, { useNewUrlParser: true },function(err, client) {
if(err){console.log(err)}
else {console.log("Connected successfully")}
});
如果我通过另一个虚拟机连接,它会完美运行。但在尝试通过 Google Cloud Shell 执行 (npm start) 相同的代码时不起作用。我收到错误
{ MongoNetworkError: failed to connect to server [testmongodb:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND testmongodb testmongodb:27017]
at Pool.<anonymous> (/home/google/mng/node_modules/mongodb-core/lib/topologies/server.js:562:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/home/google/mng/node_modules/mongodb-core/lib/connection/pool.js:316:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/home/google/mng/node_modules/mongodb-core/lib/connection/connection.js:245:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
name: 'MongoNetworkError',
message: 'failed to connect to server [testmongodb:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND testmongodb testmongodb:27017]',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
我在部署服务 [gcloud app deploy] 时遇到完全相同的错误
请帮忙。
【问题讨论】:
-
在 mongod.conf 上,将 bindip 的设置更改为 0.0.0.0
-
防火墙设置改为开放端口27017
-
请帮我理解一下,什么是“testmongodb”?是虚拟网络中vm的名字吗?
-
是的,它是我的 mongodb 所在的 VM 的主机名。它在同一个虚拟网络中
-
酷。我猜如果两台服务器都在同一个虚拟机中,使用 localhost 而不是 hostnname 应该可以工作。
标签: node.js mongodb google-app-engine google-cloud-platform microservices