nodejs mysql Error: Connection lost The server closed the connection - mysql

when I use node mysql, an error is appear between 12:00 to 2:00 that the TCP connection is shutdown by the server. This is the full message:
Error: Connection lost: The server closed the connection.
at Protocol.end (/opt/node-v0.10.20-linux-x64/IM/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
There is the solution. However, after I try by this way, the problem also appear. now I do not know how to do. Does anyone meet this problem?
Here is the way I wrote follow the solution:
var handleKFDisconnect = function() {
kfdb.on('error', function(err) {
if (!err.fatal) {
return;
}
if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
console.log("PROTOCOL_CONNECTION_LOST");
throw err;
}
log.error("The database is error:" + err.stack);
kfdb = mysql.createConnection(kf_config);
console.log("kfid");
console.log(kfdb);
handleKFDisconnect();
});
};
handleKFDisconnect();

Try to use this code to handle server disconnect:
var db_config = {
host: 'localhost',
user: 'root',
password: '',
database: 'example'
};
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();
In your code i am missing the parts after connection = mysql.createConnection(db_config);

I do not recall my original use case for this mechanism. Nowadays, I cannot think of any valid use case.
Your client should be able to detect when the connection is lost and allow you to re-create the connection. If it important that part of program logic is executed using the same connection, then use transactions.
tl;dr; Do not use this method.
A pragmatic solution is to force MySQL to keep the connection alive:
setInterval(function () {
db.query('SELECT 1');
}, 5000);
I prefer this solution to connection pool and handling disconnect because it does not require to structure your code in a way thats aware of connection presence. Making a query every 5 seconds ensures that the connection will remain alive and PROTOCOL_CONNECTION_LOST does not occur.
Furthermore, this method ensures that you are keeping the same connection alive, as opposed to re-connecting. This is important. Consider what would happen if your script relied on LAST_INSERT_ID() and mysql connection have been reset without you being aware about it?
However, this only ensures that connection time out (wait_timeout and interactive_timeout) does not occur. It will fail, as expected, in all others scenarios. Therefore, make sure to handle other errors.

better solution is to use the pool - it will handle this for you.
const pool = mysql.createPool({
host: 'localhost',
user: '--',
database: '---',
password: '----'
});
// ... later
pool.query('select 1 + 1', (err, rows) => { /* */ });
https://github.com/sidorares/node-mysql2/issues/836

To simulate a dropped connection try
connection.destroy();
More information here: https://github.com/felixge/node-mysql/blob/master/Readme.md#terminating-connections

Creating and destroying the connections in each query maybe complicated, i had some headaches with a server migration when i decided to install MariaDB instead MySQL. For some reason in the file etc/my.cnf the parameter wait_timeout had a default value of 10 sec (it causes that the persistence can't be implemented). Then, the solution was set it in 28800, that's 8 hours. Well, i hope help somebody with this "güevonada"... excuse me for my bad english.

Related

NodeJS MySQL, reconnecting on connection loss?

function Connect() {
var connection = mysql.createConnection({
host : 'hidden',
user : 'hidden',
password : 'hidden',
database : 'hidden'
});
connection.connect(function(err) {
if (err) {
console.log('Error connecting to Database'.red);
setInterval(Connect, 5000);
connection.end();
}else{
console.log('Connected to Database'.green);
}
});
}
Connect();
I am attempting to make it so my code won't crash if the database loses connection.
It will be connected to a database which is prone to many restarts and updates, meaning that the program will crash upon losing connection.
I tried adding setInterval(Connect, 5000); so it would attempt to reconnect if it loses connection but to no luck.
Can someone help me out where I am going wrong?
Assuming you're using the mysql npm package you can use connection pooling provided by the library:
https://github.com/mysqljs/mysql#pooling-connections
When a previous connection is retrieved from the pool, a ping packet is sent to the server to check if the connection is still good.
It's rather easy to use, instead of using createConnection you can use createPool
var pool = mysql.createPool({
connectionLimit : 2,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
And then use the pool to query the database.
It has many additional benefits as well e.g. load balancing connections.

How can I handle MySQL disconnection on NodeJS?

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 using a shared hosting, I can't edit the idle time on the MySQL config and I'll need to handle it in code.
I'm using module.exports to handle my connection, as shown below. So how can I make an auto-reconnection script to take care of my issue? Thank you.
var mysql = require('mysql');
module.exports =
{
handle: null,
connect: function(call){
this.handle = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'test',
timezone: 'utc',
charset : 'utf8'
});
this.handle.connect(function (err) {
if(err) {
console.log("[MySQL] Connection error: " + err.code);
} else {
console.log("[MySQL] Successfully connected");
}
});
}
};
The node mysql module that you are using also has a connection pooling mechanism.
Check out the docs at https://github.com/mysqljs/mysql#pooling-connections
Connection pools will make you task easier. You can then store the connection pool object and use its getConnection method to obtain a connection. Make sure that you release the connection when you are done with it.
If for some reason you cant use connection pooling then you will have to listen for error event on the connection and handle it accordingly. But I strongly recommend that you use connection pool.

"ER_CON_COUNT_ERROR: Too many connections" Error with pool connections to mysql from node.js

I have about 20 node.js files that use the following configuration to access my db:
var pool = mysql.createPool({
host: databaseHost,
user: databaseUser,
password: databasePassword,
database: databaseName,
multipleStatements: true
});
The functions all use the following pattern:
pool.getConnection(function (err, connection) {
if (err) {
callback(err);
} else {
// Use the connection
var sql = "...sql statement...";
var inserts = [...inserts...];
connection.query(sql, inserts, function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) {
callback(error);
} else {
callback(null, results);
}
});
}
});
I recently started getting the error:
"ER_CON_COUNT_ERROR: Too many connections"
on calls to any of my functions. I don't really understand the pool concept well enough. If each function is creating a pool, does that create a separate pool each time that function is called?
I understand get connection and release connection. Just don't really get the createPool.
I tried to log the following:
console.log(pool.config.connectionLimit); // passed in max size of the pool
console.log(pool._freeConnections.length); // number of free connections awaiting use
console.log(pool._allConnections.length); // number of connections currently created, including ones in use
console.log(pool._acquiringConnections.length); // number of connections in the process of being acquired
the result was:
10
0
0
0
I can increase the number of connections but would like to have some better understanding of why the problem exists.
If your createPool is called inside functions everytime there has to be a query, then yes, it is grave! Instead, have a different file only for mysql connection. Write a class where you create a pool inside a function, and then in the constructor you could simply return the connection from the pool. That way, if you simply require this file anywhere in your project, and create an object of the class, you could then simply use it to query and release!

mysql connection lost error nodejs

I am connecting my node to mysql using the below code for all my rest apis which i am using in my project;
i have put this as a common db connecting file for all my query request.
var mysql = require('mysql');
var db_connect = (function () {
function db_connect() {
mysqlConnConfig = {
host: "localhost",
user: "username",
password: "password",
database: "db_name"
};
}
db_connect.prototype.unitOfWork = function (sql) {
mysqlConn = mysql.createConnection(mysqlConnConfig);
try {
sql(mysqlConn);
} catch (ex) {
console.error(ex);
} finally {
mysqlConn.end();
}
};
return db_connect;
})();
exports.db_connect = db_connect;
The above code works fine and i will use my query for execution with the 'sql' as below in all of my rest api as below.
var query1 = "SELECT * FROM table1";
sql.query(query1,function(error,response){
if(error){
console.log(error);
}
else{
console.log(response);
}
})
everything goes good till now but the problem is i am getting the sql protocol connection error
after 8-12 hours of running my forever module
forever start app.js
i am starting my project with the above forever module.
after 8-12 hours i am getting the below error and all my rest api are not working or going down.
"stack": ["Error: Connection lost: The server closed the connection.", " at Protocol.end (/path/to/my/file/node_modules/mysql/lib/protocol/Protocol.js:109:13)", " at Socket.<anonymous> (/path/to/my/file/node_modules/mysql/lib/Connection.js:102:28)", " at emitNone (events.js:72:20)", " at Socket.emit (events.js:166:7)", " at endReadableNT (_stream_readable.js:913:12)", " at nextTickCallbackWith2Args (node.js:442:9)", " at process._tickDomainCallback (node.js:397:17)"],
"level": "error",
"message": "uncaughtException: Connection lost: The server closed the connection.",
"timestamp": "2017-09-13T21:22:25.271Z"
Then i got a solution in my research to configure for handle disconnection as below.
But i am struggling to configure my sql connection as below with my code.
var db_config = {
host: 'localhost',
user: 'root',
password: '',
database: 'example'
};
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();
can anyone help me in altering my code with the above code?
SHOW SESSION VARIABLES LIKE '%wait_timeout';
SHOW GLOBAL VARIABLES LIKE '%wait_timeout';
One of them is set to 28800 (8 hours). Increase it.
Or... Catch the error and reconnect.
Or... Check on how "connection pooling" is handled in your framework.
But... Be aware that network glitches can occur. So, simply increasing the timeout won't handle such glitches.
Or... Don't hang onto a connection so long. It it not playing nice. And it could lead to exceeding max_connections.
(Sorry, I don't understand your application well enough to be more specific about which of these many paths to pursue.)
max_connections
...wait_timeout and max_connections go together in a clumsy way. If the timeout is "too high", the number of connections can keep growing, thereby threatening "too many connections" error. In typical designs, it is better to lower the timeout to prevent clients from wastefully hanging onto a connection for too long.
If your situation is this: "Fixed number of clients that want to stay connected forever", then increase the timeout, but not max_connections (at least not much beyond the fixed number of clients).
Still, if the network hiccups, the connections could break. So, you can still get "connection lost". (However, if everything is on the same machine, this is rather unlikely.)
I have sloved this problem by using pool connection. Try it in this way https://www.npmjs.com/package/mysql
var mysql = require('mysql');
var pool = mysql.createPool(...);
pool.getConnection(function(err, connection) {
// Use the connection
connection.query('SELECT something FROM sometable', function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) throw error;
// Don't use the connection here, it has been returned to the pool.
});
});

Node.Js - no output from mysql connection

Sorry in advance - using nodejs for the first time..
I have installed nodejs and npm manager on linux machine. Through the npm I installed mysql module and now I try to test the mysql connection using the simple code. The problem is - no output is printed to the console, when the mysql related code is run!
source:
var mysql = require("mysql");
console.log('1');
var connection = mysql.createConnection({
host: "127.0.0.1",
user: "xxx",
password: "xxxx",
database: "xxx",
port: 3306
});
console.log('2');
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
console.log('3');
connection.end(function(err) {
console.log('Connection closed');
});
console.log('4');
process.exit();
The output is 1234:
Here are installed modules:
So my question is - why are there no messages coming from mysql connection? I tried to enter incorrect connection details on purpose - no messages were produced.
I also tried running node as sudo and also tried running nodejs index.js instead of node index.js. Oh, and I also tried to install and use nodejs-mysql module instead of mysql. Nothing seems to be working.
You're writing asynchronous code, so you need to wait for those operations to complete before you force kill the process.
What you're essentially saying is "Do this, do this, do this, and get back to me later. Also shut everything down right now."
Remove the process.exit call or move it inside a callback function so it's triggered at the right time:
var connection = mysql.createConnection({
...
});
console.log('2');
connection.connect(function(err){
if(err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
console.log('3');
connection.end(function(err) {
console.log('Connection closed');
console.log('4');
process.exit();
});
});
This aggressive nesting and re-nesting is why things like promises exist. Bluebird is a great library for implementing and using these and is used by database wrappers like Sequelize to keep your code organized.