"ECONNRESET" error in node mysql application

1.5k Views Asked by At

This error comming from my hosted node mysql application. It's wokrs fine in my local computer

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

Error: read ECONNRESET
    at TCP.onStreamRead (node:internal/stream_base_commons:217:20)
Emitted 'error' event on Connection instance at:
    at Connection._handleProtocolError (/home/ufkgmlcg/nodevenv/happyface_social/16/lib/node_modules/mysql/lib/Connection.js:423:8)
    at Protocol.emit (node:events:513:28)
    at Protocol._delegateError (/home/ufkgmlcg/nodevenv/happyface_social/16/lib/node_modules/mysql/lib/protocol/Protocol.js:398:10)
    at Protocol.handleNetworkError (/home/ufkgmlcg/nodevenv/happyface_social/16/lib/node_modules/mysql/lib/protocol/Protocol.js:371:10)
    at Connection._handleNetworkError (/home/ufkgmlcg/nodevenv/happyface_social/16/lib/node_modules/mysql/lib/Connection.js:418:18)
    at Socket.emit (node:events:513: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
}

How to solve this issue ?

2

There are 2 best solutions below

5
Yatin Gupta On

Without knowing the details of where you are hosting the application, and the code snippet used to connect to mysql its hard to tell.

But, most probably in your code where you are connecting to mysql you have added the path to your localhost running mysql while when you are hosting the application the path to mysql database is different.

To solve the issue, you should read how to connect to mysql on the platform you are hosting the application and then use environment variables to connect to it.

You will specify your local database path in your computer while the hosted db path in the hosting platform using environment variables.

1
nirmal salinda On

connection

const db = mysql.createConnection({
        host: process.env.DB_HOST,
        user: process.env.DB_USER,
        password: process.env.DB_PASSWORD,
        database: process.env.DATABASE,
        port: 3306
    });

index

db.connect((error) => error ? console.log(error) : console.log("MysqlDB connected..!"));