I am using Node.js to run a web server for my web application. I also use the node -mysql module to interact with the MySQL server for all of my constant database needs.
Whenever a critical error occurs in my Node.js application that causes my application process to crash, I receive an email sent to me. So, I keep getting this error message saying "Too many connections." Here is an example error:
Error: Too many connections at Function.Client._packetToUserObject (/apps/x/node_modules/mysql/lib/client.js:394:11) at Client._handlePacket (/apps/x/node_modules/mysql/lib/client.js:307:43) at Parser.EventEmitter.emit (events.js:96:17) at Parser.write.emitPacket (/apps/x/node_modules/mysql/lib/parser.js:71:14) at Parser.write (/apps/x/node_modules/mysql/lib/parser.js:576:7) at Socket.EventEmitter.emit (events.js:96:17) at TCP.onread (net.js:396:14)
As you can see, all this tells me that the error comes from the mysql module, but it does not tell me where the problem occurs in my application code.
My application opens a db connection at any time when I need to run one or more requests. I immediately close the connection after all my requests and data have been collected. So, I do not understand how I can exceed the limit of 151 max_connections.
If there is no place in my code where I forgot to call db.end() to close the connection, I donβt see how my application will flow like that. Even if there was such a mistake, I would not have received these letters sent by dozens. Yesterday I received almost 100 emails with about the same error. How could this happen? If my application has leaked and assigned connections over time, as soon as the first error occurs, the application process will fail and all connections will be lost, which will prevent the application from crashing again. Since I received ~ 100 emails, this means that the application crashed ~ 100 times, and all this in a short period of time. It can only mean that somewhere in my application there are a lot of connections established in a short period of time, right?
How could I avoid this problem? This is very discouraging. All help is appreciated. Thanks
source share