mysql - 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, f.g. 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!
解决方案
我已经解决了这个问题。它是由默认定义 max_allowed_packet 引起的。在 my.ini (C:\ProgramData\MySQL\MySQL Server 5.7) 中找到 max_allowed_packet。更新为“max_allowed_packet=64M”。重启mysql。完毕。
推荐阅读
- javascript - 当已经从另一台服务器登录到网站时,向一台服务器验证用户
- latex - \dott in gnuplot - 错误
- python - 提取匹配关键字python的行
- javascript - Output all nested array elements with recursion
- php - 如何找到列总和达到某个数字的日期?
- symfony - 将外部 PHP 应用程序包含到 iFrame (Twig/Symfony)
- python - 在 Python 中发送 HTTP API 请求并将 json 数据转换为 csv 格式
- android - 启动应用程序时如何选择一个活动作为启动活动?
- javascript - 将 javascript 对象转换为 JSON
- c# - 使用 clientcontext.executequery() 上传文档和更新共享点库中的元数据时操作已超时