sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
4.09k stars 625 forks source link

Error: Connection lost: The server closed the connection. #836

Closed pppplus closed 6 years ago

pppplus commented 6 years ago

Error : Error: Connection lost: The server closed the connection. at Socket. (/patho/to/node_modules/mysql2/lib/connection.js : 100 : 35) at emitNone (events.js:111:20) at Socket.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1064:12) at args.(anonymous function) (/usr/lib/node_modules/pm2/node_modules/event-loop-inspector/index.js:133:29) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickDomainCallback (internal/process/next_tick.js:218:9)

version nodejs : 8.11.3 version mysql2 : 1.6.1

sidorares commented 6 years ago

Can you give a bit more context why this is a problem? Connection lost: The server closed the connection is runtime error, can happen for many reasons ( network, server timeout / restart etc ) and your code must be prepared to handle this. Let us know how code or documentation can help you with that

pppplus commented 6 years ago

Thanks for quick reply. I don't know how to help you, because I just begin with nodejs.

It arrives often, after some minutes.

How can I help you, and have more informations to post here ?

sidorares commented 6 years ago

can you post your server version, and minimal script to reproduce problem so I can run it locally?

pppplus commented 6 years ago

Linux ---- 4.13.13-6-pve #1 SMP PVE 4.13.13-41 (Wed, 21 Feb 2018 10:07:54 +0100) x86_64 x86_64 x86_64 GNU/Linux CentOS release 6.10 (Final)

I test, with no window client opened. Only node running on server.

Connection is cuted after 460-600 seconds.

I delete maximum code, and post a minimal version in some minuts.

pppplus commented 6 years ago
var app = require('express')();
var http = require('http').Server(app);
var SqlString = require('sqlstring');
var yabbcode = require('ya-bbcode');
var parser = new yabbcode();
var striptags = require('striptags');
const util = require('util');//pour voir les objets dans la console !

//var socket = require('socket.io')(http);
var port = 3421;

const mysql = require('mysql2');
// create the connection to database
/* voir pour passer à connections pools pour réduire la latence ?*/
const connection = mysql.createConnection({
  host: 'localhost',
  user: '--',
  database: '---',
  password: '----'
});
sidorares commented 6 years ago

are you making any queries in you code? Your connection might be actively closed by server because it's idle.

pppplus commented 6 years ago

No query done. I send request only when needed. "Your connection might be actively closed by server because it's idle." Yes, but I don't understand why the node program aborts.

So, how to manage this "problem" ?

sidorares commented 6 years ago

Yes, but I don't understand why the node program aborts.

oh, ok, this is easier to explain. When mysql2 connection errors, it fires "error" event. In node "error" event is special in a way that if nobody is handling it default behaviour is to terminate process ( https://nodejs.org/api/events.html#events_error_events ). So in your case 2 possible options: 1) attach error handler and create new connection when you see error:

var connection;
function connectDb() {
  connection  = mysql.createConnection({
    host: 'localhost',
    user: '--',
    database: '---',
    password: '----'
  });
  connection.on('error', connectDb()); // probably worth adding timeout / throttle / etc
}

2) better solution is to use pool - it'll handle this for you.

const pool = mysql.createPool({
  host: 'localhost',
  user: '--',
  database: '---',
  password: '----'
});

// ... later
pool.query('select 1 + 1', (err, rows) => { /* */ });
pppplus commented 6 years ago

thank you very much for your help, and very quick replies. I will test will pool.

pppplus commented 6 years ago

perfect with pool ! No more crash !

sidorares commented 5 years ago

@natnaelab can you elaborate? If your question is different from OP can you please create separate issue?

sergiyvoytovych commented 4 years ago

If you have root or full access to mysql server change this set global wait_timeout = 28800; set global interactive_timeout = 28800;

or /etc/mysql/my.cnf [mysqld] wait_timeout = 28800 interactive_timeout = 28800

wait_timeout this is your pain :)

horacio186 commented 3 years ago

perfecto con piscina ! No más accidente !

Y como quedo tu código con la conexión a la piscina porque no lo puedo agregear

amrsa1 commented 3 years ago

I'm using a pool connection but I found that I'm losing connection when the pool is idle for 1 min, how I can increase this time out here is my pool config

    host: 'localhost',
    port: 3306,
    user: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    waitForConnections: true,
    connectionLimit: 10,
    connectTimeout: 15000,
    rowsAsArray: false,
    enableKeepAlive: true,
    multipleStatements: true
monjemaica commented 2 years ago

Thank you so much this saves me in real tough situation!!

dipakchavda2912 commented 2 years ago

connectTimeout

You might increase the connectTimeout.