mysql icon indicating copy to clipboard operation
mysql copied to clipboard

"read ECONNRESET" error is being thrown when trying to insert large size files.

Open krishna2014 opened this issue 7 years ago • 14 comments

Hi, when I am trying to insert large size files I'm getting the ECONNRESET error. I've created the db connection pool and getting the connection using the pool.getConnection() method. Below is my scenario.

  • I am getting the zip file from the web request.
  • Then reading the file data using fs.readFile() method and trying to insert the file stream data into a table's column of longblob datatype.
  • I've seen no issue when the zip file is below 2 mb to 3 mb size.
  • When it excceds the above size I'm getting this issue.
  • I've gone through similar issues reported in github but couldn't able to fix it.

Stack Trace:

Error: read ECONNRESET
        at exports._errnoException (util.js:1012:11)
        at TCP.onread (net.js:563:26)
    --------------------
        at Protocol._enqueue (app\node_modules\mysql\lib\protocol\Protocol.js:141:48)
        at PoolConnection.query (app\node_modules\mysql\lib\Connection.js:214:25)
        at Query._callback (app\services\basedao.js:85:32)
        at Query.Sequence.end (app\node_modules\mysql\lib\protocol\sequences\Sequence.js:86:24)
        atQuery._handleFinalResultPacket(app\node_modules\mysql\lib\protocol\sequences\Query.js:144:8)
        at Query.EofPacket (\node_modules\mysql\lib\protocol\sequences\Query.js:128:8)
        at Protocol._parsePacket (app\node_modules\mysql\lib\protocol\Protocol.js:280:23)
        at Parser.write (app\node_modules\mysql\lib\protocol\Parser.js:74:12)
        at Protocol.write (app\node_modules\mysql\lib\protocol\Protocol.js:39:16)
        at Socket.<anonymous> (E:\app\node_modules\mysql\lib\Connection.js:109:28)
        at emitOne (events.js:96:13)
        at Socket.emit (events.js:188:7)
        at readableAddChunk (_stream_readable.js:177:18)
        at Socket.Readable.push (_stream_readable.js:135:10)
        at TCP.onread (net.js:542:20)

My code:

var pool = mysql.createPool({
    connectionLimit: 10,
    host: process.env.HOSTNAME,
    port: process.env.PORT,
    user: process.env.USERNAME,
    password: process.env.PASSWORD,
    database: process.env.NAME,
    debug: false
});

function insertpackage() {
   getdbconnection(function(err, connection){
        if (err) {
            console.log('Failed to get connection');
        }
        else {
                    var sqlQuery = 'INSERT INTO user_packages SET ?';
                   /* fileData is the stream that is read from my zip file */
                    var params = {
                              fileName: name,
                              content: fileData
                         };
                    connection.query(sqlquery, params, function (err, result) {
                      connection.release();
                      if (err) {
                            console.log( 'Error in  executing Insert query :' + err.stack);
                      }
                      else {
                         console.log('Package uploaded successfully');
                     }
                   });
           }
   });
}

function getdbconnection(callback) {
    pool.getConnection(function (err, dbconnection) {
        if (err) {
            console.log('Error in acquiring DB Connection :', err);
            callback(null, null);
        }
        else {
            console.log('Acquired Database Connection with Connection Id as :' + dbconnection.threadId);
            callback(null, dbconnection);
        }
    });
}

krishna2014 avatar Apr 18 '17 11:04 krishna2014

Hi @krishna2014 there is no obvious reason why the connection would reset from your example. The provided code is incomplete, however I tried to complete it but wasn't able to reproduce. Could you provide the following?

  1. Version of the MySQL server.
  2. Version of this module.
  3. The complete code.
  4. Any relevant instructions to run the code.

Thank you!

dougwilson avatar Apr 19 '17 02:04 dougwilson

Hi @dougwilson , thanks for your response .Below are the list of versions that I'm using.

  • Mysql server - 5.7.11
  • mysql (npm module) - 2.11.1

My Scenario:

  • I've created an express project and in one of my REST APIs I am implementing this database insert operation and running into this issue.
  • I've attached my code in the zip file.Please extract the code from the zip file and run the npm install and start the server(npm start).By default it will run on 3000 port.
  • I'm running my db scripts on a db schema named packagedb .Please try to create a schema with the same name or give your own schema and also change the database value in the code.
  • Now hit this API http://localhost:3000/insert from rest client .
  • Below is my Stack Trace .
Error: read ECONNRESET
    at exports._errnoException (util.js:1012:11)
    at TCP.onread (net.js:563:26)
    --------------------
    at Protocol._enqueue (E:\Mysql-issue\testApp\node_modules\mysql\lib\protocol\Protocol.js:141:48)
    at PoolConnection.query (E:\Mysql-issue\testApp\node_modules\mysql\lib\Connection.js:208:25)
    at E:\Mysql-issue\testApp\routes\baseDAO.js:54:23
    at E:\Mysql-issue\testApp\routes\baseDAO.js:79:13
    at Ping.onOperationComplete [as _callback] (E:\Mysqlissue\testApp\node_modules\mysql\lib\Pool.js:110:5)
    at Ping.Sequence.end (E:\Mysql-issue\testApp\node_modules\mysql\lib\protocol\sequences\Sequence.js:86:24)
    at Ping.Sequence.OkPacket (E:\Mysql-issue\testApp\node_modules\mysql\lib\protocol\sequences\Sequence.js:95:8)
    at Protocol._parsePacket (E:\Mysql-issue\testApp\node_modules\mysql\lib\protocol\Protocol.js:280:23)
    at Parser.write (E:\Mysql-issue\testApp\node_modules\mysql\lib\protocol\Parser.js:75:12)
    at Protocol.write (E:\Mysql-issue\testApp\node_modules\mysql\lib\protocol\Protocol.js:39:16)
  • Please download the code from the below dropbox link.This zip file consists of a sample express project. https://www.dropbox.com/s/0akcewbpqo7and2/testApp.zip

krishna2014 avatar Apr 19 '17 09:04 krishna2014

Hi dougwilson , any insight in to the issue ??

krishna2014 avatar Apr 24 '17 14:04 krishna2014

Sorry, this issue got lost on my list. I just marked it to remember; I'll take a look soon.

dougwilson avatar Apr 25 '17 03:04 dougwilson

This might be due to your MySQL configuration - max packet size option. Its usually set there to prevent database from flood with data. (sadly works both ways).

Setitch avatar Jun 27 '17 06:06 Setitch

I am facing this same issue. When the file's size is greater than 4 mb the ECONNRESET is thrown. I have succesfully inserted the same file straight to the database using MySQL without node, which led me to believe this is a node issue. My code looks like that:

// main 
function (inner_callback) {
        var db = require('../lib/database')
        var file = ... // 14 Mb of base64 encoded file
        var binaryData = new Buffer(file, 'base64');
        var insertData = {
	        'db': 'temporary_files',
        	'json': {
        		... , // lots of smaller fields
	                'CONTEUDO': binaryData
	        }
        };
        db.insertValue(insertData, function(err, insertResult, successCallback) {
	        if (err) {
	        	return successCallback(new Error("File insertion error."));
	        }
                else {
	        	process.nextTick(function(){ successCallback(null); });
                }
        }, inner_callback);
}

// lib/database

exports.insertValue = function(data, callback, successCallback) {
	var sql = "INSERT INTO " + pool.escapeId(data.db) + " SET ?";
	// get a connection from the pool
	pool.getConnection(function(err, connection) {
		if(err) {
			console.log("Insert value -> Get Connection: ");
			console.log(err);
			callback(true, null, successCallback);
			return;
		}
		connection.query(sql, data.json, function(err, results) {
			connection.release();
			if(err) {
				console.log("Insert value -> Insert: ");
				console.log(err);
				callback(true, null, successCallback);
				return;
			}
			callback(false, results, successCallback);
		});
	});
}

And i get:

Insert value -> Insert:
{ [Error: read ECONNRESET]
  code: 'ECONNRESET',
  errno: 'ECONNRESET',
  syscall: 'read',
  fatal: true }

bruno147 avatar Oct 11 '17 22:10 bruno147

As @Setitch said this https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html could be the possible cause of the error. Quote: "With some clients, you may also get a Lost connection to MySQL server during query error if the communication packet is too large."

@krishna2014 and @bruno147 could you check your database configuration and see if that is the case?

kai-koch avatar Oct 29 '17 00:10 kai-koch

Hi @krishna2014, Could you please share the exact length of the query? Also could you please take a look at this issue to see if these are related?

i-m-sac avatar Mar 11 '19 04:03 i-m-sac

I'm building an API that track the database's binlog files and when it reach 4mb "read ECONNRESET" error is thrown.

Any update on it?

edilsonlm217 avatar Sep 09 '20 21:09 edilsonlm217

Can you tell us what value is set for server variable max_allowed_packet ?

Setitch avatar Sep 10 '20 06:09 Setitch

max_allowed_packet = 524288000 When I mentioned this error it was set to its default value. It seems that get a larger max_allowed_packet number stops the error as you said.

I'll do some extra test today and come with a more detailed information about it.

edilsonlm217 avatar Sep 10 '20 12:09 edilsonlm217

Can you use other client to do the exact query triggering the error - that could cross out error in client

Setitch avatar Sep 12 '20 07:09 Setitch

did you solve it?

itsjuanmatus avatar Aug 25 '21 22:08 itsjuanmatus

@edilsonlm217 did it work out?

itsjuanmatus avatar Aug 25 '21 22:08 itsjuanmatus