简体   繁体   English

'致命错误:getaddrinfo ENOTFOUND'-node.js mysql lambda

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

I am trying to write a node.js lambda function which will connect to a database within a MYSQL RDS instance in AWS and pull down some sample records, but I am getting the above error. 我正在尝试编写一个node.js lambda函数,该函数将连接到AWS中MYSQL RDS实例内的数据库并提取一些示例记录,但是出现上述错误。

I have created the RDS to be publicly accessible and have also created the lambda method under the same VPC as the RDS. 我创建了RDS以供公众访问,并在与RDS相同的VPC下创建了lambda方法。 The code I am using is below: 我正在使用的代码如下:

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);
            });
        }
    });
}

If someone could point me in the right direction of getting this error resolved that would be awesome. 如果有人可以向我指出解决此错误的正确方向,那就太好了。

Thanks! 谢谢!

You have specified host with port number at the end: 您已在端口末尾指定了主机:

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

Remove :3306 . 删除:3306

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM