MySQL by default only accepts 100 concurrent connections max. In your case, you create ~ 1500, so the error you get is normal. You can increase this value, but the problem is here in your code.
Instead, you should use the pool. This will make node create a connection pool (I think the default is 10) and let them be used by your requests:
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(); }); }); }
You can also consider using one connection and insert all at once. You can see in this the answer how to achieve this.
source share