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?