C# SSH使用C对MySQL连接进行隧道传输#

C# SSH使用C对MySQL连接进行隧道传输#,c#,mysql,ssh,ssh-tunnel,C#,Mysql,Ssh,Ssh Tunnel,我试图使用SSH隧道来使用C#访问我的MySQL数据库,但我遇到了一个异常 无法连接到任何指定的MySQL主机 我通过以下帮助获得了此代码: 这是我的密码: PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("example.com", 2222, "username", "password"); connectionInfo.Timeout = TimeSpan.FromSeconds(30); using

我试图使用SSH隧道来使用C#访问我的MySQL数据库,但我遇到了一个异常

无法连接到任何指定的MySQL主机

我通过以下帮助获得了此代码:

这是我的密码:

PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("example.com", 2222, "username", "password");
connectionInfo.Timeout = TimeSpan.FromSeconds(30);

using (var client = new SshClient(connectionInfo))
{
    try
    {
        Console.WriteLine("Trying SSH connection...");
        client.Connect();
        if (client.IsConnected)
        {
            Console.WriteLine("SSH connection is active: {0}", client.ConnectionInfo.ToString());
        }
        else
        {
            Console.WriteLine("SSH connection has failed: {0}", client.ConnectionInfo.ToString());
        }

        Console.WriteLine("\r\nTrying port forwarding...");
        var portFwld = new ForwardedPortLocal(IPAddress.Loopback.ToString(),2222, "example.com", 3306); 
        client.AddForwardedPort(portFwld);
        portFwld.Start();
        if (portFwld.IsStarted)
        {
            Console.WriteLine("Port forwarded: {0}", portFwld.ToString());
    Console.WriteLine("\r\nTrying database connection...");

 DBConnect dbConnect = new DBConnect("127.0.0.1", "database", "username", "password", "3306");
    int id =  dbConnect.Count("table");
    MessageBox.Show(id + " count ");
            }
            else
            {
                Console.WriteLine("Port forwarding has failed.");
            }

        }
        catch (SshException ex)
        {
            Console.WriteLine("SSH client connection error: {0}", ex.Message);
        }
        catch (System.Net.Sockets.SocketException ex1)
    {
        Console.WriteLine("Socket connection error: {0}", ex1.Message);
    }

}
我在控制台中得到的输出是:

正在尝试SSH连接。。。
mscorlib.dll中首次出现类型为“System.ObjectDisposedException”的异常
System.dll中首次出现“System.ObjectDisposedException”类型的异常
SSH连接处于活动状态:Renci.SshNet.PasswordConnectionInfo
正在尝试端口转发。。。
转发端口:Renci.SshNet.ForwardedPortLocal
Renci.SshNet.dll中发生了类型为“Renci.SshNet.Common.SshConnectionException”的第一次意外异常
正在尝试数据库连接。。。
System.dll中首次出现类型为“System.Net.Sockets.SocketException”的异常
MySql.Data.dll中首次出现类型为“System.Net.Sockets.SocketException”的异常
MySql.Data.dll中发生了类型为“MySql.Data.MySqlClient.MySqlException”的第一次意外异常
MySql.Data.dll中发生了类型为“MySql.Data.MySqlClient.MySqlException”的第一次意外异常
错误:0:无法连接到任何指定的MySQL主机。
MySql.Data.dll中发生了类型为“MySql.Data.MySqlClient.MySqlException”的第一次意外异常
MySql.Data.dll中发生了类型为“MySql.Data.MySqlClient.MySqlException”的第一次意外异常
未处理的异常:无法连接到任何指定的MySQL主机。。
UPATE: 我已将端口转发设置更改为:

var portFwld = new ForwardedPortLocal("127.0.0.1", 1000, "127.0.0.1", 3306);
我已将mySQL字符串更改为:

connectionString = "server=127.0.0.1;port=1000; UID=username; password=password; database=data1; charset=utf8;Allow User Variables=True";
我正在连接到ssh,我的端口已转发,但我仍然无法连接到MySQL数据库,我遇到了一个异常:

A first chance exception of type 'System.IO.EndOfStreamException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
 Error: 0 : Reading from the stream has failed.

您必须将MySQL连接到转发的绑定端口。也就是到2222

或者更准确地说,使用
portFwld.BoundPort
。同样地,使用
portFwld.BoundHost

DBConnect dbConnect = new DBConnect(portFwld.BoundHost, "database", "username", "password", portFwld.BoundPort);

还请注意,将MySQL主机称为“localhost”比称为“example.com”更有意义,因为主机名是在服务器端解析的。在服务器端,您通常不会连接到“example.com”,而是连接到“localhost”


当然,您需要在需要隧道时保持SSH会话打开。因此,您必须使用块连接到
中的数据库:

using (var client = new SshClient(connectionInfo))
{
    ...
    client.Connect();
    ...
    portFwld.Start();
    ... 
    DBConnect dbConnect = new DBConnect(portFwld.BoundHost, "database", "username", "password", portFwld.BoundPort);
}
var portFwld = new ForwardedPortLocal(IPAddress.Loopback.ToString(), 2222, "localhost", 3306); 
using (var client = new SshClient(connectionInfo))
{
    ...
    client.Connect();
    ...
    portFwld.Start();
    ... 
    DBConnect dbConnect = new DBConnect(portFwld.BoundHost, "database", "username", "password", portFwld.BoundPort);
}