4

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 ?

tom
  • 745
  • 5
  • 20
  • My Mongoose knowledge is not that extense, but, shouldn't be your URI something like `mongodb://username:password@host:port/database?options...`? Like mentioned [in here](https://mongoosejs.com/docs/connections.html) – Mangu Aug 21 '18 at 11:06
  • 1
    Cannot use mongodb with Google App Engine standard environment, Datastore is the only option. If mongodb has to be used move to **flexible environment** – cheekujha Aug 21 '18 at 11:25
  • @Mangu this is not the connection uri, hence the different format. The connection uri I use indeed use the format you specified :) – tom Aug 21 '18 at 11:36
  • @cheekujha I see, thanks for the answer! – tom Aug 21 '18 at 11:36
  • @cheekujha Not totally correct, you can use Mongodb in App Engine Standard. I have tried out [this](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/mongodb) in the past, and it worked. This bit is also mentioned [in the public documentation](https://cloud.google.com/appengine/docs/standard/nodejs/using-third-party-databases#databases_managed_by_a_third-party_vendor) – Mangu Aug 21 '18 at 11:38
  • @Mangu: You'll have to find a third-party host for MongoDb that provides a REST interface through which you can make your transactions then you can use it. Exmaple - Mongolab etc.. The question asks about connecting through IP which is not possible on standard environment. – cheekujha Aug 21 '18 at 11:49
  • @cheekujha Tbh, my knowledge in Mongo is not that big, so you may be right, but I did test this with an external IP and it worked. Tbh, it was a GCE instance with Mongo running, but, nonetheless, it was a MongoDB to which I connected through an external IP in the Standard environment. – Mangu Aug 21 '18 at 11:54
  • 1
    @Mangu: Yes its possible on GCE. But not possible on GAE – cheekujha Aug 21 '18 at 11:56
  • @cheekujha Wait, I'm saying that I connected from GAE Standard to Mongodb running on GCE, which was what OP wanted. – Mangu Aug 21 '18 at 11:59
  • @Mangu: to my knowledge it is not supported. Here a reference to another question with same discussion [https://stackoverflow.com/questions/4263395/any-one-tried-mongodb-on-google-app-engine](https://stackoverflow.com/questions/4263395/any-one-tried-mongodb-on-google-app-engine) – cheekujha Aug 21 '18 at 12:03
  • @cheekujha Hey, that's for running MongoDB IN App Engine, not connecting from App Engine, which I believe is what OP intends to do. Also, check another question on the matter which I answered https://stackoverflow.com/questions/51515839/nodejs-not-able-to-connect-to-mongodb-on-cloud-shell/51557017#51557017 – Mangu Aug 21 '18 at 12:56
  • You can't assign a static IP to an app engine project so not sure how you've added "all IP addresses"? – Dominic Dec 13 '18 at 18:45

3 Answers3

7

I had to use the older version of the atlas url

enter image description here

bj97301
  • 342
  • 1
  • 13
1

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

polis
  • 737
  • 7
  • 6
0

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).