问题
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