我尝试通过ssh tunnel forwardedport创建远程MySql连接。 sshClient连接OK。 ForwardedPort启动正常。 当我尝试连接MysqlConnection时,它会抛出System.Net.IO.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;
答案 0 :(得分:1)
我找到了解决问题的方法。由于使用了Forwarded Port,默认端口3306在连接字符串中更改为33306,因此(并告诉我,如果我错了)MySQLClient将SslMode从None(在我的第一次尝试中未设置)更改为任何Required或者等等...... 试图将它设置为MySqlSslMode.None,它最终像魅力一样工作!
使用SSH.Net和MySqlClient
使用SSHClient(ConnectionInfo)
Start ForwardedPortLocal(127.0.0.1,33306,127.0.0.1,3306)
这是代码!
cb = new MySqlConnectionStringBuilder()
{
AllowBatch = true,
Server = this.host,
Port = this.port,
UserID = this.dbuser,
Password = this.dbpassword,
Database = this.database,
SslMode = MySqlSslMode.None,
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(); ....
如果您需要任何帮助,请与我们联系。