简体   繁体   English

Node.js和MySQL“连接过多”错误

[英]Node.js and MySQL “Too many connections” error

I'm using Node.js to run a web-server for my web application. 我正在使用Node.js为我的Web应用程序运行Web服务器。 I'm also using the node-mysql module to interface with a MySQL server for all my persistent database needs. 我还使用node-mysql模块与MySQL服务器进行接口,以满足我所有的持久数据库需求。

Whenever there is a critical error within my Node.js application that crashes my app's process I get an email sent to me. 每当我的Node.js应用程序中出现严重错误而使我的应用程序崩溃时,我都会收到一封电子邮件。 So, I keep getting this email with an error saying "Too many connections". 因此,我一直收到此电子邮件,并显示错误消息“连接过多”。 Here's an example of the 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 it tells me is that the error is coming from the mysql module, but it doesn't tell me where in my application code the issue is occurring. 如您所见,它告诉我的是该错误来自mysql模块,但并没有告诉我问题在我的应用程序代码中的何处发生。

My application opens a db connection anytime I need to run one or more queries. 每当我需要运行一个或多个查询时,我的应用程序都会打开一个数据库连接。 I immediately close the connection after all my queries and data has been collected. 收集了所有查询和数据后,我立即关闭了连接。 So, I don't understand how I could be exceeding the 151 max_connections limit. 因此,我不知道如何超过151个max_connections限制。

Unless there is a place in my code where I forgot to call db.end() to close the connection, I don't see how my app would leak like this. 除非代码中没有忘记调用db.end()来关闭连接的地方,否则我不会看到我的应用程序会像这样泄漏。 Even if there was such a mistake, I wouldn't get these emails sent by the dozens. 即使有这样的错误,我也不会收到数十封发送的电子邮件。 Yesterday, I received almost 100 emails with roughly the same error. 昨天,我收到了将近100封电子邮件,并大致相同的错误。 How could this be happening? 这怎么可能发生? If my application had leaked and allocated connections over time, as soon as the first error occurred the app process would crash and all connections would be lost, preventing the app to crash again. 如果我的应用程序随着时间的流逝而泄漏并分配了连接,则第一个错误发生时,应用程序进程将崩溃并且所有连接都将丢失,从而防止了应用程序再次崩溃。 Since I received ~100 emails, this means the app crashed ~100 times, and all within a short period of time. 自从我收到约100封电子邮件以来,这意味着该应用程序在短时间内崩溃了约100次。 This could only mean that somewhere in my application a lot of connections where established in a short period of time, right? 这仅意味着我的应用程序中的某个地方在短时间内建立了许多连接,对吗?

How could I avoid this problem? 我如何避免这个问题? This is very discouraging. 这非常令人沮丧。 All help is highly appreciated. 非常感谢所有帮助。 Thanks 谢谢

MySQL has a default MAX_CONNECTIONS = '100' not 151 unless you changed it. MySQL的默认MAX_CONNECTIONS ='100'而不是151,除非您对其进行了更改。 Also, in truth you have MAX_CONNECTIONS + 1. The plus 1 allows a root user to logon even after you have maxed out the conenctions in order to figure out what is actually being used. 同样,实际上,您拥有MAX_CONNECTIONS +1。加号1允许root用户登录,即使您已最大化连接数量以弄清实际使用的是什么。 When your connections are maxed out try logging on as root and running the following command from MySQL. 当连接用尽时,请尝试以root身份登录并从MySQL运行以下命令。

mysql> SHOW FULL PROCESSLIST

Post the output of this command above. 在上方发布此命令的输出。 Once you actually know what is consuming your resources you can go about fixing it.It could easily be your code that is leaving open connections. 一旦真正了解正在消耗资源的资源,就可以对其进行修复,这很可能是您的代码留下了开放的连接。

You should take a look at the follwoing documentation: Show Processlist 您应该查看以下文档: 显示流程列表

+1 for question. +1问题。 Investigations showed us that node-mysql opens the connections and doesn't close them. 调查显示,node-mysql打开了连接并且没有关闭连接。 Because of that at one moment be reach the limit of max connections. 因此,一时达到最大连接数的限制。 The question is why node-mysql doesn't close the connections? 问题是为什么node-mysql无法关闭连接?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM