I am trying to connect to AWS MongoDB instances and its not even attempting to connect:
This is the connection URI:
mongodb://<usr>:<pwd>#host:port,host2:port2/db
And this is the code I am using to connect.
var mongoose = require('mongoose');
var dbOptions = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };
mongoose.connect(uri, dbOptions);
I also added logs inside events.. none of the events are firing. If I remove host2:port2 it works. So issue seems to be with the connection string. But connection string looks totally fine.. but still its not working.. Any idea why?
Related
I use knex.js in a node environment to run a web server that makes sql calls. I have a query that takes over 30 seconds to complete, but when it's run through knex, the default timeout seems to be 15 seconds, so I get the following timeout error:
RequestError: Timeout: Request failed to complete in 15000ms
...
How do I change the timeout for mssql queries? The official doc has an example of setting timeout on a specific query with .timeout() but this feature doesn't work with mssql. I've also tried everything in this github issue without any luck. After trying all of that, I have this messy looking connection config:
const connection = require('knex')({
client: 'mssql',
connection: {
host : process.env.NODE_ENV == 'production' ? '172.18.1.66' : 'localhost',
user : secrets.user,
password : secrets.password,
database : 'EdgeView',
dialect: "mssql",
options: {
'enableArithAbort': true,
'requestTimeout': 150000,
'idleTimeoutMillis': 150000
},
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 150000
},
dialectOptions:{
requestTimeout: 300000,
options: {
"requestTimeout": 300000
}
}
}
});
The error did not change, none of these timeout values seems to have had an impact.
The query it's self is just a raw query:
let res = connection.raw(GETSQLSTRING(args));
The answer was simply this:
const connection = require('knex')({
client: 'mssql',
connection: {
host : 'edge-sql',
user : secrets.user,
password : secrets.password,
requestTimeout: 600000,
database : 'EdgeView'
}
});
Thanks to BGPHiJACK for pointing me towards better documentation.
I have mongodb up and running in a docker container. I stop the container and node returns a MongoError. I restart the container and node continues to throw the same MongoError.
I would like for it to reconnect when there was an issue.
const uri: string = this.config.db.uri;
const options = {
useNewUrlParser: true,
useCreateIndex: true,
autoIndex: true,
autoReconnect: true,
},
mongoose.connect(uri, options).then(
() => {
this.log.info("MongoDB Successfully Connected On: " + this.config.db.uri);
},
(err: any) => {
this.log.error("MongoDB Error:", err);
this.log.info("%s MongoDB connection error. Please make sure MongoDB is running.");
throw err;
},
);
How do i setup mongoose to try and auto connect when there is a connection failure to mongodb.
I found my answer, instead of checking error events and reconnecting like others have suggested. There are some options you can set that will handle auto-reconnect.
Here are the set of mongoose options i am now using.
const options = {
useNewUrlParser: true,
useCreateIndex: true,
autoIndex: true,
reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
reconnectInterval: 500, // Reconnect every 500ms
bufferMaxEntries: 0,
connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
}
You can test it works by starting and stoping mongodb in a container and checking your node application.
For furuther information refer to this part of the documentation. https://mongoosejs.com/docs/connections.html#options
basically I see this when I wake up and see my monitor..
so I am pretty certain this happens after some time of inactivity..
I am using mlab as my DaaS.
var connection = mongoose.connect(mongoURL, (error, database) => {
if (error) return console.log(error);
console.log('[Success: connected to mlab database]');
});
this is my connection code..
I also faced similar issue with mlab. Try this:
mongoose.Promise = global.Promise;
var mongoConnectOpts = {
poolSize: 50,
reconnectTries: 10,
reconnectInterval: 500,
socketOptions: {
keepAlive: 300000, connectTimeoutMS: 30000
}
}
mongoose.connect(config.connectionString, { server: mongoConnectOpts, replset: mongoConnectOpts });
Looks like setting a keepAlive might help. mLab's recommended connection settings for mongoose:
https://gist.github.com/mongolab-org/9959376
I have a long running Node.js server task which connects to various MongoDB hosts with
// MongoClient options
// TODO: Unable to verify reconnect options
var clientConnectOptions = {
autoReconnect: true,
auto_reconnect: true,
server: {
sslKey: fs.readFileSync('X509key.pem'),
sslCert: fs.readFileSync('X509cert.pem'),
sslPass: null,
sslValidate: false,
checkServerIdentity: false
},
bufferMaxEntries: 0
}
MongoClient.connect('mongodb://server:port/test?ssl=prefer', clientConnectOptions, function cbDBConnect (err, db) { ... }
If the connection to the MongoDB host interrupts for a short time, my application reconnects as planned. But when the session disconnects for several minutes, the MongoClient does not reconnect any more.
I have looked at various reconnect options, but all attempts failed. How can I code a robust way to reconnect automatically?
right now i am connecting to mongodb like this:
uri = 'mongodb://mongodomain1.com:1234,mongodomain2.com:1234/mydb?replicaSet=replicasetname&readPreference=secondaryPreferred';
opts = {
user: 'myuser',
pass: 'mypass',
server: {
auto_reconnect: true,
socketOptions: {
keepAlive: 120
}
},
replset: {
socketOptions: {
keepAlive: 120
}
}
};
mongoose.connect(uri, opts);
now i would like to bring the readPreference url parameter into the options object to be consitent and have all connection variables in the options.
i am struggling to find documentation on how to define the readPreference in the object.
same thing the replicaSet parameter in the url. is that equivalent to the rs_name
is there anything else i could/should improve in the way i am connecting to mongodb?