V
V
vetsmen2017-02-07 22:44:21
MySQL
vetsmen, 2017-02-07 22:44:21

Why is mysql dropping the connection?

From time to time the application crashes with the following error:

Error: Connection lost: The server closed the connection.
    at Protocol.end (/srv/arcoin/node_modules/mysql/lib/protocol/Protocol.js:109:13)
    at Socket.<anonymous> (/srv/arcoin/node_modules/mysql/lib/Connection.js:115:28)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

As I understand it, mysql closes the connection to the server (or it just dies). How to be in such a situation? If there is a connection lifetime in the settings, where can I change it?
Right now I'm using the following module to connect:
var mysql = require('mysql');

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'test',
  password : 'test',
  database : 'test'
});

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }
});

module.exports = connection;

They say that to avoid this problem, you just need to create a connection pool:
var connection = mysql.createPool({
  connectionLimit : 30,
  host     : 'localhost',
  user     : 'test',
  password : 'test',
  database : 'test'
});

But will it help in my case? If it helps, how long will mysql connection live?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2017-02-07
@vetsmen

Yes, using a connection pool will help you avoid this problem.
It is also recommended to change the size of the number of concurrent connections (max-connections) in the MySQL settings.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question