MySqlConnection Open() don't open and no error is caught

北城余情 提交于 2020-02-06 08:32:48

问题


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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/59310932/mysqlconnection-open-dont-open-and-no-error-is-caught

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!