gpt4 book ai didi

mysql - ' fatal error : getaddrinfo ENOTFOUND' - node. js mysql lambda

转载 作者:行者123 更新时间:2023-11-29 18:35:58 24 4
gpt4 key购买 nike

我正在尝试编写一个 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

关于mysql - ' fatal error : getaddrinfo ENOTFOUND' - node. js mysql lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45284232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com