Mysql连接在节点js中一段时间后关闭并给出PROTOCOL_CONNECTION_LOST'
Posted
技术标签:
【中文标题】Mysql连接在节点js中一段时间后关闭并给出PROTOCOL_CONNECTION_LOST\'【英文标题】:Mysql connection closed after some time in node js and gives PROTOCOL_CONNECTION_LOST'Mysql连接在节点js中一段时间后关闭并给出PROTOCOL_CONNECTION_LOST' 【发布时间】:2018-05-03 13:55:49 【问题描述】:我是节点 mysql 的新手,我遇到了一个问题。 我的节点 js 服务器关闭并关闭连接 PROTOCOL_CONNECTION_LOST。我不明白我哪里出错了。如果有人对此有所了解,请解决。提前致谢
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;
);
);
);
【问题讨论】:
Did you read this? nodejs mysql Error: Connection lost The server closed the connection的可能重复 尝试使用池化:github.com/mysqljs/mysql#pooling-connections 【参考方案1】:你可以恢复连接,当它断开时。例如
connection.on('error', err =>
if (err.code === 'PROTOCOL_CONNECTION_LOST')
// db error reconnect
disconnect_handler();
else
throw err;
);
【讨论】:
如果没有网络连接会造成无限循环吗?以上是关于Mysql连接在节点js中一段时间后关闭并给出PROTOCOL_CONNECTION_LOST'的主要内容,如果未能解决你的问题,请参考以下文章