Node.js crashing automatically by mysql without any operation in it

*爱你&永不变心* 提交于 2020-01-06 08:25:14

问题


I am working with node.js and facing a problem with crashing of node . Error message shown below.

Error: Connection lost: The server closed the connection.
   at Protocol.end (/var/www/versions/project/js/node_modules/mysql/lib/protocol/Protocol.js:78:13)
   at Socket.<anonymous> (/var/www/versions/project/js/node_modules/mysql/lib/Connection.js:81:28)
   at Socket.EventEmitter.emit (events.js:117:20)
   at _stream_readable.js:919:16
   at process._tickCallback (node.js:419:13)

I'm using mysql queries in node server..but at crashing time it does not have any operation on it... node crashing automatically different period of time.


回答1:


I have found that working with connection pools to grab a connection only when you need it is the best approach.

// do this once
var pool = mysql.createPool( opts );

// then, just before you need a connection do this
pool.getConnection(function(err, connection) {
    if (err) throw err;

    // use the connection

    // don't forget to release the connection when you are done...
    connection.release();
});



回答2:


this is an error from nodejs mysql. "Connection lost, server closed the connection". mysql server has closed the connection created.

i can't be so sure about the way you use mysql connection, you did not provided sample code snippet.

one way to solve this problem is to re create a new connection when the server close old connection, example code provided

    connection.on('error', function(error) {
        if(erroor.code === 'PROTOCOL_CONNECTION_LOST') { 
          //re create the connection here again
          connection = mysql.createConnection(config);
        } else {                                      
          throw err;                                  
        }
      });
    }


来源:https://stackoverflow.com/questions/25358894/node-js-crashing-automatically-by-mysql-without-any-operation-in-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!