MySqlConnection Open()无法打开,没有错误被捕获 [英] MySqlConnection Open() don't open and no error is caught

查看:1014
本文介绍了MySqlConnection Open()无法打开,没有错误被捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在AWS上托管Aurora MySql实例,并尝试在Lambda函数上从中读取表.

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

这是我的连接字符串:

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

这是代码(.Net Core 2.1):

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.

从CloudWatch登录:

Log from 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秒后超时

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秒,但停止运行所需的时间却更少.

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.

推荐答案

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

假设:

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

您的安全组配置应为:

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

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

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

这篇关于MySqlConnection Open()无法打开,没有错误被捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆