Can't connect to mongoose Error: MongoError: connect ETIMEDOUT? - node.js

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.

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.

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 `

Can't connect to mongo lab on arduino yun

I can't connect to a database with mongodb or mongoose on nodejs on my arduino yun.
Unable to connect to the mongoDB server. Error: { [MongoError: Authentication failed.]
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
code: 18,
errmsg: 'Authentication failed.' }
I get that error. The code works fine on my desktop computer.
//lets require/import the mongodb native drivers.
var mongodb = require('mongodb');
//We need to work with "MongoClient" interface in order to connect to a mongodb server.
var MongoClient = mongodb.MongoClient;
// Connection URL. This is where your mongodb server is running.
var url = 'mongodb://*******:********#address:23118/arduino';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
//HURRAY!! We are connected. :)
console.log('Connection established to', url);
// do some work here with the database.
//Close connection
db.close();
}
});
I am trying to connect to a mongo lab database. I have not tried connecting to any other database to troubleshoot. This is because I cannot find a free trial version where I do not need a credit card.
EDIT:
I got an Object Rocket instance and it works perfectly on my arduino yun. However it is very expensive; $30 a month (currently on the trial). And I don't need speed or lots of data. So if anyone could figure out why it doesnt work on mlab that would be great.
I've experienced some authentication issues in the past and I've addressed them following the next steps:
1) Identify version numbers of both mongo server and mongoose
2) Identify what authentication mechanism does your server use and make sure that mongoose is trying to authenticate with the one your server is expecting. If you're using an old version, some authentication mechanism might not be supported, so you'd need to upgrade.
3) Identify where the user has been created. Check out Authentication Database and make sure that your client is authenticating using such database. It can be different than the database you store your data. Indeed, it uses to be admin. If so, take a look to the authSource option you can add to your connection string. More info here.
In addition to all of the above, I'd recommend you to use the the mongo client to verify your credentials are right and you can connect to the database.

PostgreSQL not working with Node.js on Heroku

I'm trying to work on a node.js application using Heroku and a PostgreSQL database.
I have followed the tutorial on Heroku documentation:
https://devcenter.heroku.com/articles/nodejs#using-a-postgres-database
Dependencies are good, and my code is basically the following:
var pg = require('pg');
pg.connect(process.env.DATABASE_URL, function(err, client) {
var query = client.query('CREATE TABLE users (id bigint auto_increment not null, login varchar(250), constraint pk_users primary key (id) )');
query.on('row', function(row) {
console.log(JSON.stringify(row));
});
});
I have tried various forms of this query, like this one:
var client = new pg.Client(process.env.DATABASE_URL);
client.connect();
But I got each time this error in my heroku logs:
at Object.afterConnect [as oncomplete] (net.js:875:19)
Error: connect ECONNREFUSED
at errnoException (net.js:884:11)
If someone has already encountered this kind of problem, any help would be welcome.
Thank you.
I would suggest trying a console.log(process.env.DATABASE_URL); and then using that address to manually connect using a client on your local machine. I understand that the Heroku database servers are open for remote connection(but I have been wrong before).
Failing that you can get a heroku console and use the postgre client.
ECONNREFUSED is a socket error that pops up when you try to connect to an address that is not contactable/connectable/listening.
If process.env.DATABASE_URL does return the correct and connectible address then I would be doing console.log(pg); to ensure that object is what I would expect it to be.

Resources