1

I'm trying to connect to a MySQL database with Node.JS and at first, it works. Then, kinda at random, it gives the following error:

Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object._errnoException (util.js:1031:13)
    at _exceptionWithHostPort (util.js:1052:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1195:14)
    --------------------
    at Protocol._enqueue (/root/Geola/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Protocol.handshake (/root/Geola/node_modules/mysql/lib/protocol/Protocol.js:52:23)
    at Connection.connect (/root/Geola/node_modules/mysql/lib/Connection.js:130:18)
    at Object.<anonymous> (/root/Geola/runMain.js:24:12)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:73:21)

I double-checked that the port is correct by using the command netstat -tlnp and got back tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 25362/mysqld

That seems to be correct so I restart the Node.JS app and everything works again... until again, at random, the error pops up again. I tested if it errors only when I try to interact with the database and I found that it does indeed spit out this error at random. Sometimes I can interact with the DB just fine and sometimes I can't. I find this error quite odd so if anyone knows the issue, please do let me know. Thanks!

APixel Visuals
  • 1,508
  • 4
  • 20
  • 38
  • `mysql> SHOW STATUS LIKE 'UPTIME';`. Run that query now, note the value, then run it again after the next time you hit this error. This is the number of seconds since the MySQL process was last restarted and the number should *never* be smaller unless you restarted the service. If it is, something is causing your MySQL Server daemon to crash and recover. That is the most likely explanation, here. – Michael - sqlbot Nov 26 '17 at 00:35

2 Answers2

0

This is caused if your app cannot connect to your mysql instance. You can catch these errors so they are not thrown by listening to the mysql error event.

connection.on('error', function(err) {
  console.log(err)
})

Even if mysql is running on 3306, it's possible that you have maxed out your connections or it could be a temporary network issue. You can check mysql with show variables like "max_connections", you may need to increase this. Also, make sure your app is only establishing a connection on startup, not within routes or other dynamic areas. However, when I've experienced this, it was usually due to poor network connection between app servers and db servers.

dzm
  • 22,844
  • 47
  • 146
  • 226
  • My app does indeed only establish a connection on startup, however, when I run `connection.query("show variables like 'max_connections'", function (err, result) { console.log(result) })`, nothing gets logged – APixel Visuals Nov 25 '17 at 19:19
-1

Ok I found the answer. It was simply that Digital Ocean, the database hoster, had a limit on how much data could be sent at once

APixel Visuals
  • 1,508
  • 4
  • 20
  • 38