Javascript ER_CON_COUNT_ERROR:node-mysql 中的连接过多错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27746512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ER_CON_COUNT_ERROR: Too many connections error in node-mysql
提问by u134211
I'm a node beginner. Following is the code that I'm trying to execute using node mysql but it keeps giving me this error:
我是一个节点初学者。以下是我尝试使用 node mysql 执行的代码,但它一直给我这个错误:
error connecting: Error: ER_CON_COUNT_ERROR: Too many connections
::CODE::
::代码::
var size = item.length;// size is ~1500
for (var i=0; i<size;i++) {
var connection = mysql.createConnection({
host : 'xxx.xxx.xxx.xxx',
database : 'mydb',
user : 'test',
password : 'test'
});
connection.connect(function(err, callback) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
});
var entry = {
id: item[i],
time_created: Math.floor(Date.now() / 1000),
time_created: Math.floor(Date.now() / 1000),
price_range: 0
};
var query = connection.query('INSERT INTO mytable SET ?', entry, function(err, result, callback) {
if(err) {
console.log(err.message);
}
});
connection.end(function(err) {
if(err) {
console.log(err.message);
}
});
}
How should I re-design the above code to successfully execute it?
我应该如何重新设计上面的代码才能成功执行它?
采纳答案by Chirag Jain
First you don't need 1500 connections to insert 1500 items. Remove that code out of your for loop.
首先,您不需要 1500 个连接来插入 1500 个项目。从 for 循环中删除该代码。
Something like this:
像这样的东西:
var size = item.length;// size is ~1500
var connection = mysql.createConnection({
host : 'xxx.xxx.xxx.xxx',
database : 'mydb',
user : 'test',
password : 'test'
});
connection.connect(function(err, callback) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
});
connection.end(function(err) {
if(err) {
console.log(err.message);
}
});
for (var i=0; i<size;i++) {
var entry = {
id: item[i],
time_created: Math.floor(Date.now() / 1000),
time_created: Math.floor(Date.now() / 1000),
price_range: 0
};
var query = connection.query('INSERT INTO mytable SET ?', entry, function(err, result, callback) {
if(err) {
console.log(err.message);
}
});
};
回答by Salem
MySQL by default only accepts 100 simultaneous connections max. In your case you are creating ~1500 so the error you get is normal. You can increase this value but the problem here is in your code.
MySQL 默认最多只接受 100 个同时连接。在您的情况下,您正在创建 ~1500,因此您得到的错误是正常的。您可以增加此值,但这里的问题出在您的代码中。
You should use a pool instead. This will make node create a pool of connections (i think the default is 10) and let them be shared by your queries:
您应该改用池。这将使节点创建一个连接池(我认为默认值为 10)并让它们由您的查询共享:
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'xxx.xxx.xxx.xxx',
database : 'mydb',
user : 'test',
password : 'test'
});
for (var i=0; i<size;i++) {
pool.getConnection(function(err, connection) {
connection.query( 'INSERT INTO ...', function(err, rows) {
connection.release();
});
});
}
Also you may want to consider using a single connection and insert everything at once. You can see in thisanswer how to achieve that.
此外,您可能需要考虑使用单个连接并一次插入所有内容。您可以在此答案中看到如何实现这一目标。

