I'm trying to connect to a MySQL DB running on Ubuntu using C# and SSH.Net.
Our MySQL DB only allows connections from a webserver and going by what I can see from the code below, the client object connects to the webserver and then I open a port to map to 3306 on the DB server. Finally the MySQLConnection
object opens a connection to the MySQL DB.
However I get with exception in sql.Open()
statement:
Unable to connect to any of the specified MySQL hosts
I've checked other threads on the same subject and tried their suggestions, but none of them seem to help me.
and many more.
Here is some test code that I'm using -
var ci =
new ConnectionInfo(
"server", "userid", new PasswordAuthenticationMethod("userid", "userpwd"));
using (var client = new SshClient(ci))
{
client.Connect();
if (client.IsConnected) //This part works fine - I can connect to my Webserver.
{
//not sure if this is correct in our context.
var local = new ForwardedPortDynamic("127.0.0.1",3306);
client.AddForwardedPort(local);
try
{
local.Start();
}
catch (SocketException se)
{
}
string connStr =
"Server = dbserver;Port = 3306;Database = dbname;Uid = dbuserid;Pwd = dbpwd;";
MySqlConnection sql = new MySqlConnection(connStr);
try
{
sql.Open();
local.Stop();
}
catch (MySqlException se)
{
}
}
}
All of the credentials I've put in my code was taken off a working MySQLWorkbench connection to the MySQL DB.
I work exclusively in a Windows environment, so if you want me to check for something on the Ubuntu server please also tell me, if possible, how to check for it.