Nodejs服务器在非活动时间关闭与数据库的连接

时间:2019-01-29 09:11:23

标签: mysql node.js

我正在运行连接到服务器中Mysql的Nodejs并应用大量查询,一切正常。问题出在非活动时间(没有对SQL的查询)或平均在6或7个小时后.. Node.js关闭了连接,服务器停止了。

var db_config = {
host: 'db',
port: '3306',
user: 'root',
password:'root',
database:'active_resourcing',
insecureAuth: true

};

var connection;
connection = mysql.createConnection(db_config);

function connectivity(){
    connection = mysql.createConnection(db_config);
    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(connectivity, 2000); // We introduce a delay before attempting to reconnect,
        }  else{
            console.log('Successfull connection to server ');
        }                                   // to avoid a hot loop, and to allow our node script to
      });          
      connection.on('error', function(err) {
        console.log('db error', err);
        if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
            connectivity();                         // lost due to either server restart, or a
        } else {                                      // connnection idle timeout (the wait_timeout
          throw err;                                  // server variable configures this)
        }
      });
    }





module.exports = {
    connection,

    connectivity
        }

node.js(主文件) 我将第一个连接的可变连接都称为可变连接,然后将其称为睡眠状态时的连接功能。

connection = myConfig.connection;
myConfig.connectivity();

错误

at prtotcol.end {/usr/src/app/node_modules/mysql/lib/protocol.js:1112:13)
at Socket.<anonymous>(/usr/src/app/node_modules/mysql/lib/connection.js:97:28)
at Socket.<anonymous>(/usr/src/app/node_modules/mysql/lib/connection.js:502:10)
at emitNone(events.js:111:20)
at Socket.emit(events.js:208:7)
at endReadableNT(_stream_readable.js:1064:12)
at _combinedTickCallback(internal/process/next_tick.js:139:11)
at process._tickCallback(internal/process/next_tick.js:181:9) fatala: true, code  'PROTOCOL_CONNECTION_LOST'
Successfull connection to server
events.js:183
    throw er; //Unhandled 'errorR event

Error:Connection lost:The server closed the connection.
    at prtotcol.end {/usr/src/app/node_modules/mysql/lib/protocol.js:1112:13)
    at Socket.<anonymous>(/usr/src/app/node_modules/mysql/lib/connection.js:97:28)
    at Socket.<anonymous>(/usr/src/app/node_modules/mysql/lib/connection.js:502:10)
    at emitNone(events.js:111:20)
    at Socket.emit(events.js:208:7)
    at endReadableNT(_stream_readable.js:1064:12)
    at _combinedTickCallback(internal/process/next_tick.js:139:11)
    at process._tickCallback(internal/process/next_tick.js:181:9)

    npm ERR! code LIFECYCLE
    npm ERR! errno1 
    npm ERR! app start: node'server.js'
    npm ERR! Exit status 1 
    npm ERR! 
    npm ERR! Failed at the app start script 
    npm ERR! This is probably not a problem with npm . There is likely additional logging output above   

0 个答案:

没有答案