如何通过 SSH 连接到 Aurora 无服务器 MySQL 实例

How to Connect to Aurora serverless MySQL instance over SSH

我无法通过 EC2 隧道找到 Aurora MySql Db。

我们有一个 Aurora 无服务器数据库 (MySql)。问题是我不知道如何从我的机器本地连接到数据库。

我尝试将 SSH 选项添加到 mysqlstring 构建器,例如:

        MySqlConnectionStringBuilder _connectionBuilder = new MySqlConnectionStringBuilder()
        {
            UserID = "admin",
            Server = "RDS endpoint in Aws",
            Port = 3306,
            SshHostName = "Ip to the Ec2",
            SshUserName = "the ec2 user",
            SshPort = 22,
            SshKeyFile = @"filepath to local .pem file",
            Database = "db name",
            Password = "db-password"
        };

我尝试同时使用字符串生成器和 ssh 客户端,例如:

 using (var sshClient = new SshClient(_connectionBuilder.SshHostName, 22, _connectionBuilder.SshUserName, new PrivateKeyFile(_connectionBuilder.SshKeyFile)))
            {

                sshClient.Connect();
                // SQL QUERY HERE
                sshClient.Disconnect();
            }

代码在发布到 lambda 实例但不在我的本地计算机上时工作并连接。

如果我打开 CMD window 并输入:

ssh -N -L 3306:{aws Db endpoint}:3306 -i {path to .pem} {user}@{ip}

并将服务器更改为本地主机。

可能您的数据库无法公开访问。

PubliclyAccessible Indicates whether the DB instance is an internet-facing instance. If you specify true, AWS CloudFormation creates an instance with a publicly resolvable DNS name, which resolves to a public IP address. If you specify false, AWS CloudFormation creates an internal instance with a DNS name that resolves to a private IP address.

创建数据库时,确保将其设置为可公开访问并且位于连接了 Internet 网关的子网中。

还要确保数据库的安全组允许连接到您的 SSH 端口 (22) 和数据库 tcp 端口 (3306)。

编辑

您无法访问 VPC 外部的 Aurora 服务器:

You can't give an Aurora Serverless DB cluster a public IP address. You can access an Aurora Serverless DB cluster only from within a virtual private cloud (VPC) based on the Amazon VPC service.

您可以使用 SSH.NET for this. You can find a working example here in the edited question