简体   繁体   English

Node.js mysql 读取 ECONNRESET

[英]Node.js mysql read ECONNRESET

After searching for long time regarding this error and not really be able to figure out how to handle this error in my project.在搜索了关于这个错误的很长时间之后,并没有真正弄清楚如何在我的项目中处理这个错误。 I need some help from you:).我需要你的帮助:)。

I've got the problem, that my Screen with my Node-APP will give me a error after some hours (I don't know after how many hours but my feeling says me it isn't always the same time)我遇到了问题,我的屏幕和我的 Node-APP 会在几个小时后给我一个错误(我不知道几个小时后,但我的感觉告诉我它并不总是相同的时间)

I will give you my error below.我会在下面给你我的错误。 Does anyone know how to fix this?有谁知道如何解决这一问题? I already tried with an interval of select everything from one short table, to reset the connection to database, so the connection will not be closed, because there will be always (every 30 Minutes) a short call to the Database.我已经尝试过间隔 select 从一个短表到重置与数据库的连接,因此连接不会关闭,因为总会(每 30 分钟)对数据库进行一次短调用。

Also after I get the error and restart the app everything works fine.同样在我收到错误并重新启动应用程序后一切正常。 I saw its a idle-time problem, but I did not find any way to solve this problem for me.我看到它是一个空闲时间问题,但我没有找到任何方法来解决这个问题。 This is how I create the connection to my database.这就是我创建与数据库的连接的方式。

var con = mysql.createConnection({
   host: process.env.DATABASE_HOST,
   user: process.env.DATABASE_USER,
   password: process.env.DATABASE_PASSWORD,
   database: process.env.DATABASE });


  con.connect(function(err) {
   if (err) throw err;
   console.log("Connected to Database!");
 });

Thank you very much in advance!非常感谢您!

The best regards最诚挚的问候

The error:错误:

node:events:371
      throw er; // Unhandled 'error' event
      ^

Error: read ECONNRESET

    at TCP.onStreamRead (node:internal/stream_base_commons:211:20)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (/var/www/xxx/xxx/node_modules/mysql/lib/Connection.js:423:8)
    at Protocol.emit (node:events:394:28)
    at Protocol._delegateError (/var/www/xxx/xxx/node_modules/mysql/lib/protocol/Protocol.js:398:10)
    at Protocol.handleNetworkError (/var/www/xxx/xxx/node_modules/mysql/lib/protocol/Protocol.js:371:10)
    at Connection._handleNetworkError (/var/www/xxx/xxx/node_modules/mysql/lib/Connection.js:418:18)
    at Socket.emit (node:events:394:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  errno: -104,
  code: 'ECONNRESET',
  syscall: 'read',
  fatal: true
}

And again THANK YOU:!!再次感谢您:!! :) :)

I think it becuse of multipale DB connections我认为这是因为多数据库连接

Use createPool instaed of createConnection and defile connectionLimit, acquireTimeout milliseconds so connection auto close after this time使用createPool代替createConnection和 defile connectionLimit,acquireTimeout 毫秒,以便在此时间后自动关闭连接

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  acquireTimeout  : 10000
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret',
  database        : 'my_db'
});

pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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