我試圖通過SSH隧道forwardedport創建遠程MySql連接。 sshClient連接正常。 ForwardedPort開始OK。 當我嘗試使用的MySqlConnection連接它拋出與消息System.Net.IO.IOException「失敗握手由於意外的數據包格式」c#ssh.net隧道mysql客戶端ioexception
端口即可100%肯定,因爲其他的本機應用程序(如HeidiSQL)可以連接,如果我創建此端口與我的應用程序。
PrivateKeyFile file = new PrivateKeyFile(rsaFile);
client = new SshClient(host, port, username, file);
forwardBoundHost = "127.0.0.1";
forwardBoundPort = 33306;
forwardHost = "127.0.0.1";
forwardPort = 3306;
port = new ForwardedPortLocal(forwardBoundHost, forwardBoundPort, forwardHost, forwardPort);
if(this.response != null){
port.RequestReceived += response;
}
client.Connect();
client.AddForwardedPort(port);
port.Exception += port_Exception;
port.Start();
if (port.IsStarted)
{
cb = new MySqlConnectionStringBuilder()
{
AllowBatch = true,
Server = this.host,
Port = this.port,
UserID = this.dbuser,
Password = this.dbpassword,
Database = this.database,
SslMode = MySqlSslMode.Required,
Keepalive = 60,
ConnectionProtocol = MySqlConnectionProtocol.Tcp,
CharacterSet = "utf8"
};
cb.ConnectionProtocol = MySqlConnectionProtocol.Tcp;
MySqlConnection connection = new MySqlConnection(cb.GetConnectionString(true));
MySqlCommand cmd;
MySqlDataReader reader;
try
{
Console.WriteLine("Mysql client conn");
connection.Open();
}
cmd = connection.CreateCommand();
cmd.CommandText = queryString;
cmd.Prepare();
Array myp = new Array[param.Length];
int i = 0;
foreach (String oneParam in param)
{
myp.SetValue(new MySqlParameter(oneParam, MySqlDbType.String), i);
i++;
}
cmd.Parameters.AddRange(myp);
reader = cmd.ExecuteReader();
}
catch (Exception e)
{
//Logger(e.ToString());
throw (e);
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
connection.Close();
}
return reader;