'ER_CON_COUNT_ERROR: Too many connections error in node-mysql

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:

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?



Solution 1:[1]

First you don't need 1500 connections to insert 1500 items. Remove that code out of your for loop.

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);
       }
    });
};

Solution 2:[2]

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.

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:

    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 this answer how to achieve that.

Solution 3:[3]

Just close the connection after using it. Use connection.end();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chirag Jain
Solution 2 Community
Solution 3 Marcio Ota