javascript Node-Mysql 抛出连接超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32715273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:39:29  来源:igfitidea点击:

Node-Mysql throwing connection timeout

javascriptmysqlnode.jsamazon-rdsnode-mysql

提问by Haywire

My node.js app gives 5xx due to connection timeouts at random times. Here's how I connect and query:

由于随机时间的连接超时,我的 node.js 应用程序给出了 5xx。这是我连接和查询的方式:

var mysql = require('mysql');
var config = {  
            host: '172.10.1.1',
            port: 3306,
            user: 'user',
            password: 'pwd',
            database: 'mydb',
            connectionLimit: 15,
            queueLimit: 30
        }

var poolCluster = mysql.createPool(config);

var queryDB = function(query, cb) {
    poolCluster.getConnection(function(err, connection) {
        if(err) {
            cb(err, null);
        }
        else {
            connection.query(query, function (err, rows) {
                connection.release();
                cb(err, rows);
            });
        }
    });
};

Also, in another alternate version, with connection pooling disabled, the code looks like this:

此外,在另一个替代版本中,禁用连接池后,代码如下所示:

queryDB = function(query, cb) {
    var connection = mysql.createConnection(config);
    connection.query(query, function(err, rows) {
        connection.end();
        cb(err, rows);
    });
};

But both the setups give Error: connect ETIMEDOUT at Connection._handleConnectTimeout

但是这两种设置都给出了错误:在 Connection._handleConnectTimeout 连接 ETIMEDOUT

A similar project to my current setup can be seen here: https://github.com/hay-wire/NodeBootstrap/blob/master/models/UsersUtils.js

可以在此处看到与我当前设置类似的项目:https: //github.com/hay-wire/NodeBootstrap/blob/master/models/UsersUtils.js

It would be great if you could point out what could be going wrong with the connections. Thanks.

如果您能指出连接可能出现的问题,那就太好了。谢谢。

UPDATE

更新

Since the node.js service was running in cluster mode, I thought maybe a race condition across the threads to acquire mysql connection resource from the shared connection pool is the reason. So I switched off the cluster mode to single thread mode and connection timeouts stopped.

由于 node.js 服务在集群模式下运行,我认为可能是线程之间的竞争条件从共享连接池获取 mysql 连接资源是原因。所以我将集群模式关闭到单线程模式并且连接超时停止。

Still I'm not convinced it was the race condition causing this issue. Any way to verify this?

我仍然不相信这是导致这个问题的竞争条件。有什么方法可以验证这一点吗?

回答by DR.

This has nothing to do with the timeout. I noticed the following (if you are using it in something like AWS Lambda functions, I think this also applies to many situations with callbacks).

这与超时无关。我注意到以下几点(如果你在 AWS Lambda 函数之类的东西中使用它,我认为这也适用于回调的许多情况)。

You are calling the connection.end();before it actually sends the COM_QUIT packet to the MySQL server to close the connection. So the next time you just import var mysql = require('mysql');(in my case at least) it will throw a timeout error as the previous connection still seems open to your machine but has actually been closed by MySQL.

connection.end();在它实际将 COM_QUIT 数据包发送到 MySQL 服务器以关闭连接之前,您正在调用它。因此,下次您刚刚导入时var mysql = require('mysql');(至少在我的情况下)它会抛出超时错误,因为之前的连接似乎仍然对您的机器开放,但实际上已被 MySQL 关闭。

Please see this link from directly from the documentation on terminating connections

直接从有关终止连接的文档中查看此链接

So to fix this condition use .destroy()instead of .end().

所以要解决这个问题,请使用.destroy()而不是.end().

connection.query(query, function(err, rows) 
{            
    connection.destroy();
    cb(err, rows);                  
});

Other wise use .end()correctly with a callback as in:

其他明智.end()地使用回调正确使用,如:

connection.query(query, function(err, rows) 
{            
    connection.end(function(errCon) //Don't do anything with end Error
    {
       // The connection is terminated now 
       cb(err, rows);
    });           
});

回答by baao

That happens from time to time, the default acquireTimeout is a bit low (10000ms), so you should increase it if you have multiple connections running. You can set it in your connection options with

这种情况不时发生,默认的acquireTimeout 有点低(10000 毫秒),因此如果您有多个连接正在运行,您应该增加它。您可以在连接选项中设置它

acquireTimeout: 1000000

var config = {  
        host: '172.10.1.1',
        port: 3306,
        user: 'user',
        password: 'pwd',
        database: 'mydb',
        connectionLimit: 15,
        queueLimit: 30,
        acquireTimeout: 1000000
}

回答by user2486108

I was also getting a connection timeout error while executing the following code.

在执行以下代码时,我也遇到了连接超时错误。

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  port:"3307",
  user: "root",
  password: "root",
  database:"nodedb"
});


/*
con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});
*/
module.exports=con;

I removed con.connect() method to resolve this issue. It's working fine now.

我删除了 con.connect() 方法来解决这个问题。它现在工作正常。