Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Platform - Can't connect to mongodb

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?

like image 545
Italo Maia Avatar asked Feb 27 '15 23:02

Italo Maia


People also ask

Why is my MongoDB not connecting?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.

Can I use MongoDB on Google Cloud?

Run MongoDB Atlas, a fully managed developer data platform, on Google Cloud in just a few clicks. Set up, scale, and operate MongoDB Atlas anywhere in the world with the versatility, security, and high availability you need to build and manage any application.

Why is MongoDB compass not connecting?

This error often occurs when: You provide no hostname or an invalid hostname to the Compass connect dialog. The destination server rejects a connection on an incorrect port. Your MongoDB cluster or server has been shutdown or the server hostname has changed.


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

like image 156
Kamran Avatar answered Sep 28 '22 04:09

Kamran


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

like image 40
Paul Johny Avatar answered Sep 28 '22 05:09

Paul Johny