How to remote access gcp ports of mongodb on gcp VM? - mongodb

I have installed and running mongodb on port 27017 of my VM on gcp. But I am not able to access database from my pc or any external client.
I have set firewall rules to allow ingress traffic with tcp protocol on port 27017(tcp:27017) from all ip ranges(0.0.0.0/0) and have set priority 1000.
firewall rules snapshot
Now when i am trying to access the database from external client I am not able to access it. I even tried mongodb compass and used ip address of VM and port 27017 to access the database. But unable to connect and showing(mongodb not running on provided host and port) however I am able to access to database when accessed from that VM itself in which I have installed the mongodb.
Please suggest if I am missing out anything.

By default, MongoDB doesn’t allow remote connections. So I would suggest you follow this tutorial to resolve your issue.
Let me know if this was helpful.

Add --bind_ip_all to mongodb server start command.

Related

Cannot access local MongoDB from sam local

I have Windows 10 machine where MongoDB is installed. I can connect it from a command line. I run NodeJS app with sam local. When I use a production environment, the app can access Mongo Atlas cloud instance. But when I switch to a dev environment with localhost MongoDB it fails to connect.
The sam command starts Docker so it is clear why it cannot connect Mongo running on windows localhost. I found relevant question: From inside of a Docker container, how do I connect to the localhost of the machine?. The problem is that I still cannot connect my local MongoDB, even if I try:
"MONGODB_URI": "mongodb://docker.for.win.localhost:27018/bud?retryWrites=true&w=majority"
or
"MONGODB_URI": "mongodb://host.docker.internal:27018/bud?retryWrites=true&w=majority"
Error:
Request failed { MongoNetworkError: failed to connect to server [docker.for.win.localhost:27018] on first connect [MongoNetworkError: connect ECONNREFUSED 192.168.65.2:27018]
Has anybody faced this issue as well and overcome it? Mongo is installed directly to windows, not in Docker.
If MongoDB is installed and running directly from windows, it should be accessible via localhost:27017. Default port for mongod is 27017, as described in mongoDB documentation page.
Try using:
"MONGODB_URI": "mongodb://localhost:27017/bud?retryWrites=true&w=majority"
If you are using NETWORKS_DRIVER other than bridge for your NodeJS docker container, which is set by default. Refer to Docker Network drivers
Other cases:
The default port for mongod is 27018 when running with --shardsvr command-line option or the shardsvr value for the clusterRole setting in a configuration file.
The default port for mongod is 27019 when running with --configsvr command-line option or the configsvr value for the clusterRole setting in a configuration file.
Remember, that localhost (or any name) is just for your convinience. Tcp stack works on ip addresses. If you configure dns service (e.g. via hosts file) to resolve name to 127.0.0.1 for container it doesn't mean your host, but 127.0.0.1 points to the container, always.
You could make mongo service to listen on your main ip and use it for docker app, but you can also leverage hyper-v virtual network cards and setup mongo to listen not only on host's loopback interface, but also on the virtual one and give docker app ip of that interface. It remains on your virtual lan, therefore it's not exposed to public. However, windows firewall might block it, so make sure you set it up as private network (it will be marked as unidentified and by default is public, which usually has stuff blocked).

MongoDB VPS client_ip_address

I'm setting up a MongoDB on a VPS that will run a Private network so that my apps on another VPS will be able to access it. I'm following the excellent Digital Ocean documentation, however, I'm not sure what client_ip_address is referring to. Does this mean the MongoDB VPS ip, the MongoDB VPS private network ip or the app VPS ip.
I'm a noob when it comes to databases.
Next, we'll allow access to the default MongoDB port, 27017, but
restrict that access to a specific host. If you've changed the default
port, be sure to update it in the command below.
sudo ufw allow from client_ip_address to any port 27017

How can i connect with mongodb in SlamData for my EC2 server

I can successfully connected with mongodb on SlamData but for my localhost. Now I have a mongodb database on my EC2 server that database i want to connect in Ec2 then How can I ?
I know username and password. What should be a host and port?
If you are running Slamdata on your host and want to connect to MongoDB running on a remote EC2 host.
Then apart from the username and password, the host is the Public IP or host FQDN [ec2-xxxxxxx], port is 27017. For this check if your mongos is running and binding on 0.0.0.0. If it is binding to 127.0.0.1, you cannot connect remotely
Also check you AWS security group for EC2 and see that the port 27017 is open for your IP or in general to all [0.0.0.0] (this is NOT recommended!!!)
You can also review the FAQ on docs.slamdata.com located here.
Provides step-by-step troubleshooting for connecting to cloud-hosted MongoDB servers.
Additionally, the Path value refers to path inside of SlamData rather than the operating system path. For instance entering a value of aws1 results in a query path similar to the following, assuming you have a databased named demo and a collection named coll1:
SELECT * FROM `/aws1/demo/coll1`

Google Cloud Mongo DB: External IP not connecting

I have created a ready to go MongoDB server on Google Cloud using the default parameters. Everything is working fine between them (there is communication and I can add DBs and collections). However, I can't connect to MongoDB on any external machine.
I created the firewall rules in GCP allowing all the connections ("0.0.0.0./0") on the port 27017.
I am running the command:
giuseppe#ubuntu:~$ mongo --host rs0/104.154.xx.xxx,173.255.xxx.xxx,104.197.xxx.xxx
giuseppe#ubuntu:~$ mongo --host rs0/104.154.xxx.xxx:27017,173.255.xxx.xxx:27017,104.197.xxx.xxx:27017
I'm getting the same error on both of them. I don't know how to resolve this issue.
connecting to: rs0/104.154.41.xxx,173.255.xxx.xxx,104.197.22.xxx:27017/test
2015-03-18T19:47:33.770-0500 starting new replica set monitor for replica set rs0 with seeds 104.154.41.xxx:27017,104.197.22.1xx:27017,xx.255.114.xxx:27017
2015-03-18T19:47:33.770-0500 [ReplicaSetMonitorWatcher] starting
2015-03-18T19:47:34.119-0500 changing hosts to rs0/mongo-db-jff3:27017,mongo-db-vnc4:27017 from rs0/104.154.41.246:27017,1xx.197.22.xxx:27017,173.255.1xx.xx:27017
2015-03-18T19:47:34.493-0500 getaddrinfo("mongo-db-vnc4") failed: Name or service not known
2015-03-18T19:47:34.511-0500 getaddrinfo("mongo-db-jff3") failed: Name or service not known
2015-03-18T19:47:34.512-0500 Error: connect failed to replica set rs0/104.154.xxx.xxx:27017,173.2xx.xxx.68:27017,104.197.22.xxx:27017 at src/mongo/shell/mongo.js:148
EDIT:
Here are my firewall settings.
Did you
configure the firewall rule in Google cloud console
provide a tag in your firewall rule
tag your instance with the same tag as the firewall rule
?
I explained how to open a port to the outside world in detail over here. Replace with your own port number.
I belive the issue here is that the ReplicaSetMonitorWatcher is changing hosts to rs0/mongo-db-jff3:27017, where mongo-db-jff3 is not reachable from your network. You need to configure the hosts in the replica set to something that you can reach (static IP or URL).
https://docs.mongodb.com/manual/tutorial/change-hostnames-in-a-replica-set/
Quick example, mongo into your PRIMARY (SECONDARY if you want to do it no downtime):
cfg = rs.conf()
cfg.members[0].host = "mongodb0.example.net:27017"
cfg.members[1].host = "mongodb1.example.net:27017"
rs.reconfig(cfg)

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?
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.
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
Click on the Http or Https checkbox to activate the external ip address so u can use it to access the database
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
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