Connecting Mongoose on Google App Engine
我已经在Google App引擎引擎上成功部署了自定义Node.js应用。但是我在启动应用程序时遇到问题,因为猫鼬在尝试连接时超时。令人沮丧的是,猫鼬在我的本地计算机上使用完全相同的参数就可以正常连接。
我的MongoDb URI的格式为:
1 | mongodb://<dbuser>:<dbpassword>@xxxx.mlab.com:<portNumber>/d??b-name |
正如URI所暗示的那样,该数据库由mlab托管。它是Google Cloud Platform上的沙箱实例。但是,我没有使用Google Compute Engine部署数据库。在mlab上进行设置过程时,我只是选择托管在GCP上。
我在SO上遇到过类似的问题,但大多数问题都没有一个公认的答案。只是改变评论中的论述而没有可接受的解决方案。
因此,我的问题是,当尝试连接到上述URI时,我的App Engine实例和本地计算机之间有什么区别?我为mlab选择将其托管在GCP上的事实是否重要?如果我选择将其托管在Amazon AWS上,会有所不同吗?问题的根本原因到底是什么?
作为参考,这是我发现的类似问题:
-
在应用程序引擎上部署时,我可以从本地主机连接到部署了Google Cloud的mongoDB,但不能从同一应用程序连接到
-
在Google计算引擎上使用猫鼬连接到mongodb时出错
-
在Google App Engine上部署应用程序后无法连接到mongodb
另外,如果有帮助,则将mongodb用作业务模型的数据库,应用程序的Express Session的数据存储以及用于存储来自socket.io的瞬时数据。
部署后来自App Engine的错误stacktrace:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 2017-10-18 02:13:46 default[20171017t215757] npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json' 2017-10-18 02:13:46 default[20171017t215757] npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json' 2017-10-18 02:13:46 default[20171017t215757] npm ERR! enoent This is most likely not a problem with npm itself 2017-10-18 02:13:46 default[20171017t215757] npm ERR! enoent and is related to npm not being able to find a file. 2017-10-18 02:13:46 default[20171017t215757] npm ERR! enoent 2017-10-18 02:13:46 default[20171017t215757] 2017-10-18 02:13:46 default[20171017t215757] npm ERR! Please include the following file with any support request: 2017-10-18 02:13:46 default[20171017t215757] npm ERR! /app/npm-debug.log 2017-10-18 02:23:41 default[20171017t215757] npm ERR! Linux 3.16.0-4-amd64 2017-10-18 02:23:41 default[20171017t215757] npm ERR! argv"/nodejs/bin/node""/nodejs/bin/npm""start" 2017-10-18 02:23:41 default[20171017t215757] npm ERR! node v6.11.3 2017-10-18 02:23:41 default[20171017t215757] npm ERR! npm v3.10.10 2017-10-18 02:23:41 default[20171017t215757] npm ERR! path /app/package.json 2017-10-18 02:23:41 default[20171017t215757] npm ERR! code ENOENT 2017-10-18 02:23:41 default[20171017t215757] npm ERR! errno -2 2017-10-18 02:23:41 default[20171017t215757] npm ERR! syscall open |
在Google Cloud Shell中本地运行应用程序时出现错误stacktrace:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /home/myuser/src/project-id/teammate-express-server/node_modules/mongodb/lib/mongo_client.js:421 throw err ^ MongoError: failed to connect to server [ds147454.mlab.com:47454] on first connect [MongoError: connection 1 to ds147454.mlab.com:47454 timed out] at Pool. (/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb-core/lib/topologies/server.js:336:35) at emitOne (events.js:96:13) at Pool.emit (events.js:188:7) at Connection. (/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb-core/lib/connection/pool.js:280:12) at Connection.g (events.js:292:16) at emitTwo (events.js:106:13) at Connection.emit (events.js:191:7) at Socket. (/home/myuser/src/project-id/teammate-express-server/node_modules/mongodb-core/lib/connection/connection.js:197:10) at Socket.g (events.js:292:16) at emitNone (events.js:86:13) at Socket.emit (events.js:185:7) at Socket._onTimeout (net.js:338:8) at ontimeout (timers.js:386:14) at tryOnTimeout (timers.js:250:5) at Timer.listOnTimeout (timers.js:214:5) |
如前所述,您可以按照https://cloud.google.com/nodejs/getting-started/deploy-mongodb上的说明进行操作
使用
由于您使用的是
如果直接使用
What differs between my App Engine instance and my local computer when trying to connect to the above URI?
Google App Engine是一个完全托管的平台。从某种意义上说,它是无服务器的,您正在利用工作人员池来扩大/缩小应用程序。当请求进入您的应用程序时,池中的实例将处理任务/请求。
与本地计算机不同,请求也将由您的计算机处理。
您可能还会发现Google App Engine常见问题解答很有用。
Does the fact that I chose for mlab to host it on GCP matter?
不是很多。如前所述,您目前正在使用GCP进行SandBox(免费)计划。目前,这会将您的MongoDB实例部署在
您也可以通过在
What exactly is the root cause of the issue?
如果没有正确的堆栈跟踪或错误日志描述超时问题,很难确定是什么问题。
通常,
1 2 3 4 | nconf.argv() .env() .file({ file: path.join(__dirname, 'config.json') }) .defaults() |
基本上试图按以下顺序读取配置:
- 命令行参数
- 环境变量
- 配置文件(指定路径)
- 默认值
另请参阅Google Cloud Platform:nodejs-getting-started
还以GoogleCloudPlatform / nodejs-docs-samples / appengine / mongodb项目为例。
好吧,这花了我太多时间,但是问题出在我的Dockerfile上。
我正在扩展node.js运行时,但是我的Dockerfile缺少用于复制应用程序目录并安装npm的行。
为了创建适当的Dockerfile,我使用了以下命令:
1 | gcloud beta app gen-config --custom |
可以在此处找到有关该命令和扩展node.js运行时的更多信息。
添加所需的依赖项后,我的最终Dockerfile如下所示:
注意:部署后,我只能连接到我的MongoDb实例。在Cloud Shell中本地运行仍然超时。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # Dockerfile extending the generic Node image with application files for a # single application. FROM gcr.io/google_appengine/nodejs COPY . /app/ RUN apt-get -y update && apt-get install -y libav-tools && apt-get install -y graphicsmagick # You have to specify"--unsafe-perm" with npm install # when running as root. Failing to do this can cause # install to appear to succeed even if a preinstall # script fails, and may have other adverse consequences # as well. # This command will also cat the npm-debug.log file after the # build, if it exists. RUN npm install --unsafe-perm || \\ ((if [ -f npm-debug.log ]; then \\ cat npm-debug.log; \\ fi) && false) CMD npm start |
我有一段时间有同样的问题,但最终对我有用。希望它能帮上忙。当我使用google https://cloud.google.com/community描述的nconf包从json文件中读取db连接详细信息时/ tutorials / nodejs-mongodb-on-appengine没有猫鼬,我能够解决它。最初我是从环境变量中读取的。
1 2 3 4 5 6 7 8 9 10 11 | ` nconf = require('nconf'); const mongoose = require('mongoose'); //key.json contains mongodb connection information nconf.argv().env().file('keys.json'); const user = nconf.get('mongoUser'); const pass = nconf.get('mongoPass'); const host = nconf.get('mongoHost'); const port = nconf.get('mongoPort') const mongoDatabase=nconf.get('mongoDatabase'); let mongo_url=`mongodb://${user}:${pass}@${host}:${port}/${mongoDatabase}`; mongoose.connect(mongo_url,{useNewUrlParser: true});` |
您不必按照其他答案的建议使用config.json。