gpt4 book ai didi

c# - MySqlConnection Open() 未打开且未捕获错误

转载 作者:行者123 更新时间:2023-11-29 15:22:12 27 4
gpt4 key购买 nike

我在 AWS 上托管一个 Aurora MySql 实例,并尝试通过 Lambda 函数从中读取表。

这是我的连接字符串:

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

这是代码(.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<Product>();

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

当尝试打开连接时,代码停止执行,不会捕获或引发任何异常。

来自 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

我真的被困在这里了。我不知道发生了什么。角色、政策等都可以。奇怪的是,尽管连接超时设置为 300 秒,但停止运行所需的时间却更少。

如有任何帮助,我们将不胜感激。提前致谢。

最佳答案

超时通常表示网络连接问题

假设:

  • AWS Lambda 函数配置为使用与 Aurora 实例相同的 VPC

您的安全组配置应该是:

  • Lambda 函数上的安全组 ( Lambda-SG ) - 允许所有出站
  • Aurora 数据库上的安全组 ( Aurora-SG ) - 允许适当端口 (3306?) 上来自 Lambda-SG 的入站连接

Aurora-SG特别允许来自 Lambda-SG 的入站流量.

关于c# - MySqlConnection Open() 未打开且未捕获错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59310932/

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