Just installed mongodb using click-to-deploy in google cloud platform. I have another project, for which I created the mongodb database, where my web application runs. Do I have to open some port or configure something?
-
I answered to a similar question here https://stackoverflow.com/questions/61289948/mongosocketreadexception-prematurely-reached-end-of-stream-while-connecting-mo/76868718#76868718 – Andrea Ciccotta Aug 09 '23 at 14:25
5 Answers
As the other answers in this thread suggest, mongod
daemon is listening on TCP port 27017
. Therefore, you will need to add a firewall rule on Compute Engine firewall for this port and protocol. This can be done using Google Cloud console or using gcloud
command tool:
gcloud compute firewall-rules create allow-mongodb --allow tcp:27017
It is recommended to use target tag with the firewall rule and use this target tag to specify what VM instances the firewall rule should be applied to.

- 3,397
- 26
- 40
Adding the port in the firewall is not enough. By default the host bind to 127.0.0.1 which needs to be changed to 0.0.0.0 Make changes in the file sudo nano /etc/mongod.conf inside the instance Look for the term bindIp change it to 0.0.0.0 and restart mongodb You will be able to connect to the mongo db now

- 167
- 2
- 7
-
This is correct. The accepted answer should be "merged" with this one. In my Windows Google Compute Engine, I needed to open the same port in Windows Firewall too. – brunosp86 Dec 31 '19 at 14:44
Click on the Http or Https checkbox to activate the external ip address so u can use it to access the database

- 634
- 8
- 18
On the Mongodb project you should open firewall for port 27017.
MongoDB used ports are listed at:
http://docs.mongodb.org/manual/tutorial/configure-linux-iptables-firewall/
Regards,
Paolo

- 876
- 5
- 8
This answer explains how to set the firewall rule for port 27017
.
Another issue that could cause this is running your mongodb in a separate network and having your other instances on the default network (or vice versa).
I ran into this and after getting both instances on the same network, it was able to connect to the mongo instance by name.
Here's an example of how to set the network for a managed VM in your app.yaml
:
network:
instance_tag: https-server
name: my-node-network