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.

0

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

You should have put this as a comment to the answer. Shouldn't have repeat things.
Cannot do that since I don't have enough reputation.
0

You can use connection variable .timeout function:

connection.timeout = 0;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.