2015-05-04 53 views
1

我試圖使用node-schedule和node-mysql模塊構建調度程序和節點(v0.10.33)。代碼如下:Mysql查詢在Node.js調度程序作業中拋出ECONNREFUSED

var schedule = require('node-schedule'); 
var mysql = require('mysql'); 

var rule = new schedule.RecurrenceRule(); 
rule.second = 59; 
schedule.scheduleJob(rule, function(){ 
    var connection = db.connect(); 
    console.log('Successfully established database connection.'); 
    var resultSet = db.select(connection, 'SELECT * from notifications'); 
    db.disconnect(connection); 
}); 

var db = { 

    connect: function(){ 
     var connection = mysql.createConnection({ 
      host  : 'localhost', 
      user  : 'scheduler', 
      password : 'scheduler' 
     }); 
     console.log('Trying to establish connection with database...'); 
     connection.connect(); 
     return connection; 
     }, 

    select: function(connection, queryString){ 
      console.log('Executing database query...'); 
     connection.query(queryString, function(err, rows, fields) { 
      if (err) throw err; 
      console.log('The solution is: ', rows[0].messagetext); 
      return rows; 
     }); 
     }, 

    disconnect: function(connection){ 
    connection.end(); 
    } 
} 

當我執行調度得到以下的輸出:

Trying to establish connection with database... 
Successfully established database connection. 
Executing database query... 

/home/user/notificationscheduler/scheduler.js:32 
      if (err) throw err; 
         ^
Error: connect ECONNREFUSED 
    at errnoException (net.js:904:11) 
    at Object.afterConnect [as oncomplete] (net.js:895:19) 
    -------------------- 
    at Protocol._enqueue (/home/user/notificationscheduler/node_modules/mysql/lib/protocol/Protocol.js:135:48) 
    at Protocol.handshake (/home/user/notificationscheduler/node_modules/mysql/lib/protocol/Protocol.js:52:41) 
    at Connection.connect (/home/user/notificationscheduler/node_modules/mysql/lib/Connection.js:119:18) 
    at Object.db.connect (/home/user/notificationscheduler/scheduler.js:25:14) 
    at Job.db.connect.mysql.createConnection.host [as job] (/home/user/notificationscheduler/scheduler.js:9:25) 
    at Job.invoke (/home/user/notificationscheduler/node_modules/node-schedule/lib/schedule.js:117:10) 
    at null._onTimeout (/home/user/notificationscheduler/node_modules/node-schedule/lib/schedule.js:369:11) 
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15) 

MySQL服務器在本地運行,我試圖連接併成功運行與調度用戶相同的查詢。

有沒有人有這些模塊的經驗,可能是什麼原因的問題?

非常感謝

+0

只好到mysql服務器上運行「的mysql-配置--socket」,包括產生的.sock文件中的「socketPath路徑'連接參數。 – L4zl0w

回答

1

是的,這是因爲異步引擎v8。當您的電話:

db.select(connection, 'SELECT * from notifications') 

它不會等到查詢完成並返回結果。這是你的回調處理,但不是它你正在關閉與db連接和查詢命中錯誤。您需要關閉在選擇連接,如果你想正確的行爲:

select: function(connection, queryString){ 
      console.log('Executing database query...'); 
     connection.query(queryString, function(err, rows, fields) { 
      if (err) throw err; 
      console.log('The solution is: ', rows[0].messagetext); 
      connection.end(); // Here close the connection 
      return rows; 
     }); 
     }, 
+0

好點,謝謝。但我認爲這只是問題的一部分。我得到的錯誤似乎是由TCP連接引起的,而這個連接沒有在我的MySQL服務器上配置。我得到這個驗證,但感謝輸入。 – L4zl0w