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

时间:2019-12-12 18:41:05

标签: c# mysql .net amazon-web-services amazon-rds

我正在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版本:$ LATEST
  ...
  ...
  函数名称GetFromDb()已被调用。
  使用Server = xxx.xxx.xxx4.xxx;端口= 3306;数据库=数据库; uid =用户; pwd =密码;连接超时= 300
  连接即将打开
  END RequestId:52225968-d360-4d27-8872-305e4b92e346
  REPORT RequestId:52225968-d360-4d27-8872-305e4b92e346持续时间:30030.17 ms计费持续时间:30000 ms内存大小:256 MB使用的最大内存:107 MB初始化持续时间:207.87 ms
  2019-12-12T18:23:58.089Z 52225968-d360-4d27-8872-305e4b92e346任务在30.03秒后超时

我真的被困在这里。我对发生的事情一无所知。角色,策略等都可以。 奇怪的是,尽管连接超时设置为300秒,但停止运行所花费的时间却更少。

任何帮助将不胜感激。预先感谢。

1 个答案:

答案 0 :(得分:0)

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

假设:

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

您的安全组配置应为:

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

也就是说,Aurora-SG专门允许来自Lambda-SG的入站流量。