You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KnexTimeoutError连接获取超时(连接池已满)问题求助——非Node14与Knex<8.0.3兼容场景

Troubleshooting KnexTimeoutError: Connection Pool Full Issue

Hey there, let's work through this frustrating timeout problem you're hitting. Based on the details you shared, here are the most likely fixes to try, starting with the most obvious one:

1. Fix the MySQL Port Configuration (Critical!)

Looking at your knexFile.js, you've set the MySQL port to 3000—that's Next.js's default development port, not MySQL's standard port (which is 3306). This misconfiguration means Knex is trying to connect to the wrong service entirely. Every connection attempt hangs and eventually times out, filling up your pool with stuck connections. That's almost certainly a major cause of your intermittent errors.

Update your connection config immediately:

module.exports = {
  development: {
    client: 'mysql',
    connection: {
      host: '127.0.0.1',
      port: 3306, // Swap this to MySQL's default port
      user: process.env.MYSQL_USERS,
      password: process.env.MYSQL_PASSWORD,
      database: process.env.MYSQL_DB,
    },
    debug: true,
    pool: { max: 10, min: 2 }, // Temporarily lower max to test
  }
};

2. Adjust Connection Pool Size to Match MySQL Limits

Your pool is set to max: 50, but MySQL has a default max_connections limit of 151 (this can be lower for smaller instances or shared hosts). If other processes are using connections, or your MySQL instance can't handle 50 concurrent connections, you'll hit pool timeouts.

  • Run this SQL query in your MySQL client to check the current limit:
    SHOW VARIABLES LIKE 'max_connections';
    
  • Set Knex's pool.max to a value that's 10-20% lower than MySQL's max_connections to leave room for other processes. Starting with max:10 as a test will help rule out pool overflow from excessive concurrent connections.

3. Check for Unreleased Connections (Connection Leaks)

Intermittent errors often come from connections that never get returned to the pool. Common culprits include:

  • Forgetting to await database queries or handle their promises:
    // ❌ Bad: Connection stays open because the promise isn't resolved/rejected
    knex('users').where('id', 1);
    
    // ✅ Good: Ensure queries are properly handled
    await knex('users').where('id', 1);
    // Or with then/catch
    knex('users').where('id', 1)
      .then(results => /* process data */)
      .catch(err => /* handle error */);
    
  • Unfinished transactions: If you use transactions, always ensure you call trx.commit() or trx.rollback() in all code paths (even if an error occurs):
    const trx = await knex.transaction();
    try {
      await trx('users').insert({ name: 'Test User' });
      await trx.commit();
    } catch (err) {
      await trx.rollback();
      throw err; // Don't swallow the error
    }
    

4. Upgrade Dependencies for Better Compatibility

Your Knex version (0.95.13) is quite old, and while you mentioned avoiding Node 14/Knex <8.0.3 issues, older Knex versions can have compatibility quirks with the mysql package. The mysql package itself hasn't been updated in years—switching to mysql2 (a maintained, drop-in replacement) often fixes connection-related bugs:

  1. Uninstall the old mysql package:
    npm uninstall mysql
    
  2. Install mysql2:
    npm install mysql2
    
  3. Update your Knex config's client to mysql2 (though Knex often works with mysql client setting and mysql2 installed, explicitly setting it avoids confusion):
    client: 'mysql2',
    

5. Use Debug Logs to Diagnose

You already have debug: true enabled in your Knex config—check the terminal logs for clues. Look for:

  • Repeated failed connection attempts (confirming the port issue)
  • Connections being created but never released (pointing to leaks)
  • Errors about exceeding MySQL's connection limits

These logs will help you confirm which fix is addressing your specific problem.


内容的提问来源于stack exchange,提问作者reynaldy saputra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:13:12