trouble connecting with MongoDB Atlas - node.js

I'm trying to deploy a blog app with a mongoDB Atlas Cluster (I have a mongodb for my local as well), but I can't seem to connect. I keep getting this error in my terminal:
Error: querySrv ENODATA _mongodb._tcp.cluster0-1qme8.mongodb.net
I've read the other posts on this but still can't figure it out. Maybe I'm not configuring the string correctly?
mongoose.connect("mongodb+srv://<myName>:<myPassword>#cluster0-1qme8.mongodb.net/test?retryWrites=true&w=majority", {
useNewUrlParser:true,
useCreateIndex:true,
}).then(() => {
console.log("Connected to DB!");
}).catch(err => {
console.log("Error:", err.message);
});
I've whitelisted all ip's so it's accessible anywhere. I even tried my own ip to test it out and it still wont' work. Does anyone know what my problem is?

Related

How do i resolve this Mongo Server Selection Error

Am quiet new to backend and database. I have a solution currently using mongodb. It was working fine till yesterday when i starting having the connect ETIMEDOUT 13.37.254.237:27017 error. Nothing was changed in the URI path or tampered with. It just started and i have not been able to sort it out.
is there any help available please?
I have created another cluster and its working well. But my initial cluster that has datas which are live from clients is not connecting still.
My connection code
I have used these connections code but it has not worked. It was connecting fine all through yesterday but today without tampering with the code, couldn't connect to my mongodb
mongoose.connect(process.env.MONGO_URI,{ useNewUrlParser: true, useUnifiedTopology: true });
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URL);
console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (error) {
console.log(error);
process.exit(1);
}
};
my mongoose connection and the timedout error
Whenever you connect to Mongodb using IPs that keep updating from your system causes this kind of issues.
also this can be due to your network connection. So i will advice you to:
To allow connection from any IP address(but must ensure your URI is not made known to the public to avoid attack/ access from unwanted users.)
2.Check your network status(data)
3. Run the mongo URI on your atlas

Why does local Robo3t connect to a remote Mongo DB instance, but NodeJs fails with IP whitelist error?

I am having issues connecting to MongoDb running remotely, and the connection error response I am getting from the server is somewhat weird.
My network access whitelist is set to allow all (0.0.0.0/0). Hence, my local robo3t installation was able to connect. However, I could not connect from my NodeJs code. Error is: "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"
IP whitelist seems to be an unlikely error, given that my local robo3t client is able to connect remotely to the same remote Mongo Atlas instance, as IP whitelist is allow-all.
How do I debug this kind of thing, please?
UPDATE: this is how I connect to MongoDb. Works well on local, too.
try {
const connectionString =
process.env.APP_ENV == "test"
? await getInMemoryMongoDbAdapter()
: `mongodb://${process.env.MONGODB_HOSTNAME}:${process.env.MONGODB_PORT}/${process.env.CBT_DATABASE_NAME}`;
logger.info(`Connecting to MongoDB service: ${connectionString}`);
await mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
} catch (error) {
reject(error);
}
The logger line correctly shows: Connecting to MongoDB service: mongodb://<user>:<password>#cluster0-xxx.yyy.zzz.net:<port>/<database>
UPDATE 2:
My localhost also does not connect via this node app; whereas my robo3t (local MongoDb client) connects. I guess that means Heroku-specific issues can now be comfortably ruled out
A decade later, I found that for the connection parameters, I needed to supply the authSource and ssl options, as below:
{
useNewUrlParser: true,
useUnifiedTopology: true,
authSource: "admin",
ssl: true,
}
Neither one works without the other. Big shout-out to #darklightcode for all the insights he gave, leading me to dig deeper. Thanks man!

MongoDB network access

previously I didn't really understand networking. I want to still access the database with a different internet connection. I have added the mongodb atlas whitelist to my current ip or made it allow access from anywhere but I instead got an error. how do i finish this? thanks
this is my code to connect to the server
mongoose.connect(MONGODB_URI)
.then(result => {
console.log('CONNECTED!');
app.listen(3000);
})
.catch(err => console.log(err));
here this error message
`D:\nodeJsApp\MyApp\node_modules\mongodb\lib\topologies\replset.js:368
throw err;
^
Error: Error connecting to db: failed to connect to server `

Digital Ocean MongoDB Connection Error

I use Nodejs with Mongodb connection, it works. I didn't not change anything in code . Now I can't connect to mongodb. When I try connect with Robomongo when I don't use ssh I can't connect
But when I try to connect with ssh I can connect. My connection is success.
I can't understand problem.
My nodejs mongo connection in in here:
MongoClient.connect('mongodb://username:password#ipadress/dbname', (err, database) => {
if (err) return console.log(err)
db = database
app.listen(process.env.PORT || 5000, () => {
console.log('listening on 5000')
})
})
Thank you for help.
Your mongodb is not listening on the external interface but on localhost only so you can't connect directly to the 139.x.x.x IP.
However, if you use SSH it will first tunnel into the host and then connect locally so it works.

Cannot connect to MongoDB via NodeJS - No primary found in replica set error

I am having difficulty connecting to MongoDB via NodeJS. The Mongo setup consists of a shared cluster.
The URL is below:
mongodb://dev.testserver.com:27017,dev.testserver.com:27018,dev.testserver.com:27019/ua?w=0&replicaSet=dev-testserver-com&readPreference=secondary&slaveOk=true
The code that I have is below:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(url, function(err db) {
if(!err) {
console.log("We are connected");
}
db.close();
});
The error that I keep getting is "MongoError: no primary found in replicaset".
Can someone please help?
Looks like there is a problem in your server(s). You need to get on the server(s) and run rs.status() to find out the state of your replica set, and each server's logs to find out if and why one or more has stopped or become disconnected.

Resources