NodeJS服务器在空闲几个小时后关闭连接

时间:2017-02-01 07:57:29

标签: node.js database express

过去几年这个问题已被重新提出,但是解决方案要么使用已弃用的方法,要么不清楚。

NodeJS服务器(在CentOS上运行)闲置几个小时后,通常是在晚上,关闭其连接并关闭可能,因为它失去了与MySQL的连接。

Error: Connection lost: The server closed the connection.
    at Protocol.end (/home/s/prcrm/node_modules/mysql/lib/protocol/Protocol.js:109:13)
    at Socket.<anonymous> (/home/s/prcrm/node_modules/mysql/lib/Connection.js:115:28)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

数据库配置

function DB( host, user, password, database ) {
  this.host = host;     
  this.user = user;
  this.password = password;     
  this.database = database;
  this.connection = mysql.createConnection({
      host: this.host,      
      user: this.user,
      password: this.password,
      database: this.database   
  });

    this.connection.connect(function(err){    
        if(err){
                 console.log('Error connecting to Db');             
                 return;        
               }
        console.log('Connection established');
    });

版本

Node.js: 6.4.0
Express: 4.13.4
MySQL: 10.0.29-MariaDB MariaDB Server
CentOS: 6.8 (Final)

当服务器空闲时,如何保持MySQL连接活动(没有快速和肮脏)的任何建议或指导,假设这会导致问题?

0 个答案:

没有答案