NodeJS server closes connection after idling for a few hours - node.js

This question has been re-asked at past years, however solutions are either using deprecated methods or are not clear.
NodeJS server (running on CentOS) after being idle for some hours, usually at night, closes its connection and shuts down probably because it loses its connection with MySQL.
Error: Connection lost: The server closed the connection.
at Protocol.end (/home/s/prcrm/node_modules/mysql/lib/protocol/Protocol.js:109:13)
at Socket.<anonymous> (/home/s/prcrm/node_modules/mysql/lib/Connection.js:115:28)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
DB Configuration
function DB( host, user, password, database ) {
this.host = host;
this.user = user;
this.password = password;
this.database = database;
this.connection = mysql.createConnection({
host: this.host,
user: this.user,
password: this.password,
database: this.database
});
this.connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
Versions
Node.js: 6.4.0
Express: 4.13.4
MySQL: 10.0.29-MariaDB MariaDB Server
CentOS: 6.8 (Final)
Any suggestions or guidance of how to keep MySQL connection alive (without being quick&dirty) when server is idling, supposing that this causes the problem?

Related

PostGres Timeout error in AWS EC2 Instance (Production)

I'm newbie to the backend below it's my configuration for Postgres
const pool = new Pool({
user: "username",
host: "hostname",
database: "dbname",
password: "postgres",
port: 5432,
max: 10,
min: 10,
statement_timeout: 10000,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
maxUses: 7500,
});
console.log("requesting db");
pool.connect((err, client, release) => {
console.error("Error acquiring client", err);
console.error("Error acquiring client", client);
console.error("Error acquiring client", release);
if (err) {
return console.error("Error acquiring client", err.stack);
}
});
pool.on("connect", () => {
console.log("connected to the db");
});
pool.on("error", function (err, client) {
console.log(client);
console.log(err);
});
module.exports = pool;
In production, I'm facing this below error but it works in local I tried connecting my prod DB in my local machine its working fine
Error acquiring client Error: Connection terminated due to connection timeout
at Connection.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/client.js:255:9)
at Object.onceWrapper (events.js:421:28)
at Connection.emit (events.js:315:20)
at Socket.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/connection.js:78:10)
at Socket.emit (events.js:315:20)
at emitCloseNT (net.js:1656:8)
at processTicksAndRejections (internal/process/task_queues.js:83:21)
at runNextTicks (internal/process/task_queues.js:66:3)
at listOnTimeout (internal/timers.js:518:9)
at processTimers (internal/timers.js:492:7)
Error acquiring client undefined
Error acquiring client [Function: NOOP]
Error acquiring client Error: Connection terminated due to connection timeout
at Connection.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/client.js:255:9)
at Object.onceWrapper (events.js:421:28)
at Connection.emit (events.js:315:20)
at Socket.<anonymous> (/home/ubuntu/TapToCookBackEnd/node_modules/pg/lib/connection.js:78:10)
at Socket.emit (events.js:315:20)
at emitCloseNT (net.js:1656:8)
at processTicksAndRejections (internal/process/task_queues.js:83:21)
at runNextTicks (internal/process/task_queues.js:66:3)
at listOnTimeout (internal/timers.js:518:9)
at processTimers (internal/timers.js:492:7)
Below route are working fine in production but connection is not established to postgres
app.get("/api/v1/test", function (req, res) {
res.send("Hello World test!");
});
My EC2 Configuration
Inbound rule
OutBound rule
My RDS Configuration
Inbound rule
OutBound rule
I suspect the RDS instance does not have a security group that allows connections from your EC2 instance. See this help article: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html
If your RDS instance has a public IP address, then the domain name will resolve to that. If both your EC2 instance and RDS instance are hosted in the same VPC, then I recommend disabling the public IP address. This may make it harder to connect to the database from your personal computer, but you can use an SSH tunnel to accomplish in this case.
When setting up RDS for public access, it is not enough to open the ports in the Security Group.
You also need to select yes for "Publicly accessible", which is hidden inside the "Additional connectivity configuration" section (which is closed by default, so easy to overlook).

Establish a connection between the raspberry and mongodb

I created a database on my pc and I wanted to create a javascript script using node, which would allow me to access all the database data installed on my pc. I tried to establish a connection between raspberry and my pc locally using the same router. Unfortunately, it always gives me connection errors.
Looking a bit on the internet I found many posts about "bindIp" of mongodb, but despite several tests found the same problem.
network interfaces
net:
   port: 27017
   #bindIp: 127.0.0.1,192.168.1.8
sorry for my English.
Code raspberry:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://168.192.1.6:27017/"; //ip my pc and mongodb
var day = new Date();
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("qadrio");
dbo.collection("pin").findOne({}, function(err, result) {
if (err) throw err;
console.log("NAME DEVICE: "+ result.name);
console.log("ERROR: "+ err);
db.close();
});
});
I'm not an expert so I ask for help from you users. I would like to understand how it is done. I think the problem is mongodb itself that blocks external connections except the local ones of the pc.
Error Message
failed to connect to server [168.192.1.6:27017] on first connect [MongoNetworkError: connection 0 to 168.192.1.6:27017 timed out] at Pool. at Pool.emit (events.js:182:13) at Connection. at Object.onceWrapper (events.js:273:13) at Connection.emit (events.js:182:13) at Socket. at Object.onceWrapper (events.js:273:13) at Socket.emit (events.js:182:13) at Socket._onTimeout (net.js:449:8) at ontimeout (timers.js:425:11)

node express-myconnection mysql pool : connection closed by server

I have a node server that create a connection pool to mysql, if there has been no activity for a period of time the connection is closed by server and and further access throws an error. Not sure where to go with this, have researched a number of forums with little luck.
var mysql = require('mysql');
var connection = require('express-myconnection');
// DB connection
var dbOptions = {
host: 'localhost',
user: 'admin',
password: 'admin',
port: 3306,
database: 'data',
multipleStatements: true,
connectionLimit: 20
};
app.use(connection(mysql, dbOptions, pool));
Error
Whoops there is an uncaught error Error: Connection lost: The server closed the connection.
at Protocol.end (/opt/www/CMI_intern_2/node_modules/mysql/lib/protocol/Protocol.js:109:13)
at Socket.<anonymous> (/opt/www/CMI_intern_2/node_modules/mysql/lib/Connection.js:109:28)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:921:12)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
Connection pool works fine, there was a hidden database connection in some old code that was the root cause. Non issue.
actually, if someone experiences those errors while calling localhost adding database option
socketPath: '/var/run/mysqld/mysqld.sock'
solve this problem

MongoDB mLab mongoose Node.js driver - connection timeout after a period of idle time?

I have a simple Node.js that uses mongoose to connect with Mongo database hosted on mLab.
Everything seems working just fine: adding new records, querying for existing stuff.
Only sometimes, after some period of inactivity, when I look at the console I see the following:
events.js:160
throw er; // Unhandled 'error' event
^
Error: connection timeout
at Db.<anonymous> (___PATH___/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:168:17)
at emitTwo (events.js:106:13)
at Db.emit (events.js:191:7)
at Server.listener (___PATH___/node_modules/mongodb/lib/db.js:1786:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Server.<anonymous> (___PATH___/node_modules/mongodb/lib/server.js:274:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Pool.<anonymous> (___PATH___/node_modules/mongodb-core/lib/topologies/server.js:334:12)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (___PATH___/node_modules/mongodb-core/lib/connection/pool.js:270:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
Right now it doesn't matter that much to me - I can always restart the app. I'm worried that in production it will cause a lot of headaches so I preemptively ask what's the issue here?
Note that initially everything is working fine, it after some time when I get Error: connection timeout
Sample Connection code for Mongo-Node using Mongoose
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
const options = {
useMongoClient: true,
reconnectTries: 5000,
reconnectInterval: 0,
socketTimeoutMS: 100000,
connectTimeoutMS: 100000
}
mongoose.connect(mongodb://........, options);
var db = mongoose.connection;
db.on('error', function (error) {
console.log('Error while connecting to mongodb database:', error);
});
db.once('open', function () {
console.log('Successfully connected to mongodb database');
});
db.on('disconnected', function () {
//Reconnect on timeout
mongoose.connect(mongodb://........, options);
db = mongoose.connection;
});
it seems that there is fluctuation in your internet connection. Maybe this is the reason of connection timeout.
you can handle it by setting the timeout.
As below :
var timeout = require('connect-timeout');
app.use(timeout('5s'));`
in your app.js file
Replace your mongoose connection setting by this code:
var mongoose = require('mongoose');
/*
* Mongoose by default sets the auto_reconnect option to true.
* We recommend setting socket options at both the server and replica set level.
* We recommend a 30 second connection timeout because it allows for
* plenty of time in most operating environments.
*/
var options = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };
var mongodbUri = 'mongodb://user:pass#host:port/db';
mongoose.connect(mongodbUri, options);
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function() {
// Wait for the database connection to establish, then start the app.
});

MongoError: connection 0 to localhost:27017 timed out

events.js:141
throw er; // Unhandled 'error' event
MongoError: connection 0 to localhost:27017 timed out
at Function.MongoError.create (/home/ubuntu/scripts/node_modules/mongodb-core/lib/error.js:29:11)
at Socket.<anonymous> (/home/ubuntu/scripts/node_modules/mongodb-core/lib/connection/connection.js:184:20)
at Socket.g (events.js:260:16)
at emitNone (events.js:67:13)
at Socket.emit (events.js:166:7)
at Socket._onTimeout (net.js:318:8)
at _runOnTimeout (timers.js:524:11)
at _makeTimerTimeout (timers.js:515:3)
at Timer.unrefTimeout (timers.js:584:5)
Well there is no error during connection, but when try to save some models/collections it runs for a while and then it throws this error.
BTW I also have another node process connected to the same mongodb server. Any help is highly appreciated.
const mongoose = require('mongoose');
const option = {
socketTimeoutMS: 30000,
keepAlive: true,
reconnectTries: 30000
};
const mongoURI = process.env.MONGODB_URI;
mongoose.connect(mongoURI, option).then(function(){
//connected successfully
}, function(err) {
//err handle
});
Your query is taking a long time. And mongo itself has a default time out set. So it times out, if the query takes longer than the timeout time.
you have to use this configs on your connection:
keepAlive: 300000, connectTimeoutMS: 30000
If you are sure that you exported port -p 27017:2017 and it still doesn't work.
Check your VPN if it is not blocking Local Network sharing.

Resources