【问题标题】:Node JS mysql database disconnectNode JS mysql数据库断开连接
【发布时间】:2016-09-20 00:42:36
【问题描述】:

我正在使用 Node JS,我正在尝试连接到 mysql 数据库。由于超时,它不断断开连接,所以我编写了一个函数来在超时时重新连接。虽然我需要它是一个连续的连接,否则我的代码中的引用将不起作用。这是我的相关代码:

var mysql = require('mysql');
var connection = mysql.createConnection({
    host     : '.........', // MySQL Host
    user     : '.........', // MySQL User
    password : '.........', // MySQL Password
    database : '.........' // MySQL Databse
});

// MYSQL INFO

//connection.connect();

function replaceClientOnDisconnect(connection) {
  connection.on("error", function (err) {
    if (!err.fatal) {
      console.log('Databse Error, error not fatal');
      return;
    }

    if (err.code !== "PROTOCOL_CONNECTION_LOST") {
      throw err;
      console.log('PROTOCOL_CONNECTION_LOST Error: Reconnecting to database...');
    }

    setTimeout(function () {
        connection.destroy();
        connection = mysql.createConnection(connection.config);
        replaceClientOnDisconnect(connection);
        connection.connect(function (error) {
          if (error) {
            process.exit(1);
          } else {
              console.log('Reconnected to database!');
          }
    }, 1000); // 1 sec
    });
  });
}

// And run this on every connection as soon as it is created.
replaceClientOnDisconnect(connection);

【问题讨论】:

    标签: javascript mysql node.js timeout


    【解决方案1】:

    您可以使用示例

    var dbConfig = {
            host: '----',
            user: '----',
            password: '----',
            database: '----',
            port: ----
        };
    
    var connection;
    function handleDisconnect() {
        connection = mysql.createConnection(dbConfig);  // Recreate the connection, since the old one cannot be reused.
        connection.connect( function onConnect(err) {   // The server is either down
            if (err) {                                  // or restarting (takes a while sometimes).
                console.log('error when connecting to db:', err);
                setTimeout(handleDisconnect, 10000);    // 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 onError(err) {
            console.log('db error', err);
            if (err.code == '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();
    

    【讨论】:

    • 如果从不调用 on('error') 会发生什么?
    猜你喜欢
    • 2016-11-12
    • 2019-12-31
    • 1970-01-01
    • 2016-12-20
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多