[英]How to properly use Knex / Bookshelf with MySQL on RDS
I have a Node.js
application using MySQL
on an AWS RDS
with Bookshelf
& Knex
libraries.我有一个
Node.js
应用程序,在带有Bookshelf
和Knex
库的AWS RDS
上使用MySQL
。 The RDS Instance have a max_connections
value 90
. RDS 实例的
max_connections
值为90
。 I am using the following as the connection object.我使用以下作为连接 object。
knex: {
client: 'mysql',
connection: {
host: 'xxxxxxx.rds.amazonaws.com',
user: 'xxx',
password: 'xxxxx',
database: 'xxxx',
charset: 'utf8'
},
debug: true,
pool: {
min: 2,
max: 20
},
acquireConnectionTimeout: 10000
},
const config = require('./environment');
const knex = require('knex')(config.knex);
module.exports = require('bookshelf')(knex).plugin('registry');
'use strict';
const bookshelf = require('../config/bookshelf');
const config = require('../config/environment');
module.exports = bookshelf.model(`TableA`, {
tableName: 'TableA'
}, {});
I have many requests coming along to the application and sometimes crashes with the following errors.我有很多请求伴随着应用程序,有时会因以下错误而崩溃。
Unhandled rejection TimeoutError: Knex: Timeout acquiring a connection.
未处理的拒绝 TimeoutError:Knex:获取连接超时。 The pool is probably full.
游泳池可能已经满了。 Are you missing a.transacting(trx) call?
你错过了 a.transacting(trx) 电话吗?
and和
Error: ER_CON_COUNT_ERROR: Too many connections
错误:ER_CON_COUNT_ERROR:连接太多
Also I see a number of connections (40 to 50 on an average) in the server PROCESSLIST
with Command
as sleep
.此外,我在服务器
PROCESSLIST
中看到许多连接(平均 40 到 50 个),其中Command
为sleep
。
I suspect these error happen when all the 90
connections on the server used fully / knex cannot acquire a new connection from he pool when it tries to.我怀疑这些错误发生在服务器上的所有
90
连接完全使用/knex 尝试从他的池中获取新连接时。 What could be a permanent solution for this, and best practices for handling these kind of applications.什么可能是解决此问题的永久解决方案,以及处理此类应用程序的最佳实践。
I dont think it is the RDS max_connections
that is causing the issue, assuming you only have one instance of the above application code running at any time.我不认为是导致问题的 RDS
max_connections
,假设您在任何时候都只有一个上述应用程序代码的实例。
Your application uses a DB connection pool, which can hold up to 20 connections.您的应用程序使用数据库连接池,最多可容纳 20 个连接。 If all those connections are in use, then the application waits for up to
acquireConnectionTimeout
ms in your case that is 10000
before connection timeout.如果所有这些连接都在使用中,那么在您的情况下,应用程序最多等待
acquireConnectionTimeout
毫秒,即连接超时之前的10000
毫秒。
So I suspect that your application either has a lot of db queries to be processed due to load or there are some slow queries hogging connections.所以我怀疑您的应用程序要么由于负载而需要处理大量数据库查询,要么有一些慢速查询占用连接。 This causes a backlog of queries waiting for connections that eventually times out.
这会导致等待最终超时的连接的查询积压。 Investigate which might be the case and do update us.
调查可能是这种情况并更新我们。
Things you can try in the mean time.您可以在此期间尝试的事情。
acquireConnectionTimeout
.acquireConnectionTimeout
。 If caused by slow queries then optimize them before trying the above.如果是由慢查询引起的,请在尝试上述操作之前对其进行优化。
Possible methods for logging slow queries:记录慢速查询的可能方法:
When a client is finished with MySQL, have it disconnect.当客户端完成 MySQL 后,将其断开。
Also, check the value of wait_timeout
.另外,检查
wait_timeout
的值。 Lowering it will force disconnections rather than "Sleeping" until you come back.降低它会强制断开连接,而不是“睡眠”,直到你回来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.