【问题标题】:'fatal error: getaddrinfo ENOTFOUND' - node.js mysql lambda'致命错误:getaddrinfo ENOTFOUND' - node.js mysql lambda
【发布时间】:2017-12-30 06:06:31
【问题描述】:

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

如果有人能指出我解决这个错误的正确方向,那就太棒了。

谢谢!

【问题讨论】:

    标签: mysql node.js amazon-web-services


    【解决方案1】:

    您在末尾指定了带有端口号的主机:

    host: '(myconnname).amazonaws.com:3306'
    

    删除:3306

    【讨论】:

    • 您好,我已经尝试了上述方法并收到以下错误:pastebin.com/BZ4VyAeS
    • 所以这是一个非常不同的错误。检查 lambda 是否可以访问数据库主机。
    • 您好,Lambda 函数是在与 RDS 相同的 VPC 和安全组下创建的 - 我需要做其他配置吗?
    • @CallumHolden 您需要将此标记为正确答案;您之后收到的错误是由于其他原因。也就是说,您收到的其他错误通常是由于 VPC/安全组问题。三重检查那些;在同一个安全组中是不够的。安全组还需要允许从您的 lambda 函数到 MySQL 实例的流量
    猜你喜欢
    • 1970-01-01
    • 2014-10-20
    • 2013-07-15
    • 2017-10-16
    • 2018-07-25
    • 2018-03-03
    • 2021-08-10
    • 2018-05-24
    • 2016-04-20
    相关资源
    最近更新 更多