应用程序在Google应用程序引擎上部署后,无法连接到mongodb [英] Cannot connect to mongodb after app deploys on google app engine

查看:116
本文介绍了应用程序在Google应用程序引擎上部署后,无法连接到mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是部署在Google Compute Engine上的Bitnami mongodb实例。

我可以通过MongoDB客户端从我的个人计算机连接到该mongodb实例, MongoDB实例。

我的应用程序是一个带有Node.js后端的Angular2应用程序。以下是server.js中使用的连接字符串格式

  mongoose.connect('mongodb://< username>:lt ;密码> @<外部IP>:27017 /< dbname>'); 

当我在本地主机上托管我的应用程序时,它可以连接到云上的mongodb实例并访问数据, 。

但是,当应用程序部署在谷歌应用程序引擎上时,应用程序不再连接到mongodb。
以下是错误信息。

  MongoError:无法连接到服务器[< External IP>:27017]首先连接

有人可能会提出什么是根源。

另外,App Engine应用程序可以通过公共连接到数据库如果数据库服务器和防火墙配置正确以接受连接,则为Ip。您的App Engine应用程序使用服务的公共IP地址连接到数据库。



默认情况下,mongodb守护进程正在侦听TCP端口27017.因此,您需要添加此端口和协议的计算引擎防火墙上的防火墙规则。这可以使用Google云端控制台或使用 gcloud 命令工具完成:

gcloud compute firewall-rules create allow-mongodb --allow tcp:27017


I'm using Bitnami mongodb instance deployed on Google Compute Engine.

I can connect to that mongodb instance through MongoDB clients from my personal computer using the External IP of MongoDB instance.

My app is an Angular2 app with Node.js backend. Following is the connection string format used in server.js

mongoose.connect('mongodb://<username>:<password>@<External IP>:27017/<dbname>');

When I host my app on localhost it can connect to mongodb instance on the cloud and access data without issues.

But, when the app is deployed on google app engine the app no longer connects to the mongodb. Following is the error.

MongoError: failed to connect to server [<External IP>:27017] on first connect

Can somebody suggest what is the root cause.

解决方案

App Engine instances are part of the same private internal network as Compute Engine instances. Your App Engine application can communicate with services hosted on Compute Engine within the same project without going through the public internet. In many cases, your application connects to the service using the instance's internal IP address without needing to assign the database an external, public IP address. If desired, you can assign a public IP address to allow the database to be accessed from outside of Google Cloud Platform.

Also, App Engine applications can connect to databases via public Ip if the database server and firewall are configured properly to accept connections. Your App Engine application connects to the database using the service's public IP address.

By default, mongodb daemon is listening on TCP port 27017. Therefore, you will need to add a firewall rule on Compute Engine firewall for this port and protocol. This can be done using Google Cloud console or using gcloud command tool:

gcloud compute firewall-rules create allow-mongodb --allow tcp:27017

这篇关于应用程序在Google应用程序引擎上部署后,无法连接到mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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