简体   繁体   中英

error while inserting LARGE volume data in mysql by using node.js (error code: 'ECONNRESET')

I met an error while inserting large volume data in mysql by using node.js Here is the data

instData = [ [ '73caf3d0-f6a4-11e8-8160-eb5f91ce3830',
               '20181017'],
             [ '73caf3d1-f6a4-11e8-8160-eb5f91ce3830',
               '20181019'],
            ... 49316 more items ]

Here is part of connection code:

 let pool = mysql.createPool(db);

 module.exports = {
   connPool (sql, val, cb) {  //function name
       pool.getConnection((err, conn) => {
         if(err){
           console.log('Connection Error:' + err);
         }else{
           console.log('allConnections:' + pool._allConnections.length);
           let q = conn.query(sql, val, (err, rows,fields) => {
              if (err) {
                console.log('Query:' + sql + ' error:' + err);
              }
              cb(err, rows, fields);
              conn.release();
            });
         } // end if
      }); // end pool.getConnection
   }, 
  ......

When I run the insert the code, I got the error.

 sql_inst ='insert into demo (id,upload_time) values ?';
 func.connPool(sql_inst, [instData], (err,rows,fields) => {
    if(err==null){
        res.json({code: 200, msg: 'success', data: req.body });
    } else {
        res.json({code: 400, msg: 'failed:'+err});
    }
 });

Error info:

{ Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
--------------------
at Protocol._enqueue (D:\Projects\test\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at PoolConnection.query (D:\Projects\test\node_modules\mysql\lib\Connection.js:200:25)
at pool.getConnection (D:\Projects\test\sql\func.js:29:26)
at Ping.onOperationComplete (D:\Projects\test\node_modules\mysql\lib\Pool.js:110:5)
at Ping.<anonymous> (D:\Projects\test\node_modules\mysql\lib\Connection.js:502:10)
at Ping._callback (D:\Projects\test\node_modules\mysql\lib\Connection.js:468:16)
at Ping.Sequence.end (D:\Projects\test\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Ping.Sequence.OkPacket (D:\Projects\test\node_modules\mysql\lib\protocol\sequences\Sequence.js:92:8)
at Protocol._parsePacket (D:\Projects\vutest\node_modules\mysql\lib\protocol\Protocol.js:278:23)
at Parser.write (D:\Projects\test\node_modules\mysql\lib\protocol\Parser.js:76:12)
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read',
fatal: true }

It seems the connection is closed, but the question is i use connection pool, it should not connect everytime.

When I reduce the size of data, fg the data contains 100 records, the code runs successfully.

Running env: node:v10.11.0 mysql:v5.7

How cold I address this issue? Great thx!

I have fixed this issue. It is caused by the default definition max_allowed_packet. Find max_allowed_packet in my.ini (C:\\ProgramData\\MySQL\\MySQL Server 5.7). Update to 'max_allowed_packet=64M'. Restart mysql. Done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM