Ayan
Ayan

Reputation: 8896

ECONNREFUSED error in NodeJS + MySQL web app

I have web app running on NodeJS + MySQL. Initially the web app works fine,but all of a sudden the MySQL connection gets refused with following error being thrown:

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.

The configuration in NodeJS for making MySQL connection is as following:

"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

Upvotes: 2

Views: 1662

Answers (2)

Ayan
Ayan

Reputation: 8896

To check what was going wrong,I opened the MySQL log file:

/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.

To resolve the memory issue,this answer can be followed : https://stackoverflow.com/a/32932601/3994271

The possible solutions as discussed in the above link are :

  • Increasing RAM size , or
  • configuring a swapfile

I decided to go with configuring a 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

Upvotes: 1

kawadhiya21
kawadhiya21

Reputation: 2528

You must be not closing the mysql connections while doing the query. If you keep the connections open, it would give up after sometime.

Also there is a bug in node-mysql. You can use mysql pool.enter link description here

Upvotes: 0

Related Questions