Connect to MongoDB Atlas from Google App Engine

2019-02-20 20:15发布

I'm trying to set up an API on the Google App Engine standard environment but I'm having trouble connecting to a distant MongoDB instance.

I use a free M0 cluster on MongoDB Atlas, all IP are whitelisted. The Google App Engine firewall rules allow all traffic from all IP addresses to make sure the connection request is not blocked.

However, I cannot connect to my Mongo instance and I get the following error:

ERROR db_connection querySrv ESERVFAIL <mongo-url>.gcp.mongodb.net

To connect to the MongoDB instance I use Mongoose and do the following:

const db = await mongoose.connect(uri, { useNewUrlParser: true });

However db will always be null since I cannot connect. Is there a way to connect to MongoDB Atlas from App Engine Standard or do I have to use Cloud Datastore ?

3条回答
祖国的老花朵
2楼-- · 2019-02-20 20:57

It works for me with older driver version url: enter image description here

查看更多
成全新的幸福
3楼-- · 2019-02-20 21:01

You don't have to use Datastore. You can connect to a mongoDB hosted outside of Google servers, there is some example code here. I would also like to refer you to this documentation on connecting to external databases from the Standard Environment. Making sure your firewalls are open is necessary (and you have already done that, so that's great). MongoDB Node.JS drivers are listed here, depending on the version you use, different reference documents are available with connection samples (all listed on that same link).

查看更多
成全新的幸福
4楼-- · 2019-02-20 21:03

I had to use the older version of the atlas url and set new url to false:

return connection = await MongoClient.connect(url, {
    useNewUrlParser: false
})

enter image description here

查看更多
登录 后发表回答