0

I got a Nodejs project with was hosted in Digital Ocean and I keep it running using Forever script. But I realized that there will be some error after a period of time which causing the site down. It's not just happened once but few times already, below is the Forever log:

Error: Connection lost: The server closed the connection.
    at Protocol.end (/var/www/menu/node_modules/mysql/lib/protocol/Protocol.js:109:13)
    at Socket.<anonymous> (/var/www/menu/node_modules/mysql/lib/Connection.js:109:28)
    at emitNone (events.js:72:20)
    at Socket.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:905:12)
    at nextTickCallbackWith2Args (node.js:441:9)
    at process._tickCallback (node.js:355:17)
error: Forever detected script exited with code: 1
error: Script restart attempt #178
Listening on xxx.xxx.xxx.xxx, server_port 80
events.js:141
      throw er; // Unhandled 'error' event

Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
    --------------------
    at Protocol._enqueue (/var/www/menu/node_modules/mysql/lib/protocol/Protocol.js:141:48)
    at Protocol.handshake (/var/www/menu/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/var/www/menu/node_modules/mysql/lib/Connection.js:130:18)
    at Connection._implyConnect (/var/www/menu/node_modules/mysql/lib/Connection.js:461:10)
    at Connection.query (/var/www/menu/node_modules/mysql/lib/Connection.js:206:8)
    at Object.<anonymous> (/var/www/menu/config/db.js:14:12)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
error: Forever detected script exited with code: 1

I'm not sure why this is happening as the log did not stated which line was causing error, and it seems like Forever had try to restart the script but it failed. But once I SSH into my server and start it again then it's working fine already. Anyone able to help? Thanks.

Ping
  • 339
  • 7
  • 18

2 Answers2

0
Error: connect ECONNREFUSED 127.0.0.1:3306

You are getting this error that means that port is in use. You can kill the process running over that port by using following command.

sudo kill $(sudo lsof -t -i:3306)

Thereafter, you can re-deploy your server using forever like you do.

  • But this error is just occurred after a total 178 times of Forever restart, so this is not the main reason that causing the script not able to restart right? – Ping Jul 27 '17 at 11:46
  • the cause why forever cannot restart is because your are been disconnected by mysql because of timeout and when you (forever) try to reconnect the process is already running – Noel Carcases Jul 27 '17 at 11:47
  • It means that there is something wrong with the server configuration. I would suggest you to check the forever logs to find out the error. Try "forever logs" to list out log files followed by "forever logs (number)" here number is the number of the log file. – Karan Thakkar Jul 27 '17 at 11:48
  • Refer your code. You can look into config/db.js:14:12) – Karan Thakkar Jul 27 '17 at 11:50
0

Happens to me once The problem seems to be related to mysql connection.

Please refer to this stackoverflow question to solve your problem

nodejs mysql Error: Connection lost The server closed the connection

Noel Carcases
  • 711
  • 1
  • 7
  • 27