How to connect from outside to MongoDB Atlas under VPC peering? - mongodb

I have:
MongoDB Atlas Cluster
VPC Peering between GCP.
When I tried connect to DB from my home laptop:
mongo "mongodb+srv://hello-dev-x53pa.gcp.mongodb.net/test" --username myuser
I received an error:
2020-01-09T15:32:33.697+0200 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Failed to connect to 192.168.240.4:27017 after 5000ms milliseconds, giving up.
2020-01-09T15:32:33.698+0200 W NETWORK [thread1] Failed to connect to 192.168.240.2:27017 after 5000ms milliseconds, giving up.
I see that received local IP address but I don't understand how I can connect to the my DB which in the private VPC?
I will be very happy for any help!

You have to whitelist your IP address. VPC Peering allowed GCP resources to connect to your database, but you also need to specify your private IP address.
Refer to below document to add IP address:
https://docs.atlas.mongodb.com/security-whitelist/#add-whitelist-entries
EDIT:
I wouldn't whitelist all IP addresses, just use the "current IP address" option to white list the IP address your laptop is trying to access from.
Also, try adding --password option, as this should be used in conjunction with --username option. Also, confirm that "test" db is where you created the username/password.
mongo "mongodb+srv://hello-dev-x53pa.gcp.mongodb.net/test" --username myuser --password mypassword

Related

How to get the IP address of Heroku so that my Heroku hosted backend server can connect to MongoDB?

I'm trying to host my backend on Heroku, but after I tried deploying I was getting an "application error" . I checked the "heroku logs" and I think the problem is my MongoDB database only accepts connection to my specific IP address.
2023-02-07T09:34:05.807213+00:00 app[web.1]: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
How do I get the IP address of Heroku so that I can add it to this whitelist?
https://i.imgur.com/K9k0TFk.png
This is the entire Heroku log just in case maybe I'm wrong about the reason it's crashing?
https://pastebin.com/btc79fQq
EDIT:
Also is there a way to connect to the mongo atlas database with username + password instead of IP address maybe?
It seems like it's very dynamic so you need an addon like Fixie socks which sets a static IP of your Heroku app for this exact purpose.

mongodb atlas and aws lightsail VPC peering seem to work but node.js server still cannot connect

I am trying to establish VPC peering between my AWS server and MongoDB Atlas. Everything looks good on both AWS VPC dashboard side and MongoDB Atlas/Network Access/Peering.
Also if I run the following code in my AWS lightsail Ubuntu terminal
host cluster0-shard-00-01.a7dlx.mongodb.net, I get the following response: cluster0-shard-00-01.a7dlx.mongodb.net is an alias for ec2-52-201-68-123.compute-1.amazonaws.com. ec2-52-201-68-123.compute-1.amazonaws.com has address 52.201.68.123
So VPC peering seem to work properly. But for some reason I am still getting this connection error in my Node.js application:
MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
Could my Mongo URI be the problem? Maybe I need some special URI for VPC?? I tried both urls:
mongodb+srv://admin:<password>#cluster0.123.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
mongodb://admin:<password>#cluster0-shard-00-00.123.mongodb.net:27017,cluster0-shard-00-01.123.mongodb.net:27017,cluster0-shard-00-02.123.mongodb.net:27017/myFirstDatabase?ssl=true&replicaSet=atlas-gz4wg0-shard-0&authSource=admin&retryWrites=true&w=majority

Can't connect to amazon rds

I just setup aws rds and I'm trying to connect pgadmin to it. I put in the Endpoint and the port shown in the rds dashboard, and the username and password I set. When I try connecting, I get an error message saying: Unable to connect to server "host name" port "port" failed: timeout expired.
I also tried connecting prisma to it by running npx prisma migrate dev --name init and I also get an error saying P1001: Can't reach database server at "host name".
I made sure to set Publicly accessible to Yes, but it's still not working. What am I doing wrong and how can I fix it?
(For the settings, I used the default free tier settings.)
In the question thread, the security group is defined to accept All Traffic from all IPv6 addresses ::/0. Permission for IPv4 address should be added as well. You may want to address All Traffic for IPv4 address 0.0.0.0/0.

Cannot connect to RDS PostgeSQL instance

I have just created a new RDS instance with PostgreSQL but I cannot connect to it. I'm using the following command to connect:
psql --host=<dbendpoint> --port=5432 --username=<user> --password --dbname=<dbname>
I have been searching for information related to the problem, but none of the solutions have worked.
I have public access to the DB enabled.
I have allowed all traffic in the security group of the database.
The error I'm getting is this one:
psql: error: could not connect to server: Connection timed out
Is the server running on host "database-test-1-by-hand.cwsxzm3kzzim.us-west-1.rds.amazonaws.com" (204.236.171.212) and accepting
TCP/IP connections on port 5432?
Any idea about what's happening?
Update
Here are the options from the VPC the database is on:
I remember enabling the DNS hostname to allow public access.
Update 2
I have created an EC2 instance in the same VPC where the db instance is, and I have been able to connect to it.
The db instance is deployed in the same VPC, where I have 4 different subnets, two public and two private. This is the configuration:

MongoDB Atlas Cannot get console mode 6

I get this error in my terminal whenever I try connecting to MongoDB Atlas. Actually, I have my IP whitelist to 'Allow access from anywhere'. I don't know why i'm getting this problem.
I also tried:
$ mongo "mongodb+srv://cluster0-cueit.mongodb.net/test" --username <usrnm> --password <pwd>
but it gives me different error.
It looks like this is a MongoDB Atlas cluster. Please ensure that your
IP whitelist allows connections from your network.
It should be active and 0.0.0.0/0 (includes your current IP address) under Network Access