简体   繁体   English

NodeJS + MySQL Web应用程序中的ECONNREFUSED错误

[英]ECONNREFUSED error in NodeJS + MySQL web app

I have web app running on NodeJS + MySQL. 我有在NodeJS + MySQL上运行的Web应用程序。 Initially the web app works fine,but all of a sudden the MySQL connection gets refused with following error being thrown: 最初,Web应用程序运行良好,但是突然之间,由于引发以下错误,MySQL连接被拒绝:

ECONNREFUSED 127.0.0.1:3306

Simply restarting the server with pm2 reload solves the issue temporarily.But again after a long span of time,the above error creeps in. 只需使用pm2 reload重新启动服务器即可暂时解决此问题。但是经过很长一段时间后,上述错误再次蔓延。

The configuration in NodeJS for making MySQL connection is as following: NodeJS中用于建立MySQL连接的配置如下:

"sqlconn": {
        "connectionLimit": 10,
        "host": "127.0.0.1",
        "user": "root",
        "password": "XYZ",
        "database": "test",
        "port": 3306,
        "multipleStatements": true
}

Any idea on how to resolve this issue? 关于如何解决此问题的任何想法?

NOTE : I am using a digital ocean droplet with RAM size 512MB 注意 :我正在使用RAM大小为512MB的数字海洋液滴

To check what was going wrong,I opened the MySQL log file: 要检查出了什么问题,我打开了MySQL日志文件:

/var/log/mysql/error.log

The logs read something like following: 日志内容如下:

InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
InnoDB: Cannot allocate memory for the buffer pool
InnoDB: Plugin initialization aborted with error Generic error
Plugin 'InnoDB' init function returned error.
Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
[ERROR] Failed to initialize plugins.

So the reason was that Mysql can't restart because it's out of memory . 因此,原因是Mysql由于内存不足而无法重新启动

To resolve the memory issue,this answer can be followed : https://stackoverflow.com/a/32932601/3994271 要解决内存问题,可以遵循以下答案: https : //stackoverflow.com/a/32932601/3994271

The possible solutions as discussed in the above link are : 上面链接中讨论的可能解决方案是:

  • Increasing RAM size , or 增加RAM大小,或者
  • configuring a swapfile 配置交换文件

I decided to go with configuring a swapfile . 我决定继续配置swapfile

The following link provides details on configuring a swap file: https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 以下链接提供了有关配置交换文件的详细信息: https : //www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04

You must be not closing the mysql connections while doing the query. 执行查询时,您必须不关闭mysql连接。 If you keep the connections open, it would give up after sometime. 如果保持连接打开,则一段时间后将放弃连接。

Also there is a bug in node-mysql. 在node-mysql中也有一个错误。 You can use mysql pool. 您可以使用mysql pool。 enter link description here 在此处输入链接说明

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

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