Node.Js- Error: Connection lost: The server closed the connection - mysql

Two weeks ago I deployed my first application as part of my new job (Woo Hoo! :)), but every night since I deployed it- the app crashes.
The server-side was built with Node.js, and I'm using XAMPP and MYSQL to run the DataBase.
So this is the error I continuously get:
I was searching for a solution, and found this code:
var connection;
function handleDisconnect() {
connection = mysql.createConnection(con); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
// console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
// console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
I pasted this code after the createConnection function:
var con = mysql.createConnection({
host: "EXAMPLE FOR STUCK OVER FLOW",
user: "EXAMPLE FOR STUCK OVER FLOW",
password: "EXAMPLE FOR STUCK OVER FLOW",
database: "EXAMPLE FOR STUCK OVER FLOW"
});
I will really appreciate for your advice!
Thanks!!!

Related

DB Error: connection lost: server Closed the connection

I'm facing this error when the there are no requests to mysql it goes to idles state and we face this db error. I'm working with node, mysql deployed onto openshift cluster.
How do I keep the db connection alive such that the server never closes the connection?
PFA
Please, lemme know is there any solutions? I'm stuck for past 2 weeks
Update -
Following is the code I'm using
`var connection;
function handleDisconnect() {
connection = mysql.createConnection({
host: config.db.host,
user: config.db.user,
password: config.db.password,
database: config.db.database,
port: config.db.port,
}); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function (err) {
// The server is either down
if (err) {
// or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function (err) {
console.log('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
// Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else {
// connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();`
Since, you are using Node.js, you could use the connection pool.
pooling-connections
Below is a snippet from the link. Notice, connection.release(); It doesn't destroy the connection, but allows the connection to be used again.
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
// Use the connection
connection.query('SELECT something FROM sometable', function (error, results, fields) {
// When done with the connection, release it.
connection.release();
// Handle error after the release.
if (error) throw error;
// Don't use the connection here, it has been returned to the pool.
});
});

How to monitor mysql connection status in node?

So this is from https://www.npmjs.com/package/mysql,
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'example.org',
user : 'bob',
password : 'secret'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
there're 2 parts confuse me,
is connection.connect() making the real connection ? i see it's checking for error. But what happens if everything is ok, but i turn off mysql server after 5 minutes, how to monitor the status pls ?
Even for pool events, i don't see the disconnect event.
for the above code, is there a async/await version for connection.connect() ?
Thanks !
connection.connect is sync you can use it after connection. To handle connection errors you can use:
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
By the way everything is explained in node-mysql read me

Nodejs, mysql 'connection lost the server closed the connection'

I connected mysql to nodejs. After a certain period of time, you will get the error :
'Connection lost the server closed the connection'.
I need your help.
An error has occurred and we added the function handleDisconnect. However, once a disconnect is resolved, the second error occurs again from Connection lost the server closed the connection.
I wonder why it should be only once and not the second one.
ps: The description may not be smooth using a translator.
This is part of the app.js file
// connection to databases
var mysql_dbc = require('./config/db_con')();
var connection = mysql_dbc.init();
mysql_dbc.test_open(connection);
// Added Code
handleDisconnect(connection);
function handleDisconnect(client) {
client.on('error', function (error) {
if (!error.fatal) return;
if (error.code !== 'PROTOCOL_CONNECTION_LOST') throw err;
console.error('> Re-connecting lost MySQL connection: ' + error.stack);
mysql_dbc.test_open(connection);
});
};
You can try to use this code to handle server disconnect:
var mysql = require("mysql");
var configuration = {
host: "localhost",
user: "root",
password: "mysql",
database: "blog"
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(configuration);
connection.connect(function(err) {
if (err) {
console.log("error when connecting to db:", err);
setTimeout(handleDisconnect, 2000);
}else{
console.log("connection is successfull");
}
});
connection.on("error", function(err) {
console.log("db error", err);
if (err.code === "PROTOCOL_CONNECTION_LOST") {
handleDisconnect();
} else {
throw err;
}
});
}
handleDisconnect();
You may lose the connection to a MySQL server due to network problems,
the server timing you out, the server being restarted, or crashing.
All of these events are considered fatal errors.
Re-connecting a connection is done by establishing a new connection.
Once terminated, an existing connection object cannot be re-connected by design.
With Pool, disconnected connections will be removed from the pool freeing up space for a new connection to be created on the next getConnection call.

Handle Disconnect Express JS Throw Error Access Denied for user localhost

hello i'm trying to handle disconnect my server express js , i'm using this code of handle disconnect , and it throws error access denied for user in localhost.
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
and my connection host
var mysql = require('mysql');
var db_config = module.exports = {
'connection': {
'host': 'localhost',
'user': 'root',
'password': ''
},
'database': 'database'
};
and it happends on my server also which is throwing error like access denied for user localhost.
the error shows like this
Error: ER_DBACCESS_DENIED_ERROR: Access denied for user ''#'localhost' to database 'databases'
The error message is telling the empty user (at) localhost is not allowed.
That leads me to the db_config:
Replace db_config with db_config.connection in the code line
connection = mysql.createConnection(db_config.connection);

Node.js and MYSQL handle disconnect

I have cot the following code from a web search and can connect from a client and return data from the database ok.
This is setup on a Mac at home. If I lose the internet connection (usually doing a router re-start to get a faster connection) it recovers okay. However if the connection is down for a while I still have my node.js stuff working but the MYSQL connection is dead. I have to stop and start node.js to re-establish the connection.
var db_config = {
host : 'www.xxxx.com',
user : 'xxx',
password : 'xxx',
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}
handleDisconnect();
What is the best way to ensure the connection recovers?
MrWarby.