NodeJS (Express) with MySQL - How to handle connection resets?

2021-03-10T22:25:31

I'm running my website with NodeJS, I'm using the Express framework. Since MySQL is the only database type at my hosting, I went with it.

I created a db.js file:

const mysql = require('mysql');

const con = mysql.createConnection({
    host: .........
    user: ...........
    password: ............
    database: ............
});
  
con.connect(function(err) {
    if (err) {
        //throw err;
        console.error(err.message);
    } else {
        console.log('Connected Ro MySQL!');
    }
});

module.exports = con;

And I using it like:

const db = require(path.join(__dirname, 'db'));

    db.query('select * from table where name = ?', request.params.id, (error, result) => {
        if (error) { 
            response.send(error.message);
        } else {
            //My Render Stuffs Here
            });
        }
    });

My website works fine however sometimes let's say once every 2 weeks there is a MySQL connection reset, I think the database not available for a minute or so for some reason, and then my website crashes, I have to manually restart NodeJS what is very annoying.

Error in the console:

node:events:355
      throw er; // Unhandled 'error' event
      ^
Error: read ECONNRESET
    at TCP.onStreamRead (node:internal/stream_base_commons:211:20)
[...]
  errno: -4077,
  code: 'ECONNRESET',
  syscall: 'read',
  fatal: true
}

How should I change my codes to prevent crashing? I replaced throw err; with the console.error(err.message); but it only works when I start the website, not when a connection reset happens at runtime.

Copyright License:
Author:「South3492」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/66566718/nodejs-express-with-mysql-how-to-handle-connection-resets

About “NodeJS (Express) with MySQL - How to handle connection resets?” questions

I'm running my website with NodeJS, I'm using the Express framework. Since MySQL is the only database type at my hosting, I went with it. I created a db.js file: const mysql = require('mysql'); co...
I'm using NodeJS with express. I'm store the sessions to a MySQLStore (express-mysql-session) My question is how can i handle connection error? When it can't connect i want to render a page to the ...
I have a front-end app on app.example.com _______backend app on api.example.com My API connects to the backend this way: const mysql = require("mysql2"); const dbConfig = require("../
I'm a novice at NodeJS and Express, I followed a tutorial to create a CRUD with a connection to Mysql where "express-myconnection" is used, that connection is recorded as middleware and is used fro...
edit: Solved my problem. After removing the line connection.connect(function(err) { if (err) throw err; }); I got the raw MySQL error message saying that the login credentials for MySQL are w...
ANy idea how can I connect from NodeJs to the Mysql ClearDB on Heroku? I was able to connect to the ClearDB Mysql runing on Heroku from Navicat, I even created a table called t_users. But I'm having
First of all, I'm a beginner on NodeJS. Well, I'm using a shared hosting to my project and when the database reaches 1 minute of inactivity, NodeJS crashes and disconnects me from MySQL. Since I'm ...
i'm trying to connect Mysql with a files java-script by nodejs. But i want my server apache send my new data automatically to my app android. For this I have a table in my database who have for ...
My nodejs application is connecting to mysql externally, it is all good if i start interactively by docker run -p 49160:8080 -t -i <image>, and run nodejs /src/server.js manually. However, wh...
I am writing an application using NodeJS, Express, mysql, so far everything works fine, but when I run my application after sometime when mysql connection is interrupted my application throughs this

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.