简体   繁体   English

Node-Mysql 抛出连接超时

[英]Node-Mysql throwing connection timeout

My node.js app gives 5xx due to connection timeouts at random times.由于随机时间的连接超时,我的 node.js 应用程序给出了 5xx。 Here's how I connect and query:这是我连接和查询的方式:

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.由于 node.js 服务在集群模式下运行,我认为可能是线程之间的竞争条件从共享连接池中获取 mysql 连接资源是原因。 So I switched off the cluster mode to single thread mode and connection timeouts stopped.所以我将集群模式关闭到单线程模式并且连接超时停止。

Still I'm not convinced it was the race condition causing this issue.我仍然不相信这是导致这个问题的竞争条件。 Any way to verify this?有什么方法可以验证这一点吗?

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();您正在调用connection.end(); before it actually sends the COM_QUIT packet to the MySQL server to close the connection.在它实际将 COM_QUIT 数据包发送到 MySQL 服务器以关闭连接之前。 So the next time you just import var mysql = require('mysql');所以下次你只需 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. (至少在我的情况下)它会抛出超时错误,因为之前的连接似乎仍然对您的机器开放,但实际上已被 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);
    });           
});

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.这种情况不时发生,默认的acquireTimeout 有点低(10000 毫秒),因此如果您有多个连接正在运行,您应该增加它。 You can set it in your connection options with您可以在连接选项中设置它

acquireTimeout: 1000000

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

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.我删除了 con.connect() 方法来解决这个问题。 It's working fine now.它现在工作正常。

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

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