0
var express = require('express');
var mysql = require('mysql');
var connection = module.exports = mysql.createConnection({
  host: 'myhost.com',
  port:'3307',
  user: 'username',
  password: 'password',
  database: 'mydatabase'
});

connection.connect(function(err) {
  if (err){
    throw err;
  }
  else
  console.log('You are now connected...');
});

Sometimes when I try to connect to the database I get read ECONNRESET error. Please help. I tried many things, but nothing seems to work.

user1937623
  • 15
  • 1
  • 4

2 Answers2

2

You can find the solution in this answer: https://stackoverflow.com/a/22906189/1790397

  1. MySQL does indeed prune idle connections. There's a MySQL variable "wait_timeout" that sets the number of second before timeout and the default is 8 hours. We can set the default to be much larger than that. Use show variables like 'wait_timeout'; to view your timeout setting and set wait_timeout=28800; to change it.
  2. According to this issue, node-mysql doesn't prune pool connections after these sorts of disconnections. The module developers recommended using a heartbeat to keep the connection alive such as calling SELECT 1; on an interval. They also recommended using the node-pool module and its idleTimeoutMillis option to automatically prune idle connections.
putuyuwono
  • 111
  • 10
0

You can use connection variable .timeout function:

connection.timeout = 0;
double-beep
  • 5,031
  • 17
  • 33
  • 41