I can connect to a google cloud deployed mongoDB from localhost but not from the same app when deploying on the app engine

穿精又带淫゛_ 提交于 2019-12-24 06:47:31

问题


My app is written in React and Express using Mongoose for the db. I deployed a bitnami mongoDB instance on google compute engine. Now I'm trying to deploy the server on the App engine. When I run my app locally it connects fine to the db (I can add stuff and see it). But when I do an npm start in the google cloud shell is get this error:

'failed to connect to server [35.196.182.249:27017] on first connect [MongoError: connection 0 to 35.196.182.249:27017 timed out]' }
(node:1310) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: failed to connect to server [35.196.182.249:27017] on first connect [MongoError: connection 0 to 35.196.182.249:27017 timed out]

Here is my code to connect:

mongoose.connect(process.env.MONGODB_URI,{
    useMongoClient: true
}).catch(function(err){
    console.log(err)
});

where MONGODB_URI=mongodb://root:password@XX.XXX.XXX.XXX:27017, password is the password I got from the compute engine, and XX.XXX.XXX.XXX is the external IP provided by the compute engine. I use the same URI when connecting locally and in the google cloud shell but it only connects from localhost. I tried hardcoding in the URI too and it didn't work. Any ideas?

来源:https://stackoverflow.com/questions/45601970/i-can-connect-to-a-google-cloud-deployed-mongodb-from-localhost-but-not-from-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!