Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
MySQL node.js PROTOCOL_CONNECTION_LOST error

I've been encountering this problem where the mysql connection times out every 2 minutes or so. here's my code.

require('dotenv').config();

const LocalStrategy = require('passport-local').Strategy;

const mysql = require('mysql');
const bcrypt = require('bcrypt');
const db = require('../models/database');

handleDisconnect = () => {
    const connection = mysql.createConnection(db.connection);

    connection.connect(function (err) {
        if (err) {
            console.log('error when connecting to db:', err);
            setTimeout(handleDisconnect, 2000);
        }
    });

    connection.on('error', function (err) {
        console.log('db error', err);
        if (err.code === 'PROTOCOL_CONNECTION_LOST') {
            handleDisconnect();
        } else {
            throw err;
        }
    });
    return connection;
}


connection = handleDisconnect();
connection.query('USE ' + db.database);

    handleDisconnect();

    //connection.end()
}

If I include connection.end() it breaks it. I tried this handleDisconnect(); workaround but it doesn't work either.

Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

require('dotenv').config();

const LocalStrategy = require('passport-local').Strategy;

const mysql = require('mysql');
const bcrypt = require('bcrypt');
const db = require('../models/database');

// this method keeps the server connected to db
mysql.createConnection({
   host: "hostname", // localhost or remote hostname
   username: "your_username",
   password: "your_password",
   port: "port_number_used_for_db"
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!