【问题标题】:KnexTimeoutError: Knex: Timeout acquiring a connectionKnexTimeoutError: Knex: 获取连接超时
【发布时间】:2022-01-18 10:20:44
【问题描述】:

连接到 knex 时出现以下错误: KnexTimeoutError:Knex:获取连接超时。游泳池可能已经满了。您是否缺少 .transacting(trx) 调用?

这是我的代码:

Api.js

const Knex = require('knex');
const config = require('./config');
const configuration = {
user: config.config.sqlUser, // e.g. 'my-user'
password: config.config.sqlPw, // e.g. 'my-user-password'
database: config.config.sqlDbName, // e.g. 'my-database'
};
configuration.host = `${config.config.sqlConnectionName}`;
const knex = Knex({client: 'pg', connection: configuration});
knex.client.pool.max = 5;
knex.client.pool.min = 5;
knex.client.pool.createTimeoutMillis = 30000; // 30 seconds
knex.client.pool.idleTimeoutMillis = 600000; // 10 minutes
knex.client.pool.createRetryIntervalMillis = 200; // 0.2 seconds
knex.client.pool.acquireTimeoutMillis = 600000; // 10 minutes



router.get('/things', async (req, res) =>{
 
   await methods.getThings(req, res, knex);
});

methods.js:

exports.getThings = async (req, res, knex) => {
let response = {};
try{
    console.log("knex.client.pool.max");
    console.log(knex.client.pool.max);
    response = await knex.select('id', 'userUid', 'firstName', 'lastName', 'cv', 'statement', 'country', 'represented').from('things').where('approved',true)
}
catch (err){
    console.log("error: ", err);
    return res.status(500).json(err);
}
return res.status(200).json(response)
}

我正在使用这些: 节点 v14.0.0 第“8.7.1” knex "0.95.14"

似乎创建与云 sql 的连接(日志中超时 30 秒)存在问题。如何正确创建连接?我应该使用云代理以及如何使用?

我在 VM 中有一个启动脚本,用于启动 node express 服务器。

【问题讨论】:

    标签: node.js postgresql google-cloud-sql knex.js cloud-sql-proxy


    【解决方案1】:

    如果您在 VM 上运行此应用,您还需要将 Cloud SQL Auth Proxy 作为单独的进程运行。

    你可以这样启动代理:

    cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:5432
    

    然后您的应用可以连接到 127.0.0.1(而不是 sqlConnectionName)。

    【讨论】:

    • 如何将 Cloud SQL 身份验证代理作为单独的进程运行?它目前正在破坏我的启动脚本(linux bash)。
    • 在 VM 上下文中做的最简单的事情是通过在代理命令后添加 &amp; 将代理作为后台进程运行。不过,这不值得生产。因此,您可能会考虑使用 systemd 运行代理和您的应用程序。例如,请参阅github.com/GoogleCloudPlatform/cloudsql-proxy/issues/…。
    • 我正在按照您的描述进行操作,但现在连接抛出错误:“表'事物'的权限被拒绝”。这是什么意思以及如何解决?
    • 我设法使用 sql 实例的私有 ip 而不是 127.0.0.1 来获得连接工作。
    • 来自数据库的权限错误意味着您的用户没有适当的数据库权限。尝试连接其他用户或验证您的数据库用户权限。
    【解决方案2】:

    这个错误可能意味着很多事情,但这就是开始的地方: 首先,它也可能是由您的database host name 中的typo 造成的,因此请检查您的凭据两次! 属性propagateCreateError 应设置为false 以防止超时获取连接。游泳池可能已经满了。尝试将此行添加到您的pool configuration。还要更改 min 和 max 例如2-6祝你好运!

    knex.client.pool.propagateCreateError = false; 
    

    此外,我发现了两个有趣的资源,您可能应该阅读12

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多