简体   繁体   English

如何在 RDS 上正确使用带有 MySQL 的 Knex / Bookshelf

[英]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应用程序,在带有BookshelfKnex库的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 个),其中Commandsleep

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.您可以在此期间尝试的事情。

  • Increase acquireConnectionTimeout .增加acquireConnectionTimeout
  • Increase connection pool size.增加连接池大小。

If caused by slow queries then optimize them before trying the above.如果是由慢查询引起的,请在尝试上述操作之前对其进行优化。

Possible methods for logging slow queries:记录慢速查询的可能方法:

  • Enable slow query log on RDS.在 RDS 上启用慢查询日志。
  • Knex query event to log transaction duration assuming you are using transactions.假设您正在使用事务,则 Knex查询事件以记录事务持续时间。

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.

相关问题 如何在 MySQL(Knex/书架)的 JSON 字段中搜索 substring? - How do I search for a substring in a JSON field in MySQL (Knex/Bookshelf)? 书架,Knex和mysql保存对象列表 - Bookshelf, Knex and mysql to save a list of an object 如何与书架和膝关节中的连接表进行交互? - How to interact with a join table in bookshelf and knex? 如何在不使用InvokeThen的情况下使用bookshelf js(和knex querybuilder)将多行插入mysql? - How to insert multiple rows into mysql using bookshelf js (and knex querybuilder) without using InvokeThen? 如何使用 bookshelf.js 或 knex 在 MySQL 中查询按距离与纬度、经度坐标排序的地点? - How to query places sorted by distance with lat, lng coordinates in MySQL with bookshelf.js or knex? 使用Node,Express,Knex和Bookshelf控制对MySQL中数据的访问 - Controlling access to data in MySQL, using Node, Express, Knex and Bookshelf 如何在 Knex.js 和 MySQL 中使用索引 - How to use index with Knex.js and MySQL 如何在 BelongsToMany 连接表上的 Bookshelf/Knex 中查询? - How can I query in Bookshelf/Knex on a BelongsToMany join table? Bookshelf(knex) - belongsToMany关系不起作用 - Bookshelf(knex) - belongsToMany relation not working 使用书架或Knex ORM选择MySQL JSON列数据类型内的特定字段 - SELECT specific fields inside the MySQL JSON Column datatype using bookshelf or Knex ORM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM