在Google App Engine上连接猫鼬 [英] Connecting Mongoose on Google App Engine

查看:69
本文介绍了在Google App Engine上连接猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google App引擎引擎上成功部署了自定义Node.js应用.但是我在启动应用程序时遇到问题,因为猫鼬在尝试连接时超时.令人沮丧的是,猫鼬在我的本地计算机上使用完全相同的参数就可以正常连接.

I've deployed a custom Node.js app on Google App engine engine successfully. However I'm having issues starting the app because mongoose times out when attempting to connect. Frustratingly, the mongoose connects just fine on my local machine with the exact same parameters.

我的MongoDb URI的格式为:

My MongoDb URI is of the form:

 mongodb://<dbuser>:<dbpassword>@xxxx.mlab.com:<portNumber>/d‌​b-name

正如URI所暗示的那样,数据库由mlab托管.它是Google Cloud Platform上的沙箱实例.但是,我没有使用Google Compute Engine部署数据库.在mlab上进行设置过程时,我只是选择托管在GCP上.

As the URI implies, the db is hosted by mlab. It is a sandbox instance on Google Cloud Platform. I however did not deploy the db with Google Compute Engine. I merely opted to host in on GCP when going through the setup process on mlab.

我在SO上遇到过类似的问题,但是大多数问题都没有一个公认的答案.只是改变评论中的论述,没有可接受的解决方案.

I've come across similar questions on SO but most of them do not have an accepted answer. Just varying discourse in comments with no accepted solution.

因此,我的问题是,当尝试连接到上述URI时,我的App Engine实例和本地计算机之间有什么区别?我为mlab选择将其托管在GCP上的事实是否重要?如果我选择将其托管在Amazon AWS上,会有所不同吗?问题的根本原因到底是什么?

My question therefore is, what differs between my App Engine instance and my local computer when trying to connect to the above URI? Does the fact that I chose for mlab to host it on GCP matter? Would it be any different if I chose for it to be hosted on Amazon AWS? What exactly is the root cause of the issue?

作为参考,这是我发现的类似问题:

For reference, here are similar questions I've found:

  • 使用猫鼬连接到mongodb时出错在Google计算引擎上

    无法连接到mongodb在Google App Engine上部署应用程序后

    如果有帮助,mongodb还将用作业务模型的数据库,应用程序的Express Session的数据存储以及用于存储来自socket.io的瞬态数据的数据库.

    Also, if it helps, mongodb is used as the db for business models, the datastore for the app's Express Session, and for storing transient data from socket.io.

    部署后来自App Engine的错误stacktrace:

    Error stacktrace from App Engine after deployment:

    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:

    Error stacktrace from running app locally in Google Cloud Shell:

    /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.<anonymous> (/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.<anonymous> (/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.<anonymous> (/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)
    

    推荐答案

    这花了我太多时间,但是问题出在我的Dockerfile上.

    Well this took me entirely too long, but the issue was with my Dockerfile.

    我正在扩展node.js运行时,但是我的Dockerfile缺少用于复制应用程序目录并安装npm的行.

    I was extending the node.js runtime but my Dockerfile was missing lines to copy the app directory and install npm.

    要创建适当的Dockerfile,我使用以下命令:

    To create the appropriate Dockerfile I used the following command:

    gcloud beta app gen-config --custom
    

    有关该命令和扩展node.js运行时的更多信息,请参见这里.

    More information about the command and extending the node.js runtime can be found here.

    添加所需的依赖项后,我的最终Dockerfile如下所示:

    My final Dockerfile after I added the dependencies I needed looks like this:

    注意:部署后,我只能连接到我的MongoDb实例.在Cloud Shell中本地运行仍然超时.

    NOTE: I can only connect to my MongoDb instance after deployment. Running locally in Cloud Shell still times out.

    # 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 App Engine上连接猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆