Google Cloud Platform - Can't connect to mongodb

10,513

Solution 1

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.

Solution 2

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

Share:
10,513
Italo Maia
Author by

Italo Maia

Your friendly neighborhood python dev

Updated on June 12, 2022

Comments

  • Italo Maia
    Italo Maia almost 2 years

    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?

  • brunosp86
    brunosp86 over 4 years
    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.