MySQL giving “read ECONNRESET” error after idle time on node.js server

烂漫一生 提交于 2019-11-27 15:08:32

I reached out to the node-mysql folks on their Github page and got some firm answers.

  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.

If this happens when establishing a single reused connection, it can be avoided by establishing a connection pool instead.

For example, if you're doing something like this...

var db = require('mysql')
  .createConnection({...})
  .connect(function(err){});

do this instead...

var db = require('mysql')
  .createPool({...});

Does this problem appear to be a disconnection of Node's connection to my MySQL server(s), perhaps due to a connection lifetime limitation?

Yes. The server has closed its end of the connection.

When using connection pools, node-mysql is supposed to gracefully handle disconnections and prune them from the pool. Is it not aware of the disconnect until I make a query, thus making the error unavoidable?

Correct, but it should handle the error internally, not pass it back to you. This appears to be a bug in node-mysql. Report it.

Considering that I see the "read ECONNRESET" error a lot in other StackOverflow posts, should I be looking elsewhere from MySQL to diagnose the problem?

It is either a bug in the node-MySQL connection pool implementation, o else you haven't configured it properly to detect failures.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!