我的代码:
var connection = mysql.createConnection(configdb);
//- Establish a new connection
connection.connect(function(err){
console.log(err);
if(err) {
// mysqlErrorHandling(connection, err);
console.log("\n\t *** Cannot establish a connection with the database. ***");
connection = reconnect(connection);
}else {
console.log("\n\t *** New connection established with the database. ***")
}
});
//- Reconnection function
function reconnect(connection){
console.log("\n New connection tentative...");
//- Destroy the current connection variable
if(connection) connection.destroy();
//- Create a new one
var connection = mysql.createConnection(configdb);
//- Try to reconnect
connection.connect(function(err){
if(err) {
setTimeout(reconnect, 5000);
}else {
console.log("\n\t *** New connection established with the database. ***")
return connection;
}
});
}
//- Error listener
connection.on('error', function(err) {
console.log(err);
//- The server close the connection.
if(err.code === "PROTOCOL_CONNECTION_LOST"){
console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
connection = reconnect(connection);
}
//- Connection in closing
else if(err.code === "PROTOCOL_ENQUEUE_AFTER_QUIT"){
console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
connection = reconnect(connection);
}
//- Fatal error : connection variable must be recreated
else if(err.code === "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR"){
console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
connection = reconnect(connection);
}
//- Error because a connection is already being established
else if(err.code === "PROTOCOL_ENQUEUE_HANDSHAKE_TWICE"){
console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
}
//- error fatal
else if(err.code === "ER_BAD_FIELD_ERROR"){
console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
connection = reconnect(connection);
}
//- Anything else
else{
console.log("/!\\ Cannot establish a connection with the database. /!\\ ("+err.code+")");
connection = reconnect(connection);
}
});
Docker日志:
罗克萨娜| > APIOVT@1.0.0 start / home / node / app 罗克萨娜| > node index.js 罗克萨娜| *与数据库建立新连接。 * 罗克萨娜| {错误:连接丢失:服务器关闭了 连接。 罗克萨娜|在Protocol.end (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:113:13) 罗克萨娜|在Socket。 (/home/node/app/node_modules/mysql/lib/Connection.js:109:28) 罗克萨娜|在Socket.emit(events.js:185:15) 罗克萨娜| at endReadableNT(_stream_readable.js:1106:12) 罗克萨娜|在process._tickCallback (internal / process / next_tick.js:178:19)致命:是的,代码: 'PROTOCOL_CONNECTION_LOST'} 罗克萨娜| /!\无法与数据库建立连接。 /!\ (PROTOCOL_CONNECTION_LOST)roxana |
罗克萨娜|新的连接暂定...roxana |
罗克萨娜| *与数据库建立新连接。 * 罗克萨娜| events.js:165 罗克萨娜|扔掉//未处理的'错误'事件 罗克萨娜| ^roxana |
罗克萨娜|错误:连接丢失:服务器关闭了 连接。 罗克萨娜|在Protocol.end (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:113:13) 罗克萨娜|在Socket。 (/home/node/app/node_modules/mysql/lib/Connection.js:109:28) 罗克萨娜|在Socket.emit(events.js:185:15) 罗克萨娜| at endReadableNT(_stream_readable.js:1106:12) 罗克萨娜|在process._tickCallback (内部/过程/ next_tick.js:178:19) 罗克萨娜|发布'错误'事件: 罗克萨娜|在Connection._handleProtocolError (/home/node/app/node_modules/mysql/lib/Connection.js:433:8) 罗克萨娜|在Protocol.emit(events.js:180:13) 罗克萨娜|在Protocol._delegateError (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:392:10) 罗克萨娜|在Protocol.end (/home/node/app/node_modules/mysql/lib/protocol/Protocol.js:117:8) 罗克萨娜|在Socket。 (/home/node/app/node_modules/mysql/lib/Connection.js:109:28) 罗克萨娜| [...匹配原始堆栈跟踪的行...] 罗克萨娜|在process._tickCallback (内部/过程/ next_tick.js:178:19) 罗克萨娜|错误的ERR!代码ELIFECYCLE 罗克萨娜|错误的ERR!错误1 罗克萨娜|错误的ERR! APIOVT@1.0.0开始:node index.js
罗克萨娜|错误的ERR!退出状态1 罗克萨娜| npm ERR! 罗克萨娜|错误的ERR! APIOVT@1.0.0启动脚本失败。 罗克萨娜|错误的ERR!这可能不是npm的问题。那里 可能是上面的额外日志输出。roxana |
罗克萨娜|错误的ERR!可以在以下位置找到此运行的完整日志: 罗克萨娜| npm ERR!
/root/.npm/_logs/2018-06-08T10_38_06_128Z-debug.log 根@ ovdesa:/选择/合成#
有时只会发生,没有连接超时? 怎么了? 该应用程序尚未投入生产。