4

I am new in node mysql and I am facing an issue.
My node js server gets closed and gives connection closed PROTOCOL_CONNECTION_LOST.
I don't understand where I am going wrong. Please resolve if any one has knowledge about it.
Thanks in advance

var sockjs = require('sockjs');
var mysql = require('mysql');

var connection = mysql.createConnection({
  host     : process.env.IP,
  user     : 'root',
  port     : '80',
  password : '123456',
  database : 'test',
});

connection.connect(function(err) {
    if(err){
        console.log('error in connection is : ',err);
    }else{
        console.log("Connected!");
    }
});

var echo = sockjs.createServer();
var connections = [];


echo.on('connection', function (conn) {
    connections.push(conn);
    conn.on('data', function (message) {        

        var sql1 = "SELECT count(`id`) as counting FROM `complaints_chat` WHERE `createby`='client' and `read`='0'";
        connection.query(sql1, function(err, rows, fields) {
            if (err) throw err;
            if (rows.length > 0) {
                counting=rows[0]['counting'];
            }else{ counting=0; }  
        });
    });
});
Nebbing
  • 47
  • 7
Mohammad Zeeshan
  • 825
  • 1
  • 9
  • 20
  • [Did you read this?](https://stackoverflow.com/questions/20210522/nodejs-mysql-error-connection-lost-the-server-closed-the-connection) – Martin Nov 20 '17 at 12:07
  • 1
    Possible duplicate of [nodejs mysql Error: Connection lost The server closed the connection](https://stackoverflow.com/questions/20210522/nodejs-mysql-error-connection-lost-the-server-closed-the-connection) – Martin Nov 20 '17 at 12:07
  • Try using pooling: https://github.com/mysqljs/mysql#pooling-connections – Musa Haidari Dec 15 '19 at 08:04

1 Answers1

0

you can recover connect,when it's unconnect.such as

connection.on('error', err => {
        if (err.code === 'PROTOCOL_CONNECTION_LOST') {
            // db error reconnect
            disconnect_handler();
        } else {
            throw err;
        }
    });
Kirito K
  • 29
  • 1
  • 1
  • 10