user19996
user19996

Reputation: 103

C# programatically remotely connect to Linux server to connect to Amazon RDS DB

I have an executable in C# that needs to remotely connect to a Linux server in order to connect to an Amazon RDS database.

The executable lives on a Windows Server. Only the Linux server is able to connect to the RDS database due to permissions. Since the C# executable needs information from the database, I have found myself needing to first connect to the Linux server, and then from there connect to the database.

// include files
using MySql.Data;
using MySql.Data.MySqlClient;
using Renci.SshNet;    
//...
     using (var client = new SshClient("server.amazonaws.com", "serverUser", privateKeyFile))
     {
            client.Connect();

            MySqlConnection databaseConnection = null;
            string connString = "Server=dbServer.rds.amazonaws.com; database=database " +             
                                "UID=databaseUser password=databasePassword";
            databaseConnection = new MySqlConnection(connString);
            databaseConnection.Open();

            string queryString = "SELECT * FROM tbl_table;"; // errors here


           // ..... }

I get an error when assigning the querystring that says "MySql.Data.MySqlClient.MySqlException: 'Unable to connect any of the specified MySQL hosts.'"

I'm using SSH.Net to form the connection to the Linux server. I'm using MySQL Connector to connect and retrieve information from the database.

If there's something wrong with my code, I'd greatly appreciate it being pointed out, however I'm fairly certain I'm missing some necessary step to properly connect to the database. I noticed other questions asking something similar, but they either haven't been using an RDS DB or didn't have to SSH in through C#.

EDIT: Since posting this, the server I am executing this code on has been given access to the RDS database. I currently do not have time to attempt the solutions posted. What I can say is that after being given access, things are working smoothly only using MySql Connector.

Upvotes: 0

Views: 1713

Answers (3)

Matt Houser
Matt Houser

Reputation: 36113

You're almost there. I'm not familiar with SSH.NET, but when using Putty (an SSH terminal), you would configure SSH "port tunneling" or "port forwarding" which will setup a listening port on your local computer. Any connections to your local port are forwarded along the SSH connection to the remote computer, then forwarded on to the final destination.

https://github.com/sshnet/SSH.NET does mention that it supports "port forwarding". Take a look at using that.

  1. Connect to your Linux box using SSH.NET
  2. Configure a port forward from localhost:12345 to dbServer.rds.amazonaws.com:3306
  3. Use localhost:12345 in your connection string as the target server rather than dbServer.rds.amazonaws.com:3306 for MySqlConnection.

Upvotes: 1

Xavier J
Xavier J

Reputation: 4728

Skip the SSH. On your Linux box, configure IPtables to port-forward to AWS. Pick any arbitrary port number, say 12345.

Any incoming TCP connections to port 12345 will forward traffic to a given host name and port on AWS.

Subsequently, your MySQL connection string will connect to port 12345 on your linux box, but the forward will send the traffic to AWS.

Here are some sample articles:

As IPtables can be a pain in the rear to configure manually, consider installing Webmin first as an easy-to-use web interface to configure iptables with.

Upvotes: 2

Icemanind
Icemanind

Reputation: 48736

Connecting through SSH is not enoguh. What you want to do can't be done. The problem with your code is that the AWS service allows connections to the database only from the Linux machine. You are trying to initiate a connection from the Windows machine. The only way to do this is to create some kind of API between the Linux machine and the Windows machine or else you need to log in to your AWS account and grant database access to your Windows machine

Upvotes: 0

Related Questions