2013-10-14 5 views
5

Node.js 서버에서 웹 응용 프로그램을 실행 중이며 항상 온라인 상태 여야하므로 영원히 사용하고 있습니다. 그러나 여기에 일정 기간이 지나면 무엇이 나옵니 까.Node.js - 서버가 연결을 종료 했습니까?

Error: Connection lost: The server closed the connection. 
    at Protocol.end (/home/me/private/app/node_modules/mysql/lib/protocol/Protocol.js:73:13) 
    at Socket.onend (stream.js:79:10) 
    at Socket.EventEmitter.emit (events.js:117:20) 
    at _stream_readable.js:910:16 
    at process._tickCallback (node.js:415:13) 
error: Forever detected script exited with code: 8 
error: Forever restarting script for 3 time 

나는 약 10 일 동안 똑바로 달리기가 2 번 더있다. 모든 서버에서 "keepalive"루프를 사용하여 매 5 분마다 "select 1"mysql 쿼리를 작성하지만 어떤 차이도 발생하지 않는 것처럼 보입니다.

아이디어가 있으십니까?

EDIT 1 개

내 다른 서버 유사한 오류를주고 있었다, 나는 그것이 "연결 시간 초과"라고 생각, 그래서 나는이 기능을 넣어 :

function keepalive() { 
    db.query('select 1', [], function(err, result) { 
     if(err) return console.log(err);  
     console.log('Successful keepalive.'); 
    }); 
} 

을 그리고 그것은 내 다른 두 개의 서버를 고정 . 하지만 내 주 서버에서 나는 여전히 위의 오류가 나타납니다. 내가 보는 당신이 관심이 무엇 코드 확실하지 않다

var https = require('https'); 
https.createServer(options, onRequest).listen(8000, 'mydomain.com'); 

: 내 메인 서버를 시작하고 방법은 다음과

이다. 기본적으로 서버는 REST API이며 항상 유지해야합니다. 약 2-5, 분당 10 건의 요청이 발생합니다.

+0

응용 프로그램 스크립트에 몇 가지 오류가있는 것 같습니다. 영원히가 아니라 'node app.js'를 실행하여 오류를 봅니다. – Sriharsha

+0

로그에 오류가 표시되지 않고 node-dev로 실행하려고했습니다. 이것은 내가 보는 유일한 오류 메시지입니다. –

+0

앱에 대해 더 자세히 알려주세요. 그래서 우리는 무슨 일이 일어나고 있는지 이해할 수 있습니다. mysql에 대해 소켓을 열어 둔 채로 있습니까? 왜? 원격 데이터베이스입니까? – TheBronx

답변

13

오류는 HTTPS 인스턴스와 관련이 없습니다. MySQL 연결과 관련이 있습니다.

데이터베이스 연결이 예기치 않게 종료되어 처리되지 않습니다. 이 문제를 해결하려면 수동 재 연결 솔루션을 사용하거나 재 연결을 자동으로 처리하는 연결 풀링을 사용할 수 있습니다.

여기 node-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.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(); 
+0

늦게 답장을 드려 죄송합니다. 방금 내 코드를 답으로 바꾸었고 서버를 시작했습니다. 우리는 내일까지 그것이 작동하는지 안다 :) 환호 –

+0

그것이 효과가 있었느냐? 또한, @ hexacyanide, 이것이 왜 필요한지에 대한 생각? MySql 연결이 산발적으로 손실되는 이유는 무엇입니까? –

+2

방금 ​​처음이 오류를 발견했습니다. 몇 달의 가동 시간이 지나면 갑자기 MySQL 연결이 끊어졌습니다. "unattended-upgrades"라는 패키지가 설치되어있는 것으로 밝혀졌으며 자동으로 MySQL 보안 업데이트를 적용하여 MySQL 서버를 다시 시작했습니다. –

관련 문제