gpt4 book ai didi

mysql - "ER_CON_COUNT_ERROR: Too many connections"从 node.js 到 mysql 的池连接出错

转载 作者:行者123 更新时间:2023-11-29 07:34:40 26 4
gpt4 key购买 nike

我有大约 20 个 node.js 文件使用以下配置来访问我的数据库:

var pool = mysql.createPool({
host: databaseHost,
user: databaseUser,
password: databasePassword,
database: databaseName,
multipleStatements: true
});

所有函数都使用以下模式:

pool.getConnection(function (err, connection) {
if (err) {
callback(err);
} else {
// Use the connection

var sql = "...sql statement...";
var inserts = [...inserts...];

connection.query(sql, inserts, function (error, results, fields) {

// And done with the connection.
connection.release();

// Handle error after the release.
if (error) {
callback(error);
} else {
callback(null, results);
}
});
}
});

我最近开始收到错误:

"ER_CON_COUNT_ERROR: Too many connections" 

调用我的任何函数。我不太了解池的概念。如果每个函数都创建一个池,那么每次调用该函数时是否都会创建一个单独的池?

我了解获取连接和释放连接。只是不要真正获得 createPool。

我尝试记录以下内容:

console.log(pool.config.connectionLimit);     // passed in max size of the pool
console.log(pool._freeConnections.length); // number of free connections awaiting use
console.log(pool._allConnections.length); // number of connections currently created, including ones in use
console.log(pool._acquiringConnections.length); // number of connections in the process of being acquired

结果是:

10
0
0
0

我可以增加连接数,但希望更好地理解问题存在的原因。

最佳答案

如果你的 createPool 在每次必须有查询时都在函数内部调用,那么是的,这很严重!相反,只有一个不同的文件用于 mysql 连接。编写一个类,在其中您在函数内创建一个池,然后在构造函数中您可以简单地返回池中的连接。这样,如果您只是在项目的任何地方需要这个文件,并创建一个类的对象,您就可以简单地使用它来查询和发布!

关于mysql - "ER_CON_COUNT_ERROR: Too many connections"从 node.js 到 mysql 的池连接出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49546125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com