mysql cannot open connection,MySqlConnection Open()无法打开,没有错误被捕获

I'm hosting an Aurora MySql instance on AWS and trying to read a table from it on a Lambda function.

This is my connection string:

Server=xxx.xxx.xxx4.xxx; port=3306; database=thedatabase; uid=theuser; pwd=thepassword; Connect Timeout=300

This is the code (.Net Core 2.1):

private static void GetFromDb()

{

LambdaLogger.Log($"Function name GetFromDb() has been called.\n");

int counter = 0;

try

{

LambdaLogger.Log($"Using {str}\n");

using (MySqlConnection conn = new MySqlConnection(str))

{

LambdaLogger.Log($"Connection is about to be opened\n");

conn.Open();

LambdaLogger.Log($"Connection was opened\n");

var text = "SELECT * FROM MarketPlace.Customers";

using (MySqlCommand cmd = new MySqlCommand(text, conn))

{

cmd.CommandTimeout = 360;

var reader = cmd.ExecuteReader();

LambdaLogger.Log($"Command was issued\n");

if (reader.HasRows)

{

LambdaLogger.Log($"reader has rows\n");

products = new List();

while (reader.Read())

{

counter++;

LambdaLogger.Log($"Reading # {counter}\n");

Product p = new Product();

p.Id = reader.GetInt32(0);

p.Name = reader.GetString(1);

products.Add(p);

}

}

reader.Close();

LambdaLogger.Log($"{counter} items readed");

}

}

}

catch (Exception ex)

{

throw new Exception($"[GetFromDb] Error {ex.ToString()}", ex);

}

}

When try to open the connection, code stops executing, no exception is caught or raised.

Log from CloudWatch:

START RequestId: 52225968-d360-4d27-8872-305e4b92e346 Version: $LATEST

...

...

Function name GetFromDb() has been called.

Using Server=xxx.xxx.xxx4.xxx; port=3306; database=thedatabase; uid=theuser; pwd=thepassword; Connect Timeout=300

Connection is about to be opened

END RequestId: 52225968-d360-4d27-8872-305e4b92e346

REPORT RequestId: 52225968-d360-4d27-8872-305e4b92e346 Duration: 30030.17 ms Billed Duration: 30000 ms Memory Size: 256 MB Max Memory Used: 107 MB Init Duration: 207.87 ms

2019-12-12T18:23:58.089Z 52225968-d360-4d27-8872-305e4b92e346 Task timed out after 30.03 seconds

I'm really stuck here. I don't have any idea of what is happening. Roles, policies, etc. are ok.

The strange thing is despite connection timeout is set to 300 seconds, it takes less the this to stop running.

Any help would be appreciated. Thanks in advance.

解决方案

A timeout is usually an indication of network connectivity issues.

Assumption:

The AWS Lambda function is configured to use the same VPC as the Aurora instance

Your Security Group configurations should be:

A Security Group on the Lambda function (Lambda-SG) — Allow all Outbound

A Security Group on the Aurora database (Aurora-SG) — Allow inbound connections on the appropriate port (3306?) from Lambda-SG

That is, Aurora-SG specifically allows inbound traffic from Lambda-SG.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值