![](/img/trans.png)
[英]db URL works in MySQL Workbench, but not node.js (getaddrinfo ENOTFOUND)
[英]'fatal error: getaddrinfo ENOTFOUND' - node.js mysql lambda
我正在嘗試編寫一個node.js lambda函數,該函數將連接到AWS中MYSQL RDS實例內的數據庫並提取一些示例記錄,但是出現上述錯誤。
我創建了RDS以供公眾訪問,並在與RDS相同的VPC下創建了lambda方法。 我正在使用的代碼如下:
var mysql=require('mysql'); //Require whatever connector you need.
//console.log("beware of ", beware);
var beware=1; //This is declared outside the handler: it is possible that its last value will be reused when we enter the function again.
//This function will return a connection. Notice that it's declared outside
//the handler, thus inside the container. It may be reused but I don't think
//that matters because it is self-contained.
function getConnectionCred()
{
var params={
host : '(myconnname).amazonaws.com:3306',
user : 'username',
password : 'password',
database: 'database'};
return mysql.createConnection(params);
}
//This is your handler.
exports.handler=function(event, context)
{
//This is declared inside the handler: it is guaranteed to never be reused!.
var connection=getConnectionCred();
var del = connection._protocol._delegateError;
connection._protocol._delegateError = function(err, sequence){
if (err.fatal) {
console.trace('fatal error: ' + err.message);
}
return del.call(this, err, sequence);
};
//Do things with your connection.
var query_string='SELECT * from table';
connection.query(query_string, [beware], function(res, err){
//Check for errors, disconnect and exit with failure.
if(err){
console.log("Query failed", err);
connection.end(function(err){
context.fail(0);
});
}
//Disconnect and exit with success.
else{
connection.end(function(err){
if(err){
console.log("Warning: disconnection failed" + err);
}
context.succeed(res);
});
}
});
}
如果有人可以向我指出解決此錯誤的正確方向,那就太好了。
謝謝!
您已在端口末尾指定了主機:
host: '(myconnname).amazonaws.com:3306'
刪除:3306
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.