what is mongoose connection validity time? - mongodb

I have custom circuit breaker logic for 3rd party services. I checked mongoose connection status like below,
mongoose.connection.on("connected", function() {
console.log("Mongoose connected");
});
mongoose.connection.on("disconnected", function() {
console.log("Mongoose disconnected");
});
while running on server logs like below,
Mongoose disconnected //Happened 12.30PM
// after sometime
Mongoose connected //Happened 12.35PM
Mongoose disconnected //Happened 12.35PM
Mongoose connected //Happened 12.35PM
My connection string,
mongoose.connect(URL, {
keepAlive: true,
keepAliveInitialDelay: 300000,
socketTimeoutMS: 300000,
poolSize: 10,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
})
I dont know while connect why system act like above in same second of time interval ?
Why mongo connection goes to disconneted mode even i apply "keepAlive" ?
Is there any validity for mongoose connection alive status?

Related

MongooseServerSelectionError: In Cpanel , Simple Routes are working But whenever try to Hit route of (MongoDb get/set) its Not Working. Mongodb error

Hy I have React NodeJs App, Trying to Deploy it On Cpanel Namecheap...
The site is Deploy Correctly. And Simple Routes are also Working but Routes that use to get/set data from DB is Note Working and stay in a loading state for long time...
I Also See my stderr.log file it says "MongooseServerSelectionError"...
But my MongoDB NetworkAccess is a enter image description herepublic already!!!!
Please Help..
here is my connection Code start.....
const mongoose = require("mongoose");
const connectDb = async () => {
mongoose.connect(
"mongodb://serverBoiler:myPassword#cluster0-shard-00-00.t30x6.mongodb.net:27017,cluster0-shard-00-01.t30x6.mongodb.net:27017,cluster0-shard-00-02.t30x6.mongodb.net:27017/RagdollCatServer?ssl=true&replicaSet=atlas-1r1rhb-shard-0&authSource=admin&retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
}
)
.then((res) => console.log(`Db connected on ${res.connection.user}`));
};
module.exports = connectDb;
////////////////////////////////////////////////////////////////////////
You better try with localhost like this
it may help full I think.
var db="mongodb://localhost/loginapp"
mongoose.connect(db,{useNewUrlParser: true})
.then(()=>console.log('MongoDB successfully connected'))
.catch(err=>console.log(err));

Unable to connect to Mongodb Atlas using mongoose

I am trying to connect to a cluster created in Mongodb Atlas using mongoose in node js and I am facing below issues when doing so.
When I use the connection string that is given in the Mongo db atlasmongodb+srv://lm_dev_app:<password>#lmdev-q5biw.mongodb.net/test?retryWrites=true&w=majorityI get below error
{ Error: queryTxt EBADNAME lmdev-q5biw.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:196:19)
errno: 'EBADNAME',
code: 'EBADNAME',
syscall: 'queryTxt',
hostname: 'lmdev-q5biw.mongodb.net'}
I cannot use this connection string in Mongodb Compass as well as I am getting the same error there.
If I try to connect using mongodb://lm_dev_app:<password>#lmdev-shard-00-01-q5biw.mongodb.net/test i get below error
MongooseServerSelectionError: connection to 54.66.221.230:27017 closed
However I am able to connect to each node using Mongodb Compass which eliminates the possibility of my ipaddress not being whitelisted.
Here is the sample code that I am using
const mongoosePromise = mongoose.connect("mongodb://lm_dev_app:<password>#lmdev-shard-00-01-q5biw.mongodb.net/test", {
useNewUrlParser: true,
useUnifiedTopology: true,
replicaSet: "LMDEV"
}, (err) => {
if (err) {
console.log(err);
} else {
console.log("Successful");
}
});
Any thoughts on what is happening here.
There are couple of things that I need to highlight here.
The default connection string that is shown in Mongodb Atlas seems to be wrong. It shows you mongodb+srv://<username>:<password>#<cluster_url>/test?retryWrites=true&w=majority. But I used mongodb://<username>:<password>#<node_url>:27017/ to make it work. You can also use mongodb://<username>:<password>#<node_url>:27017/admin.
Pass ssl:true in the options that we are passing.
Finally one of the 3 options can be used to connect to the database.
a. const mongoosePromise = mongoose.connect("mongodb://lm_dev_app:<password>#lmdev-shard-00-01-q5biw.mongodb.net:27017/", {
useNewUrlParser: true,
useUnifiedTopology: true,
authSource:"admin",
ssl: true,
}, (err) => {
if (err) {
console.log(err);
} else {
console.log("Successful");
}
});
b. const mongoosePromise = mongoose.connect("mongodb://lm_dev_app:<password>#lmdev-shard-00-01-q5biw.mongodb.net:27017/", {
useNewUrlParser: true,
useUnifiedTopology: true,
authSource:"admin",
ssl: true,
}, (err) => {
if (err) {
console.log(err);
} else {
console.log("Successful");
}
});
c. const mongoosePromise = mongoose.connect("mongodb://lm_dev_app:<password>#lmdev-shard-00-01-q5biw.mongodb.net:27017/admin", {
useNewUrlParser: true,
useUnifiedTopology: true,
ssl: true,
}, (err) => {
if (err) {
console.log(err);
} else {
console.log("Successful");
}
});
EDIT 1:
After having a chat with Atlas support team I was told that issue in point 1 is due to DNS resolution issue with my service provider. So i have changed my DNS settings to point to a public DNS server.
After trying different connection strings for several hours, I finally just copy/pasted the connection string from MongoDB Compass and it works! (first connect, then edit the connection string as it will change)
It looks like this:
mongodb+srv://username:password#fra-atlas-shard.abcde.mongodb.net/test?authSource=admin&replicaSet=atlas-abcde-shard-0&readPreference=primary&appname=MongoDB%20Compass&ssl=true

Connecting to the mongodb

I'm new in the MEAN developing, I'm developing a simple app, and for my first step I'm trying to connect to my mongodb, so I installed node, express, morgan,mongodb, mongoose.
So here is my code in index.js:
const express = require('express');
const morgan = require('morgan');
const app = express();
const { MongoClient } = require('./database');
// Settings
app.set('port', process.env.PORT || 3000);
// Middlewares
app.use(morgan('dev'));
app.use(express.json());
// Routes
// Starting the server
app.listen(app.get('port'), () => {
console.log('server on port', app.get('port'));
});
and then the code on my database.js:
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://duke:<password>#cluster0-dreyi.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
console.log("horrorrrrrr");
// perform actions on the collection object
client.close();
});
module.exports = MongoClient;
I also try this code that is on the mongodb page to connect to the application:
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://duke:<password>#cluster0-dreyi.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
Of course I change the password to the real one. Please keep in my today it's my first time I touch mongodb and also the MEAN full stack, and I spent too many hours stuck in this connection.
this is the error I get:
(node:5284) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
EDIT
#iLiA thanks for your reply ! I tried your code and ain't working, I will show you how I did it with the real password :
const url = 'mongodb+srv://duke:password#cluster0-dreyi.mongodb.net/test?retryWrites=true&w=majority';
const mongoose = require('mongoose');
mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(()=>{
console.log('congrats, problem solved')
})
.catch((err)=>{
console.log(`there is not a problem with ${err.message}`);
process.exit(-1)
})
module.exports = mongoose;
and the error is :
there is not a problem with Server selection timed out after 30000 ms
[nodemon] app crashed - waiting for file changes before starting...
Kind regards,
I am confused about why do you downloaded both mongodb and mongoose but here is mongoose solution
const mongoose = require('mongoose');
mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(()=>{
console.log('congrats, problem solved')
})
.catch((err)=>{
console.log(`there is a problem with ${err.message}`);
process.exit(-1)
})
EDIT:
As it seems you forgot to whitelist your IP address in mongo atlas.

Moongose ready state and aws lambda - cache connection

I am trying to implement a connect to database function that uses mongoose (MongoDB), and AWS lambda functions. Best practice, as I understand it, is to cache the database so that the connection can be reused. This, I have working. However, the problem is that the mongoose.readyState variable uses some fixed amount of time to determine if the connection is valid or not.
I was wondering if there is an alternative to .readyState to check if the connection is valid. I know that I can try to run a random query against the database every time I want to reuse the connection. Thus, I could determine if an exception happens or not. However, this feels a little hackish.
Here is my connect to database function:
let cacheDb = null
let options = {
useMongoClient: true,
autoIndex: true,
autoReconnect: true,
keepAlive: true,
socketOptions: {
keepAlive: true,
autoReconnect: true,
connectTimeoutMS: 30000
},
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 200,
poolSize: 1,
bufferMaxEntries: 0,
};
mongoose.Promise = global.Promise
module.exports = {
connectToDatabase: function connectToDatabase(context) {
context.callbackWaitsForEmptyEventLoop = false
if (cacheDb && mongoose.connection.readyState == 1) {
return cacheDb
}
else {
mongoose.connect(process.env['MONGODB_URI'], options, function(error){
if(error){
console.log(error.toString())
}
cacheDb = mongoose.connection
return cacheDb
})
}
}
}
Do you have any idea as to an alternative approach?
In order to check for a valid connection , you should be checking your cached db serverConfig.isConnected method.
like so:
if (cachedDb && cachedDb.serverConfig.isConnected()) { ...
This is according to mongodb, in this helpful article:
https://www.mongodb.com/blog/post/optimizing-aws-lambda-performance-with-mongodb-atlas-and-nodejs

Mongo w/ Mongoose on Express connection closes

My connection to my mongo database will close or timeout if left in active. I'm getting the following error when I leave my app in active "no open connections"
My mongo DB is running in a replication set up on AWS. I'm using the following options when connecting using mongoose. I'm unsure if any other flags should be set. I was basing my options on teh monodo node driver doc # http://mongodb.github.io/node-mongodb-native/api-generated/server.html.Users will be in my applicaiton for 8+ hours at a time and I don't want anything to timout when they go to lunch or leave for a meeting.
MongoOptions : {
user: 'root',
pass: '********',
replset: {
auto_reconnect: true,
poolSize: 25,
socketOptions: { keepAlive: 1 },
ssl: true,
sslCert: fs.readFileSync('./server/config/ssl/mongodb-cert.crt'),
sslKey: fs.readFileSync('./server/config/ssl/mongodb-cert.key')}
}
mongoose.connect('mongodb://server.com:27017', config.MongoOptions);
The connection getting closed turned out to be another issue that was causing the problem.
We have a replicate set running w/ SSL and are able to keep the connection up for days using the following config:
MongoOptions : {
user: 'ssssss',
pass: 'xxxxxx',
replset: {
auto_reconnect: false,
poolSize: 10,
socketOptions: { keepAlive: 1 },
ssl: true,
sslCert: fs.readFileSync('./server/config/ssl/mongodb-cert.crt'),
sslKey: fs.readFileSync('./server/config/ssl/mongodb-cert.key')}
}
Try adding
connectTimeout: 43200000
that should be good for 12 hours