栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

nodejs mysql错误:连接丢失服务器关闭了连接

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nodejs mysql错误:连接丢失服务器关闭了连接

尝试使用以下代码来处理服务器断开连接:

var db_config = {  host: 'localhost',    user: 'root',    password: '',    database: 'example'};var connection;function handleDisconnect() {  connection = mysql.createConnection(db_config); // Recreate the connection, since      // the old one cannot be reused.  connection.connect(function(err) {   // The server is either down    if(err) {    // or restarting (takes a while sometimes).      console.log('error when connecting to db:', err);      setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,    }    // to avoid a hot loop, and to allow our node script to  });    // process asynchronous requests in the meantime.         // If you're also serving http, display a 503 error.  connection.on('error', function(err) {    console.log('db error', err);    if(err.pre === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually      handleDisconnect();   // lost due to either server restart, or a    } else {     // connnection idle timeout (the wait_timeout      throw err; // server variable configures this)    }  });}handleDisconnect();

在您的代码中,我错过了之后的部分

connection = mysql.createConnection(db_config);



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/411537.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号