Let me start by saying I am by no means a NodeJS/Mongodb/Google Compute engine guy whatsoever, or even a server guy, but unfortunately it seems I am the closest to being one in the office, so I've been asked to upload an existing NodeJS app that someone else has made, to Google compute engine.
I can run the app locally just fine, but I've been struggling for days trying to work my way around getting it, or indeed anything, up on GCE - finally I've got to the point where I can upload my app code by following this Bookshelf tutorial - https://cloud.google.com/nodejs/getting-started/run-on-compute-engine - and replacing their code with my own. I could also get the bookshelf tutorial itself to work on GCE successfully.
So far so good but unfortunately my app doesn't run on GCE, and it appears that it gets held up by not being able to connect to a mongodb database I've set up using the Click to Deploy service. This has generated three VM instances for me - mongo-arb-me7o, mongo-db-0ysz, mongo-db-efg0. My code runs on a separate instance, and I don't think the app requires any existing data to be on the database, I think it just needs access to one to store information.
I really hope my question is a relatively simple one - what server details do I need to place into my app to allow it to connect to the mongodb database on GCE? I can't find anything at all online to help me work out what it should be (I don't find Google's documentation on it very helpful for complete newbies to be honest).
By default, the app was originally set to localhost, port 27017 but this logs an error "Failed to connect" on GCE. The tutorial says to open up port 8080 for access to the site in general, so I thought I'd try that for the database too. This, interestingly, logs a slightly different error: "Connection closed", though I don't know what that indicates? When accessed through the browser, it simply reads "Internal Server Error".
I've also attempted to get to all three mongodb instances by both their IP addresses and their instance names (in place of localhost), but to no joy. Am I even right to have the app in a separate instance? Should mongodb be installed on that same instance that runs my code, and if so, how do I do that? The fact that it generates three different instances for the database suggests not, to me but really I have no idea.
If what I've already tried should work and anyone wants to see any logs or code samples to try and diagnose the problem, I'm more than happy to provide, but I've got my fingers crossed that it's just a matter of putting the right address and port number in, which I am yet to find or figure out.
Any help would be greatly appreciated, thanks a lot.
I think I may have cracked it myself. I stumbled upon this other post: Google Compute Engine MongoDB trouble connecting via Java Driver which isn't exactly the same, and isn't NodeJS, which is why I probably missed it at first, but it sort of relates to the same issue and I attempted some of the suggestions in it and they seemed to work.
For any fellow newbies at all this node/mongo/gce stuff, and lost in a sea of confusion, it seems the server address needs to be set to the internal IP address of one of the mongo instances. If you used Click to Deploy, you can find out which one by going to Deploy and Manage > Deployments. On the lower right hand side of your deployment, under Suggested next steps, it'll say something like "The initial MongoDB primary server is mongo-db-0ysz" - which refers to one of the instances. Find that instance and get its internal IP from the list of VMs.
I changed the port number back to the default 27017, but I had to open up access to it by typing this 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 that. Can't believe I've finally got it working though! Unless anyone can spot any issues with what I've done? That other post mentions security concerns, but I don't know if that applies here? Again, any advice greatly appreciated!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With