2016-04-13 177 views
2

我正嘗試使用「felixge/node-mysql」包從Lambda中承載的nodejs代碼連接到我的Amazon RDS(MySQL)實例。我需要幫助來找出我做錯了什麼。我得到「連接ETIMEDOUT」錯誤。我的代碼在Lambda中託管,Lamda和RDS都是相同的帳戶和區域。我還將RDSFullAccess策略附加到IAM角色。 RDS實例也可以從所有IP訪問(無白名單/黑名單IP)。從node-mysql連接到Amazon RDS(MySQL)時出錯

與此同時,我能夠從我本地的MySQL Workbench以相同的憑據進行連接。

這裏是我的代碼:

var connection = mysql.createConnection({ 
     host  : 'xxxxxxxx.crhmtrscnbgt.us-east-1.rds.amazonaws.com', 
     user  : 'my_user', 
     password : 'my_passowrd', 
     database : 'my_db', 
     port  : '3306', 
     debug : true 
}); 

connection.connect(function(err) { 
     if (err) { 
       console.error('error connecting: ' + err.stack); 
       return; 
     } 
     console.log('connected as id ' + connection.threadId); 
}); 

var query = "SELECT * FROM users";    

connection.query(query, function(err, rows) {     
     if (err) throw err;     
     console.log(rows); 
}); 

輸出是:

2016-04-12T13:32:34.367Z fa04401a-00b2-11e6-9a23-2dd70e33cc5d error connecting: Error: connect ETIMEDOUT 
at Connection._handleConnectTimeout (/var/task/node_modules/mysql/lib/Connection.js:412:13) 
at Socket.g (events.js:180:16) 
at Socket.emit (events.js:92:17) 
at Socket._onTimeout (net.js:327:8) 
at _makeTimerTimeout (timers.js:438:11) 
at Timer.unrefTimeout [as ontimeout] (timers.js:502:5) 
-------------------- 
at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:141:48) 
at Protocol.handshake (/var/task/node_modules/mysql/lib/protocol/Protocol.js:52:41) 
at Connection.connect (/var/task/node_modules/mysql/lib/Connection.js:123:18) 
at Object.Workout.save (/var/task/storage.js:32:24) 
at Object.storage.saveWorkout (/var/task/storage.js:62:28) 
at intentHandlers.NewExcerciseIntent (/var/task/intentHandlers.js:19:17) 
at AlexaSkill.eventHandlers.onIntent (/var/task/AlexaSkill.js:65:27) 
at AlexaSkill.requestHandlers.IntentRequest (/var/task/AlexaSkill.js:28:37) 
at AlexaSkill.execute (/var/task/AlexaSkill.js:105:24) 
at exports.handler (/var/task/index.js:9:19) 

回答

0

我固定它。問題在於Lambda函數配置中的超時設置。它被設置爲3秒,但我的腳本需要超過10秒才能執行。這就是我得到ETIMEDOUT錯誤的原因。我將設置更改爲30秒。現在沒有問題,一切正常。