MongoDB network access - node.js

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 `

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

Mongoose is unable to connect with DB running on Kubernetes: ECONNREFUSED 127.0.0.1:27017

I have a MongoDB running in a Kubernetes Pod. The Pod is forwarded to my 27017 Port on localhost. Opening localhost:21017 returns It looks like you are trying to access MongoDB over HTTP on the native driver port.. Additionaly I can connect to my DB via DataGrip.
However, when I try to create a connection with node.js it fails with the said error: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
Checking my running services under Task-Manager -> Services displays no MongoDB. However I assume that this is right since my DB is running under Kubernetes on a Remote Server. Right?
Can someone tell me what I'm doing wrong?
Here's my code:
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/testing')
.catch(error => {
console.log("Error with message: " + error.message)
})
Update:
The above code works, if it's being executed in VSCode. When I try to run it in WebStorm it somehow doesn't. If anyone has made the same experience and knows the cause for this I'd love to know.

trouble connecting with MongoDB Atlas

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?

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.

Can't connect to mongoose Error: MongoError: connect ETIMEDOUT?

I have a running db on mlab, however I cannot connect. This is my code:
mongoose.connect('mongodb://myUsername:myPassword#ds161012.mlab.com:61012/gpbdatabase');
const db = mongoose.connection;
db.on('error', (e) => console.log(e))
.once('open', () => console.log('Successfully connected to database'))
I am sure I am using the right username and password, it's a database user I create on Users tab at mlab. And this is my complete error statement.
{ MongoError: failed to connect to server [ds161012.mlab.com:61012] on
first connect [MongoError: connect ETIMEDOUT 54.78.29.56:61012]
How can I fix it?
I always had these problems and it was down to mLab just being that slow and unreliable. Eventually made one for local use while developing.
ETIMEDOUT may occurred when your database not start.
Make sure your database is started.

Resources