MongoDB Error: connect ECONNREFUSED 35.156.235.216:27017 - node.js

trying to connect mongoose to my express app and getting this error:
[nodemon] starting `node index.js`
Listening to the port 5000
connect ECONNREFUSED 35.156.235.216:27017
Im connecting the app directly to MongoDB Atlas, everything was working fine yesterday but this morning i got this Error. Here is the db connection file:
const mongoose = require("mongoose");
require("dotenv").config();
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
console.log("connected to MongoDB..");
} catch (err) {
console.log(err.message);
}
};

Related

i got a problem when im connecting to mongodb

Terminal
[nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
(node:16036) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning.
(Use `node --trace-deprecation ...` to show where the warning was created)
server is running on 5000
db error
this is index.js
const express = require('express');
const app = express();
var mongoUrl ="mongodb://localhost:27017/TestDB"
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/TestDB", { useNewUrlParser: true, useUnifiedTopology: true }, (err) => {
if (!err) console.log('db connected');
else console.log('db error');
})
app.listen(5000, () => {
console.log('server is running on 5000');
});
mongoose
.connect(`mongodb+srv://DB_USER:DB_PASS#cluster0.mbv6h.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('Database connection successful!');
}).catch(error){
console.log(error)
}
you can get the entire connection string from MongoDB and input your DB user and password
It seems that you are trying to connect to a local instance of MongoDB. I remember this caused me a few headaches.
Try making sure that you have it downloaded and running:
Open command prompt (on Windows) or terminal (on MacOS)
Type mongod and click enter
If you get a long response then you have it installed but it might not be running or running on the correct port.
If you get a response something like this: 'mongod' is not recognized as an internal or external command, operable program or batch file. then that means you don't have MongoDB installed https://www.mongodb.com/docs/manual/administration/install-community/ to find instructions on how to install it.
Also...
The code you are using would work but it's a little untidy.
Try:
const express = require("express")
const mongoose = require("mongoose")
const app = express()
// Define port and mongodb url once so you don't have to repeat yourself
const PORT = 3000
const MONGO = "mongodb://localhost:27017/TestDB"
// Add this line to get rid of the first error you got
mongoose.set("strictQuery", false)
mongoose
.connect(MONGO, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Database connected!"))
.catch((err) => console.log(err))
app.listen(PORT, () => {
console.log("Server is running on port " + PORT)
})

i am getting connection error on prod not localhost

I am trying to connect to cloud mongo db using mongoose with the connection string
const mongoose = require("mongoose");
mongoose.connect(
"mongodb+srv://<user>:<pass>#<cluster>/<db>?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(res => {
console.log("successful" + res);
})
.catch(err => {
console.log("connection error:", err);
})
module.exports = mongoose;
This works fine on my local machine but when I upload and run it on my production server it doesn't connect.
I have to set it to allow connections from all IPs and I have also add my server IP to it but still it shows me the same error

Unable to establish a mongodb atlas connection

I've tried connecting a MongoDB cloud atlas using URL in my Node application,but getting the following error:-
UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0-coypu.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19)
Ive connected using:-
mongoose.connect(process.env.MONGODB_URI || config.connectionString, { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true });
mongoose.Promise = global.Promise;
In order to check whether the connection is established or not ive donw using:-
mongoose.connect('config.connectionString',{
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
}).then(
() => {
console.log("Database connection established!");
},
err => {
console.log("Error connecting Database instance due to: ", err);
}
);
where config.connectionString contains the URL generated in my atlas.
mongoose
.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
log.info("successfully connected to db");
})
.catch((err) => {
log.error("db connection failed " + err);
});
Just make sure the URI is in correct format

Problem connecting to MongoDB Atlas cluster with mongoose (NodeJS)

I've been struggling to connect to my mongodb atlas cluster through mongoose. I'm still fairly new to nodejs, but after searching around all I could find was to set the flag to
useNewUrlParser: true and my own compiler threw a warning for me to add useUnifiedTopology: true. However it keeps getting caught as an error. Thanks for any advice or direction
Update: when outputting err, i get
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
going to dig into this now
//THIS IS PART OF .ENV
ATLAS_URI=mongodb+srv://myusername:mypw#cluster(:idhere).mongodb.net/test?retryWrites=true&w=majority
//THIS IS PART OF SERVER.JS
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config();
const app = express();
const uri = process.env.ATLAS_URI;
// console.log('URI = "' + uri + '"'); //outputs correct URI
//without these two flags, get deprecated warning
mongoose.connect( uri, { useNewUrlParser: true, useUnifiedTopology: true })
.catch(err => {
console.log('URI error'); //still goes into here
});

Node.js and MongoDB Atlas Mongoose Connection Error

I am trying to connect a MongoDB atlas instance to a nodejs/express server :
const mongoose = require("mongoose");
const dbURI =
"mongodb+srv://(url copied from atlas connect)";
const options = {
useNewUrlParser: true,
dbName: "data"
};
mongoose.connect(dbURI, options).then(
() => {
console.log("Database connection established!");
},
err => {
console.log("Error connecting Database instance due to: ", err);
}
);
But I keep get the following error:
MongoNetworkError: connection 5 to cluster ... 27017 closes at TLSSocket. ...
How can I fix this?
Resolved -- Check IP whitelist!

Resources