Node.js and MySQL error "Too many connections"

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

+4
source share
2 answers

MySQL has a default value of MAX_CONNECTIONS = '100' not 151 unless you change it. In addition, in truth, you have MAX_CONNECTIONS + 1. Plus 1 allows the root user to log in even after you have typed the maximum number of cones to find out what is actually being used. When your connections are exceeded, try logging in as root and run the following command from MySQL.

 mysql> SHOW FULL PROCESSLIST 

Send the output of this command above. Once you actually know what your resources are consuming, you can fix it. It could be your code leaving open connections.

You should take a look at the following documentation: Show process list

+2
source

+1 for the question. Studies have shown that node-mysql opens connections and does not close them. Because of this, the limit of maximum connections can be reached at one point. The question is why node-mysql does not close connections?

0
source

Source: https://habr.com/ru/post/1480568/


All Articles