简体   繁体   English

带有mysql的NodeJS服务器挂起

[英]NodeJS Server with mysql hangs

I am writing an application using NodeJS, Express, mysql, so far everything works fine, but when I run my application after sometime when mysql connection is interrupted my application throughs this exception and my application goes down. 我正在使用NodeJS,Express,mysql编写应用程序,到目前为止一切正常,但是当我运行我的应用程序之后,当mysql连接中断时,我的应用程序通过此异常并且我的应用程序关闭。

Error: read ECONNRESET
    at errnoException (net.js:901:11)
    at TCP.onread (net.js:556:19)

From another stackquestion i came to know that i have to handle such uncaught exceptions like this. 从另一个堆栈问题我知道我必须处理这样的未被捕获的异常。

process.on('uncaughtException', function(err) {
    console.log('Caught exception: ' + err);
    console.log(err.stack);
});

after this now my application does not exit, but instead it hangs up, so my question is how do I handle this exception so that mysql connection is ok even after this exception and my application does not hang up. 在此之后,我的应用程序不会退出,而是它挂起,所以我的问题是我如何处理此异常,以便即使在此异常后我的应用程序没有挂断,mysql连接也没问题。

I'm not sure if you're using the node-mysql module for your project, but I was, and I encountered the same ECONNRESET issue. 我不确定你是否正在为你的项目使用node-mysql模块,但我当时遇到了同样的ECONNRESET问题。 Here's a repeat of my answer on my issue : 以下是我对我的问题的回答:

I reached out to the node-mysql folks on their Github page and got some firm answers. 我在他们的Github页面上找到了node-mysql的人,得到了一些坚定的答案。

  1. MySQL does indeed prune idle connections. MySQL确实修剪了空闲连接。 There's a MySQL variable "wait_timeout" that sets the number of second before timeout and the default is 8 hours. 有一个MySQL变量“wait_timeout”,用于设置超时前的秒数,默认为8小时。 We can set the default to be much larger than that. 我们可以将默认值设置为远大于此值。 Use show variables like 'wait_timeout'; 使用show variables like 'wait_timeout'; to view your timeout setting and set wait_timeout=28800; 查看超时设置并set wait_timeout=28800; to change it. 改变它。

  2. According to this issue , node-mysql doesn't prune pool connections after these sorts of disconnections. 根据此问题 ,node-mysql在这些断开连接后不会修剪池连接。 The module developers recommended using a heartbeat to keep the connection alive such as calling SELECT 1; 模块开发人员建议使用心跳来保持连接活动,例如调用SELECT 1; on an interval. 在一个间隔。 They also recommended using the node-pool module and its idleTimeoutMillis option to automatically prune idle connections. 他们还建议使用node-pool模块及其idleTimeoutMillis选项自动修剪空闲连接。

I found the solution and I am posting if someone else is facing the same problem this might help you guys as well. 我找到了解决方案,如果其他人面临同样的问题,我也会发布这个问题。

First I caught all uncaught exceptions, which made my application not to exit abnormally. 首先,我捕获了所有未被捕获的异常,这使得我的应用程序不会异常退出。

Second the problem of hanging I had was because when server would close the connection, all my requested queries would fail and my server would simply hang up I guess its node-mysql bug, but I solved it using connecting pooling, in pooling if server close the connection its re-aquired after a second or so again so my problem was solved this way. 第二个悬挂的问题是因为当服务器关闭连接时,我所请求的所有查询都会失败,我的服务器只会挂起我猜它的node-mysql错误,但我解决了它使用连接池,在池中如果服务器关闭一段时间之后重新获得的连接,所以我的问题就这样解决了。

Here is how to make most out of node-mysql pooling 以下是如何最大限度地利用node-mysql池

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM