我試圖使用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服務器在本地運行,我試圖連接併成功運行與調度用戶相同的查詢。
有沒有人有這些模塊的經驗,可能是什麼原因的問題?
非常感謝
只好到mysql服務器上運行「的mysql-配置--socket」,包括產生的.sock文件中的「socketPath路徑'連接參數。 – L4zl0w