Connecting to mongodb in Nodejs using Google Compute Engine

Let me start by saying that I am by no means a NodeJS / Mongodb / Google Compute guy, or even a server guy, but unfortunately I seem to be the closest to one in the office, so I was asked to download an existing application NodeJS that someone else did, Google figure out the engine.

I can run my app locally just fine, but I struggled to get it going for days trying to find a way to get it, or even anything, on GCE - finally I got to the point where I can load my app codes by following this bookshelf tutorial - https://cloud.google.com/nodejs/getting-started/run-on-compute-engine - and replacing your code with my own. I could also get a bookshelf tutorial to work successfully on GCE.

So far so good, but unfortunately my application didn’t run on GCE and it looks like it lingering not being able to connect to the mongodb database I installed using the Click to Deploy service. This created three VM instances for me - mongo-arb-me7o, mongo-db-0ysz, mongo-db-efg0. My code is running on a separate instance and I don't think the application requires any existing data to be in the database, I think it just needs access to one to store the information.

I really hope my question is relatively simple: what server data do I need to put in my application so that it can connect to the mongodb database on the GCE? I can't find anything on the internet to help me figure out what should be (I don't think the Google documentation on it is very helpful for complete beginners, to be honest).

By default, the application was initially configured on localhost, port 27017, but this logs the "Failed to connect" error on the GCE. The tutorial says to open port 8080 to access the site in general, so I thought I'd try that for the database too. This, interestingly, logs a slightly different error: "Connection closed", although I don't know what that means? When accessed through a browser, it just reads "Internal Server Error".

I also tried to get to all three mongodb instances both by their IP addresses and by their instance names (instead of localhost), but no joy. Am I even right to have the app in a separate instance? If mongodb is going to be installed on the same instance that runs my code, and if so, how would I go about doing that? The fact that it generates three different database instances does not point to me, but I actually have no idea.

If what I've already tried should work and someone wants to see any logs or code samples to try and diagnose the problem, I'm more than happy to provide, but I have fingers that it's just a matter of entering the correct address and port number that I have not found or understood yet.

Any help would be greatly appreciated, thanks a lot.

+3


source to share


1 answer


I think I may have hacked this myself. I came across this post: Google Compute Engine MongoDB failed to connect via Java Driver which is not exactly the same and is not NodeJS so I probably missed it at first, but it refers to the same problem and I tried make some suggestions to it and they seemed to work.

For anyone new to all this node / mongo / gce stuff and lost in a sea of ​​confusion, it seems like the server address should be set to the internal IP of one of the mongo instances. If you've used Click to Deploy, you can find out which one can be found under Deployment and Management> Deployments. On the bottom-right side of your deployment, under Suggested Next Steps, it will say something like "Initial MongoDB Primary Server - mongo-db-0ysz", which refers to one of the instances. Find this instance and get its internal IP address from the list of virtual machines.

I changed the port number to 27017 by default, but I had to open access to it by typing the following command:



gcloud compute firewall-rules create default-allow-http-27017 --allow tcp:27017 --source-ranges 0.0.0.0/0 --target-tags http-server --description "Allow port 27017 access to http-server"

      

I knew it would be something like this. I can't believe I finally figured it out! Can't anyone spot any problem with what I've done? This other post mentions security issues, but I don't know if this applies here? Again, any advice is greatly appreciated!

+5


source







All Articles